diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..572f1d8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "effect"] + path = repos/effect + url = https://github.com/Effect-TS/effect.git + shallow = true diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..46cef3b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["VoidZero.vite-plus-extension-pack"] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 863215d..0850d15 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,41 @@ { + "editor.codeActionsOnSave": { + "source.fixAll.oxc": "explicit" + }, + "editor.defaultFormatter": "oxc.oxc-vscode", + "editor.formatOnSave": true, + "editor.formatOnSaveMode": "file", "explorer.fileNesting.enabled": true, "explorer.fileNesting.expand": false, "explorer.fileNesting.patterns": { "*.ts": "${capture}.test.ts, ${capture}.spec.ts", "*.tsx": "${capture}.test.tsx, ${capture}.spec.tsx" + }, + "files.watcherExclude": { + "**/repos/**": true + }, + "js/ts.preferences.autoImportFileExcludePatterns": ["${workspaceFolder}/repos/**"], + "npm.scriptRunner": "vp", + "oxc.fmt.configPath": "./vite.config.ts", + "oxc.lint.customization": { + "require-yield": { + "severity": "off" + } + }, + "oxc.typeAware": true, + "search.exclude": { + "**/repos/**": true + }, + "[javascript]": { + "editor.defaultFormatter": "oxc.oxc-vscode" + }, + "[javascriptreact]": { + "editor.defaultFormatter": "oxc.oxc-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "oxc.oxc-vscode" + }, + "[typescriptreact]": { + "editor.defaultFormatter": "oxc.oxc-vscode" } } diff --git a/AGENTS.md b/AGENTS.md index be2bb70..c021e62 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,24 @@ # AGENTS.md - Never commit or push unless asked for. -- For custom code we write, prefer self-contained domain classes over floating functions. -- For scripts, use `Bun.file(...).text()` or `Bun.file(...).json()` and `Bun.write(...)` for file I/O instead of `node:fs/promises` `readFile` or `writeFile`. +- Put production behavior in `Context.Service`; do not add floating functions, classes, clients, factories, or singletons. +- For scripts, use `Bun.file(...).text()` or `Bun.file(...).json()` for reads. +- For script writes, use `Bun.write(...)`; reserve `node:fs/promises` for application adapters. - Keep `./src/agents/instructions.ts`, the README.md "Agent?" section, and this file's instructions block in sync when editing any of them. + +## Effect reference + +- Run `vp run effect:reference` when `repos/effect/LLMS.md` is missing before writing Effect code. +- Read `repos/effect/LLMS.md`, then use `repos/effect` as read-only reference material. +- Import Effect from package dependencies; never import application code from `repos/effect`. +- Import Effect APIs as named imports from `effect`; do not use deep Effect imports. +- Keep private pure helpers as local const arrows inside a service `make`. +- Keep private effectful helpers as named `Effect.fn("Service.method")(function* (...) {})` values. +- Return public operations from the service `make` as a readonly object using `as const`. +- Keep `ConfigService` APIs data-only; move callable behavior to a capability or platform service. +- Export production layers with descriptive names ending in `Layer`; close internal dependencies in the layer graph. +- Model recoverable operational failures with `Schema.TaggedErrorClass` names ending in `Error`. +- Use `Effect.acquireRelease`, scoped layers, retries, and schedules at the reliability seams that need them. +- Keep `Effect.run*` and `ManagedRuntime` calls at approved application, script, or compatibility boundaries. +- Keep existing focused libraries behind Effect service adapters instead of replacing them. +- Do not create barrel files; import from the module that owns each symbol. diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index b2600a1..a1ec7e2 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -20,7 +20,6 @@ src/ providers/ tooling/ utils/ - define.ts ``` Each domain may contain these layers: @@ -29,9 +28,8 @@ Each domain may contain these layers: types -> config -> repo -> service -> runtime -> ui ``` -Only create a layer directory when the domain has code for it. Every domain exposes a -deliberate public API from `index.ts`. Cross-domain and app imports use that API instead of -importing domain internals. +Only create a layer directory when the domain has code for it. Every service and type is exported +from the concrete module that owns it. Cross-domain and app imports name that module directly. ## Domain ownership @@ -63,68 +61,89 @@ Top-level responsibilities: | `providers` | Domain-independent process, filesystem, clock, environment, and terminal access | | `utils` | Pure domain-independent code with at least two real consumers | | `tooling` | Architecture enforcement and development tooling | -| `define.ts` | Construction vocabulary for behavior-bearing layers and app roots | External data is parsed at `repo`, `runtime`, or `app` boundaries before it enters domain behavior. Keep Rig's public SDK Promise-based and Zod-based while internal architecture moves. +## Domain dependency graph + +Domain edges are explicit and acyclic. A domain may depend only on the domains declared here: + +| Domain | Allowed domain dependencies | +| ------------- | ------------------------------------- | +| `collections` | none | +| `registry` | `settings` | +| `scheduling` | `settings`, `tools` | +| `settings` | none | +| `tools` | `collections`, `registry`, `settings` | +| `updates` | `settings` | + +Across an allowed domain edge, code imports only `types`, `service`, or `runtime` contracts at +the same or an earlier layer. Config and repository implementations remain private to their +owning domain. The architecture model rejects undeclared edges even when they would be acyclic. + +## Effect v4 reliability harness + +Rig keeps its existing libraries and uses Effect v4 as the internal orchestration harness. Effect +owns dependency composition, typed operational failures, resource lifetimes, cancellation, +retries, schedules, and tracing. Commander, Ink, Zod, Bun SQLite, and other focused libraries stay +in place behind Effect services when they already solve their responsibility well. + +- Model capabilities with `Context.Service`; use stable `@rendotdev/rig/...` service identities. +- Export production implementations with descriptive camel-case names ending in `Layer`. +- Name alternatives `testLayer`, `bunLayer`, `sqliteLayer`, or another role-specific `Layer` name. +- Model recoverable operational failures with `Schema.TaggedErrorClass` and an `Error` suffix. +- Use `Schema.Class` and branded schemas for domain entities and value objects when migrating them. +- Use `Effect.acquireRelease`, `Layer.scoped`, or `Scope` for every owned closeable resource. +- Capture service dependencies while constructing the layer; service methods do not leak them. +- Run Effects at application or compatibility boundaries; do not call `Effect.run*` inside Effects. +- Adapt existing Promise and Zod APIs at the boundary instead of forcing downstream tools to move. +- Retry only transient, idempotent operations; cap attempts and use an explicit `Schedule` policy. +- Preserve the final failure cause when retries are exhausted and record retry attempts in spans. +- Keep private pure transformations as local const arrows inside their owning service `make`. +- Keep React rendering and the public Zod SDK compatible while services own their supporting behavior. + +The pinned Effect source lives under `repos/effect` as a shallow, read-only git submodule. Run +`vp run effect:reference` when it is missing from a fresh checkout. Production code imports the +installed `effect` packages, never files under `repos/effect`. Agents should read the referenced +`LLMS.md`, patterns, source, and tests before relying on remembered APIs. + ## Dependency rules Within a domain, a layer may import itself and the layers listed below: -| Source | Allowed targets | -| --------- | ------------------------------------------------------------------- | -| `types` | `types`, `utils` | -| `config` | `types`, `config`, `utils` | -| `repo` | `types`, `config`, `repo`, `providers`, `utils` | -| `service` | `types`, `config`, `repo`, `service`, `providers`, `utils` | -| `runtime` | `types`, `config`, `service`, `runtime`, `providers`, `utils` | -| `ui` | `types`, `config`, `service`, `runtime`, `ui`, `providers`, `utils` | +| Source | Allowed targets | +| --------- | ------------------------------------------------------------- | +| `types` | `types`, `utils` | +| `config` | `types`, `config`, `utils` | +| `repo` | `types`, `config`, `repo`, `providers`, `utils` | +| `service` | `types`, `config`, `repo`, `service`, `providers`, `utils` | +| `runtime` | `types`, `config`, `service`, `runtime`, `providers`, `utils` | +| `ui` | `types`, `config`, `service`, `runtime`, `ui`, `utils` | Additional rules: -- Cross-domain imports use `domains//index.ts` or a deliberate layer `index.ts`. -- `app` imports domain public APIs, providers, utilities, and `define.ts`. -- Providers import only providers, utilities, `define.ts`, and external packages. +- Import cross-domain symbols from the module that owns them; barrel files are forbidden. +- `app` imports domain types, services, runtimes, UI, providers, and utilities. +- Close config and repo dependencies inside exported domain layers before app wiring consumes them. +- Providers import only providers, utilities, and external packages. - Utilities import only utilities and external packages. - Tooling imports only tooling, utilities, and external packages. - Domains and providers never import `app`. - Production code never imports `tooling`. -- Migrated code cannot import a legacy source root. -- Legacy code may import migrated public APIs while its vertical is being moved. +- Domain `repo`, `service`, and `runtime` layers may use Providers; types, config, and UI may not. +- Every production module must classify under this model; there is no legacy source escape hatch. `src/tooling/architecture/architecture.ts` is the executable source of truth. Custom Oxlint rules and structural tests consume the same model. -## Definition vocabulary - -Use helpers that match the owning architectural layer: - -- `defineRepo` in `repo`. -- `defineService` in `service`. -- `defineRuntime` in `runtime`. -- `defineProvider` in `providers`. -- `defineUIComponent` and `defineUIHook` in `ui`. -- `defineApp` at executable app roots. -- `defineSingleton` for intentional shared behavior with stable `params` and `deps`. -- `defineType`, `defineConfig`, and `defineUtil` for named data-only definitions. -- Plain TypeScript for type-only declarations. - -Repository, service, runtime, and provider definitions are classes that extend their matching -helper. Replace `params` and `deps` through the constructor in deterministic tests. Singleton, -component, hook, and app definitions keep their implementation inline in the definition object. - -## Migration sequence - -Rig currently has legacy roots listed in the shared architecture model. They are temporary, -explicit migration allowances rather than permanent exceptions. +## Mechanical enforcement -1. Move one business capability into `domains//`. -2. Add the domain or layer public `index.ts`. -3. Update outside consumers to use the public API. -4. Replace legacy construction with the owning layer helper and direct class implementation. -5. Remove the migrated legacy root or narrow its allowance. -6. Run `vp run ci` before moving the next high-risk runtime seam. +- `src/tooling/architecture/architecture.ts` owns the layer and domain matrices. +- Oxlint rejects invalid source locations and dependency edges with concrete remediation. +- Structural tests parse every production import with the TypeScript AST and validate the real graph. +- CI runs architecture lint, structural tests, Effect diagnostics, dead-code analysis, and behavior tests. +- Human review feedback becomes a documented invariant or a mechanical rule when it can recur. ## Taste invariants @@ -132,7 +151,6 @@ explicit migration allowances rather than permanent exceptions. - Test files contain at most 600 meaningful lines. - Functions and methods contain at most 80 meaningful lines. - Blank and comment-only lines do not count toward these limits. -- Existing oversized files use exact migration ceilings and cannot grow. -- Migration ceilings shrink or disappear as each file is split. - Split by responsibility and layer; avoid generic helper buckets. +- Assign compound `if` conditions to descriptive boolean constants. - Boundary diagnostics include concrete remediation for the next agent run. diff --git a/README.md b/README.md index 10a08d4..3503d22 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Do not implement anything yet. Ask the user which workflow they want to implemen ## Agent? - + > Are you an AI agent looking for how to use rig? Assume the following: diff --git a/bun.lock b/bun.lock index 2c022d5..20c29f8 100644 --- a/bun.lock +++ b/bun.lock @@ -5,18 +5,24 @@ "": { "name": "rig", "dependencies": { + "@effect/platform-bun": "4.0.0-beta.102", "@types/bun": "1.3.14", "commander": "15.0.0", + "effect": "4.0.0-beta.102", "ink": "^7.1.0", "react": "^19.2.7", "typescript": "6.0.3", "zod": "4.4.3", }, "devDependencies": { + "@effect/tsgo": "0.24.3", + "@effect/vitest": "4.0.0-beta.102", + "@oxlint/plugins": "1.68.0", "@types/react": "^19.0.0", "@typescript/native": "npm:typescript@7.0.2", "@vitest/coverage-v8": "4.1.10", "ink-testing-library": "^4.0.0", + "knip": "6.29.0", "pino": "^10.3.1", "vite": "npm:@voidzero-dev/vite-plus-core@0.2.4", "vite-plus": "0.2.4", @@ -45,16 +51,136 @@ "@blazediff/core": ["@blazediff/core@1.9.1", "", {}, "sha512-ehg3jIkYKulZh+8om/O25vkvSsXXwC+skXmyA87FFx6A/45eqOkZsBltMw/TVteb0mloiGT8oGRTcjRAz66zaA=="], + "@effect/platform-bun": ["@effect/platform-bun@4.0.0-beta.102", "", { "dependencies": { "@effect/platform-node-shared": "^4.0.0-beta.102" }, "peerDependencies": { "effect": "^4.0.0-beta.102" } }, "sha512-hTIb0jjTfXhNgnDq2OX5wrigc0OSvTT0VWWb1PLpMM395RJPu8rCIbTlGhy0NS7kZOd8FfgNi9e0sDhgyu+5Ag=="], + + "@effect/platform-node-shared": ["@effect/platform-node-shared@4.0.0-beta.102", "", { "dependencies": { "@types/ws": "^8.18.1", "ws": "^8.21.0" }, "peerDependencies": { "effect": "^4.0.0-beta.102" } }, "sha512-gVd793I72MrkX4dXo7eYtRKfNj0RW4eMRfVEKEJI16h2+mBDCzQ+gqMrog2hSTHQnaIbvbYShNQ4TVGuRCYZeQ=="], + + "@effect/tsgo": ["@effect/tsgo@0.24.3", "", { "optionalDependencies": { "@effect/tsgo-darwin-arm64": "0.24.3", "@effect/tsgo-darwin-x64": "0.24.3", "@effect/tsgo-linux-arm": "0.24.3", "@effect/tsgo-linux-arm64": "0.24.3", "@effect/tsgo-linux-x64": "0.24.3", "@effect/tsgo-win32-arm64": "0.24.3", "@effect/tsgo-win32-x64": "0.24.3" }, "bin": { "effect-tsgo": "dist/effect-tsgo.js" } }, "sha512-WQxKU3MFzWzI38GbE9cf0POkVX4y0xahD8QqDjLfixW1AEValuHNfOQvyKM2MDWOLdGff4hP8VnIOeduwQt66A=="], + + "@effect/tsgo-darwin-arm64": ["@effect/tsgo-darwin-arm64@0.24.3", "", { "os": "darwin", "cpu": "arm64" }, "sha512-eO83D7ZmpAsocA5WJQ8SSAPnAOZ+dEduFIozg9c2SmH36PCHG1Eb++I0UiQml+2LAr4o/QE2wKdWj7LExebjZA=="], + + "@effect/tsgo-darwin-x64": ["@effect/tsgo-darwin-x64@0.24.3", "", { "os": "darwin", "cpu": "x64" }, "sha512-ksX9BdCM8289RRuN0lWhtzuvstH1frxwdxhoW08rrGW3ryER3udEFqEtgtveP0cYvK9gTlV5lXbQPNGktQ2sTw=="], + + "@effect/tsgo-linux-arm": ["@effect/tsgo-linux-arm@0.24.3", "", { "os": "linux", "cpu": "arm" }, "sha512-0TzebGLMNZkHOGSK0w1B4zuvHGlA+Od4LdElWeHHP6iMPEiEWPq17If787+oc+Y67yITPJ9DZcibu69eI9mZhg=="], + + "@effect/tsgo-linux-arm64": ["@effect/tsgo-linux-arm64@0.24.3", "", { "os": "linux", "cpu": "arm64" }, "sha512-CMiThuURi14rT5Ikag5pVnUC3tx4IcmPpRR4WBQkHxAbOf5a1Lo1NUF82zY0azhLW2WszBamTSZP5Tcv4uks2A=="], + + "@effect/tsgo-linux-x64": ["@effect/tsgo-linux-x64@0.24.3", "", { "os": "linux", "cpu": "x64" }, "sha512-R4jl1JWoYEldUU1rtCG1fXWW2ywj/rgY1xaCzK8HSb8fQhQ71WRLLAyAe8ewROuseWQ9Ip+IbbfAeXQOpmpdDA=="], + + "@effect/tsgo-win32-arm64": ["@effect/tsgo-win32-arm64@0.24.3", "", { "os": "win32", "cpu": "arm64" }, "sha512-DOJ4oa8pk8qxEQwKHpJOqtjOfB9iOMuc6Cnb2ZbTG32PBfhqeRYv6YFGhFtgAojXyxfuRP2gCJVpFfjzHJh7nQ=="], + + "@effect/tsgo-win32-x64": ["@effect/tsgo-win32-x64@0.24.3", "", { "os": "win32", "cpu": "x64" }, "sha512-6GpENpPn3mXldmJoM+Z7qi+8fe39xJYB2bLD5jqCFWl14vFp1AeJvYwHYQak8HRm1FiPz72Ninslv8DjHYtZgA=="], + + "@effect/vitest": ["@effect/vitest@4.0.0-beta.102", "", { "peerDependencies": { "effect": "^4.0.0-beta.102", "vitest": "^3.0.0 || ^4.0.0" } }, "sha512-4dipFAYG6imOzrY3zy3BgzCJkbb9xESyzUef0WSx8bsK0/SpITqbJpdwKeOyDWLZ+rimjua8IbTFMAde865pIQ=="], + + "@emnapi/core": ["@emnapi/core@1.11.2", "", { "dependencies": { "@emnapi/wasi-threads": "1.2.2", "tslib": "^2.4.0" } }, "sha512-TC8MkTuZUtcTSiFeuC0ksCh9QIJ5+F21MvZ4Wn4ORfYaFJ/0dsiudv5tVkejgwZlwQ39jL9WWDe2lz8x0WglOA=="], + + "@emnapi/runtime": ["@emnapi/runtime@1.11.2", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-kyOl3X0DuTiT1h2ft8r2fYO8JYtU9a9Xis/zBSiGArNaagCOWx90N1k2wxp18czFDH+OgcWGb5ZP/XMt3dcyPA=="], + + "@emnapi/wasi-threads": ["@emnapi/wasi-threads@1.2.2", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-c95qOXkHdydNKhscBTebqEC1CVAZpyqOfVfBzQ1qgzyl3gfeldUjIggDbIZgDKsHLgnsM+igH7TJ/eAasaVuMA=="], + "@jridgewell/resolve-uri": ["@jridgewell/resolve-uri@3.1.2", "", {}, "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw=="], "@jridgewell/sourcemap-codec": ["@jridgewell/sourcemap-codec@1.5.5", "", {}, "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og=="], "@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.31", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw=="], + "@msgpackr-extract/msgpackr-extract-darwin-arm64": ["@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.4", "", { "os": "darwin", "cpu": "arm64" }, "sha512-LCkGo6JDfaBhgST7UpPWgNgLINpcpabaHfyz5OBx75nUYxBsaEPxjnyNjWpeb/xBup/682QnBfRBy2/LvPutZQ=="], + + "@msgpackr-extract/msgpackr-extract-darwin-x64": ["@msgpackr-extract/msgpackr-extract-darwin-x64@3.0.4", "", { "os": "darwin", "cpu": "x64" }, "sha512-zExlW9zUJKZH/tOtVMttwjKa4Xm/3KcNjnE3dPN92uCktwavMxpgCA3MoJK/DOnTWsQgo224OaST27/mPNAf+w=="], + + "@msgpackr-extract/msgpackr-extract-linux-arm": ["@msgpackr-extract/msgpackr-extract-linux-arm@3.0.4", "", { "os": "linux", "cpu": "arm" }, "sha512-Tg3yX65f5GbtXLkrYEHE5oibZG9epyYWas7FogTTEJeDEF9JlXJzKgXaNhT3UXlTOeA+AfZpYZYZ0uPj7Cfquw=="], + + "@msgpackr-extract/msgpackr-extract-linux-arm64": ["@msgpackr-extract/msgpackr-extract-linux-arm64@3.0.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-dgX0P/9wGPJeHFBG+ZmhgE6bmtMt7NP5CRBGyyktpopdk/mW4POnrpQsSLtKI1dwpc+pPLuXHDh6vvskyQE/sw=="], + + "@msgpackr-extract/msgpackr-extract-linux-x64": ["@msgpackr-extract/msgpackr-extract-linux-x64@3.0.4", "", { "os": "linux", "cpu": "x64" }, "sha512-8TNXMEjJc3QEy7R/x1INhgiU+XakDAFUzBhaz7+Rbrs8NH5UQeHQxxmzsSBJGyV6I1jW79undiQm8tOI+D+8FQ=="], + + "@msgpackr-extract/msgpackr-extract-win32-x64": ["@msgpackr-extract/msgpackr-extract-win32-x64@3.0.4", "", { "os": "win32", "cpu": "x64" }, "sha512-CmCXPQrkbwExx3j946/PtHWHbYJiCRBRDl4BlkRQcJB/YOwQxJRTpoo7aTsortjgoJ1x7opzTSxn7C+ASSLVjQ=="], + + "@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.2.0", "", { "dependencies": { "@tybys/wasm-util": "^0.10.3" }, "peerDependencies": { "@emnapi/core": "^2.0.0-alpha.3", "@emnapi/runtime": "^2.0.0-alpha.3" } }, "sha512-kDoONqMa+VnZ4vvvu/ZUurpJ4gkZU57e7g69qpNgWhYcZFPUHZM2CEMKm+cG6ufDVALbjMvfmMjFVqaK7uEMnA=="], + + "@oxc-parser/binding-android-arm-eabi": ["@oxc-parser/binding-android-arm-eabi@0.140.0", "", { "os": "android", "cpu": "arm" }, "sha512-ZfjDZ422mo7eo3b3VltqNsV9kmv1qt/sPEAMSl64iOSwhVfd0eIZ9LB79Mbs1xYXJnk7WSROwzBCKDIiVxPTvQ=="], + + "@oxc-parser/binding-android-arm64": ["@oxc-parser/binding-android-arm64@0.140.0", "", { "os": "android", "cpu": "arm64" }, "sha512-Ia8jSvikUX6Sf+Ht+KOCUF/k1HpR0VlmqIYymubmWDebOEGtsyliHDR6JxsZ4IX3/c/GbrB1uh09aVGQv/LQmQ=="], + + "@oxc-parser/binding-darwin-arm64": ["@oxc-parser/binding-darwin-arm64@0.140.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-G6VK0nK61pH0d0mBjUqSZbVxGqqO5uzeginLDQj+gOO6ObfJjXRwgkD/ol0w1INcnFeAb6YGGO7qc3ueGHaycQ=="], + + "@oxc-parser/binding-darwin-x64": ["@oxc-parser/binding-darwin-x64@0.140.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-HazBOuZzd2pO1C2uMmp8Gv7mhzMHqKSKDS1OZfcLEvpIcgA+48J92HEtNanVHDIzRD9PRPCV6aS6fkZIWOVl8Q=="], + + "@oxc-parser/binding-freebsd-x64": ["@oxc-parser/binding-freebsd-x64@0.140.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-9hSUU+HmTUyOe4JzMHxNGgLWNY7rrO+6ShicZwImNJacEAACDMIkuEQQkvXSL+WJN50jaNtLYJv8s4OcBdpyUQ=="], + + "@oxc-parser/binding-linux-arm-gnueabihf": ["@oxc-parser/binding-linux-arm-gnueabihf@0.140.0", "", { "os": "linux", "cpu": "arm" }, "sha512-RAEuQsYtS0KcDFqN0ABTjyyNlokS91JeuDuoW9tEbG0JTbRNXnpQUdbYc/16JoA6Z/2ALbNrE3KmxtqDiuIjCQ=="], + + "@oxc-parser/binding-linux-arm-musleabihf": ["@oxc-parser/binding-linux-arm-musleabihf@0.140.0", "", { "os": "linux", "cpu": "arm" }, "sha512-c4CkHvPvqfojouredJ0w3e6+jiBq0SbFyhH61kr/zPb/7XsaYTNKQ54vmlSsopfdQbNDX40ZeK9Abs2Qet6wcw=="], + + "@oxc-parser/binding-linux-arm64-gnu": ["@oxc-parser/binding-linux-arm64-gnu@0.140.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-yrjmLj8ixPB25yqvPGr28meGjb+keed7m1GqqY/0uqkhZIoT4t9zmfwUgFEtC33C7dtE+UQ7TU0IaVxf97SWJg=="], + + "@oxc-parser/binding-linux-arm64-musl": ["@oxc-parser/binding-linux-arm64-musl@0.140.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-ggGMQTN8Agwxp2WiLMpdY671dt0qTDJWiWlJeig3HnUwTnerRl0J2JdGVghWBeDcss2D9S2V2Js6dZHEiVabVA=="], + + "@oxc-parser/binding-linux-ppc64-gnu": ["@oxc-parser/binding-linux-ppc64-gnu@0.140.0", "", { "os": "linux", "cpu": "ppc64" }, "sha512-IgTs8xYAFgAUGNmR65tIqjlJ8vKgrfXzC515e9goSdfMyKQV4aJpd2pUUudU4u51G64H0/DSEJEXKOraxm9ZCA=="], + + "@oxc-parser/binding-linux-riscv64-gnu": ["@oxc-parser/binding-linux-riscv64-gnu@0.140.0", "", { "os": "linux", "cpu": "none" }, "sha512-A1x+PMWZmSGaFVOx2YeNTFau8uD+QO14/vLP4GrcuvUPs3+nBkUOjy9Lus86ftHsDojjYMbvBelmKc3F7Rv08g=="], + + "@oxc-parser/binding-linux-riscv64-musl": ["@oxc-parser/binding-linux-riscv64-musl@0.140.0", "", { "os": "linux", "cpu": "none" }, "sha512-zBqpfRo2myWPrPo5xUjeZqlnPXPXsX8BcWtWff66/eGRQdbPjhzPgXa/F+AtxT2afUViPxbuDlwscMKzQ5tg+g=="], + + "@oxc-parser/binding-linux-s390x-gnu": ["@oxc-parser/binding-linux-s390x-gnu@0.140.0", "", { "os": "linux", "cpu": "s390x" }, "sha512-2M1DPm/8w9I//YzFlFC9qXw+r2tJFh5CYwRlYTq2vUJQS7qoQftEDeCZ8EnN7KHtvSiXvYj8mZI5pR7DpXmcEw=="], + + "@oxc-parser/binding-linux-x64-gnu": ["@oxc-parser/binding-linux-x64-gnu@0.140.0", "", { "os": "linux", "cpu": "x64" }, "sha512-8aRDbZ/U/jO8N7go1MO72jtbpb4uswV8d7vOkMvt/BPgZiyEYvl1VIWK4ESxZZhnJ4tqwVldgX7dNiP/eB1Jdg=="], + + "@oxc-parser/binding-linux-x64-musl": ["@oxc-parser/binding-linux-x64-musl@0.140.0", "", { "os": "linux", "cpu": "x64" }, "sha512-xRqpeI8U2sQQS1W5BMWRyMTxtagkuLG2dEWruet5lFsWHTvBth11/TpSaJatHdqVVwHN0q3uuoS9zRsGinq8hg=="], + + "@oxc-parser/binding-openharmony-arm64": ["@oxc-parser/binding-openharmony-arm64@0.140.0", "", { "os": "none", "cpu": "arm64" }, "sha512-GbGRe26MqAKciFRvXeHNQJ6VAHYs9R4miP89sEAncysM3n+f4lnyLWgsa9kklJNpfnxdq2yRoNYHFqwBckVimw=="], + + "@oxc-parser/binding-wasm32-wasi": ["@oxc-parser/binding-wasm32-wasi@0.140.0", "", { "dependencies": { "@emnapi/core": "1.11.2", "@emnapi/runtime": "1.11.2", "@napi-rs/wasm-runtime": "^1.1.6" }, "cpu": "none" }, "sha512-vFiC1hqys+hkX1GnQkIoiTQJNiUm43Z0lO35ETKXTw0YtpW7+cN58YRRXFAQQ+TgpkIi3lrhcxdlnqz+Oi3ptQ=="], + + "@oxc-parser/binding-win32-arm64-msvc": ["@oxc-parser/binding-win32-arm64-msvc@0.140.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-fGSQldwEYKhM+H8uLt76Op8hh5+FYaR6lvvQ1Txw3Mhn86DyQXLcI0fi1EkFlTK7F+46OCk/j0AJMzZQm6g5Xg=="], + + "@oxc-parser/binding-win32-ia32-msvc": ["@oxc-parser/binding-win32-ia32-msvc@0.140.0", "", { "os": "win32", "cpu": "ia32" }, "sha512-sDS2Bai+g3ZWYwfZqmosiSuFDBcVnZ3Ta6pszzsiJoLMqsJEWKcxXXbGa7b7yXr++W2lQNPb3ZRJ8czseqL7RA=="], + + "@oxc-parser/binding-win32-x64-msvc": ["@oxc-parser/binding-win32-x64-msvc@0.140.0", "", { "os": "win32", "cpu": "x64" }, "sha512-kHbE1zWyb5OQgJA6/5P4WjiuB01sYdQwtZnSSyE58FQEXDAMnyeeq4vj7KgN75i5SlBzOs8A5MrtlD3gOlDKqQ=="], + "@oxc-project/runtime": ["@oxc-project/runtime@0.138.0", "", {}, "sha512-yHhoXsN8tYxgdJCdD91PbySNjEEaBX/tH2OQRDXJpsQv5b184oC4/qVbU7qlblvfil/JP15Lh2HW7+HN5DS90Q=="], "@oxc-project/types": ["@oxc-project/types@0.138.0", "", {}, "sha512-1a7ZKmrRTCoN1XMZ4L0PyyqrMnrNlLyPuOkdSX2MZg7IiIGRUyurNhAm73ptDOraoBcIordsIGKNPKUzy3ZmfA=="], + "@oxc-resolver/binding-android-arm-eabi": ["@oxc-resolver/binding-android-arm-eabi@11.24.2", "", { "os": "android", "cpu": "arm" }, "sha512-y09e0L0SRI2OA2tUIrjBgoV3eH5hvUKXNkJqXmNo5V2WxIjyC7I7aJfRLMEVpA8yi95f90gFDvO0VMgrDw+vwA=="], + + "@oxc-resolver/binding-android-arm64": ["@oxc-resolver/binding-android-arm64@11.24.2", "", { "os": "android", "cpu": "arm64" }, "sha512-cl4icWaZFnLdg8m6qtnh5rBMuGbxc/ptStFHLeCNwr+2cZjkjNwQu/jYRS0CHlnPecOJMpuS5M6/BH+0J/YkEg=="], + + "@oxc-resolver/binding-darwin-arm64": ["@oxc-resolver/binding-darwin-arm64@11.24.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-At29QEMF6HajbQvgY8K6OXnHD1x9rad74xBEfmCB6ZqCGsdq75aK7tOYcTbOanMy8qdIBrfL3SMr3p/lfSlb9w=="], + + "@oxc-resolver/binding-darwin-x64": ["@oxc-resolver/binding-darwin-x64@11.24.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-A5Kqr1EUj4oIL5CF4WRssq/o5P0Y11cwoFouMRmQ7YnC/A8V93nv1nb7aSU8HwcgmXropjLNkVTl4MN87cu28Q=="], + + "@oxc-resolver/binding-freebsd-x64": ["@oxc-resolver/binding-freebsd-x64@11.24.2", "", { "os": "freebsd", "cpu": "x64" }, "sha512-R5xkRBRRz7ceH/P5Jrc6G7FmdUdgpLYyESFAUDVTNQ9K0sGPxcp4ljiwEwEqsvNcQ4sYbMRrWcHHBCu7ksAJVw=="], + + "@oxc-resolver/binding-linux-arm-gnueabihf": ["@oxc-resolver/binding-linux-arm-gnueabihf@11.24.2", "", { "os": "linux", "cpu": "arm" }, "sha512-k/RuYL4L/R58IBn3wT5ma3Wh4k62bp1eYCFRWCmMsasUOqL+H6sW0VGFadEzKWXFFlz+2uIMoeMk9ySSZJHgbg=="], + + "@oxc-resolver/binding-linux-arm-musleabihf": ["@oxc-resolver/binding-linux-arm-musleabihf@11.24.2", "", { "os": "linux", "cpu": "arm" }, "sha512-bnHAak3ujYfH5pKk4NieFNbvYvernfoQDgwLddbZ3OtMYrem87/qjlA+u+aKG0oZcqSLGCful/6/CEA+aeAgaA=="], + + "@oxc-resolver/binding-linux-arm64-gnu": ["@oxc-resolver/binding-linux-arm64-gnu@11.24.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-vDT3KHgzYp47gmtNOqL2VNhCyl5Zv643eyxm//A68J8DeUGXrvD1pZFiaT4jSfe+RInfnn1R2yVHye4enx6RnA=="], + + "@oxc-resolver/binding-linux-arm64-musl": ["@oxc-resolver/binding-linux-arm64-musl@11.24.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-+kMlQvbzfyEYtu5FcjE4p+ttBLpKW4d/AsAsuE69BxV6V4twZJeIQZFfD8gh/wqglY0MkPSezWXQH0jBV13MUw=="], + + "@oxc-resolver/binding-linux-ppc64-gnu": ["@oxc-resolver/binding-linux-ppc64-gnu@11.24.2", "", { "os": "linux", "cpu": "ppc64" }, "sha512-shjfMhmZ3gq9fv/w7bi3PnZlgOPG+2QAOFf0BJF0EgBSIGZ6PMLN2zbGEblTUYB/NKVDRyYhE2ff3dJ1QqNPkA=="], + + "@oxc-resolver/binding-linux-riscv64-gnu": ["@oxc-resolver/binding-linux-riscv64-gnu@11.24.2", "", { "os": "linux", "cpu": "none" }, "sha512-zGelwFR5oRo+b69k8Lrzun86DyUHzfKN6cnjbR9l7Z7NIRznOE/2ZvPa1IUKqAL2PzAXOdwkfVqNvO1H2RlpAw=="], + + "@oxc-resolver/binding-linux-riscv64-musl": ["@oxc-resolver/binding-linux-riscv64-musl@11.24.2", "", { "os": "linux", "cpu": "none" }, "sha512-qxZ1SWCXJY0eyhAlP6Lmo9F2Nrtx7EkYj9oCgL8apDPCwXwCEDA2U697bbT81JIc2IrVjxO4KX6WU2N+oN9Z4w=="], + + "@oxc-resolver/binding-linux-s390x-gnu": ["@oxc-resolver/binding-linux-s390x-gnu@11.24.2", "", { "os": "linux", "cpu": "s390x" }, "sha512-sGCecF3cx2DFlH4t/z7ApnOnXqN48p5p5mlHDEnHTAukQa2P+qMVE4CwyWE9W+q/m3QJ7kKfGrIjax31f44oFQ=="], + + "@oxc-resolver/binding-linux-x64-gnu": ["@oxc-resolver/binding-linux-x64-gnu@11.24.2", "", { "os": "linux", "cpu": "x64" }, "sha512-k/VlMMcSzMlahb3/fENM4rTlsJ0s3fFROA0KXPBmKggqmTSaE383sl8F3KCOXPLmVsYfW6hCitMhXCEtNeZxxg=="], + + "@oxc-resolver/binding-linux-x64-musl": ["@oxc-resolver/binding-linux-x64-musl@11.24.2", "", { "os": "linux", "cpu": "x64" }, "sha512-8hbnZyNi97b/8wapYaIF9+t9GmZKBW2vunaOc3h9HGJptH7b7XpvZqOTBSm/MpTjr7H497BlgOaSfLUdhmy2bw=="], + + "@oxc-resolver/binding-openharmony-arm64": ["@oxc-resolver/binding-openharmony-arm64@11.24.2", "", { "os": "none", "cpu": "arm64" }, "sha512-MvyGik3a6pVgZ0t/kWlbmFxFLmXQJwgLsY2eYFHLpy0wGwRbfzeIGgDwQ3kXqE30z+kSXennRkCrT7TUvkptNg=="], + + "@oxc-resolver/binding-wasm32-wasi": ["@oxc-resolver/binding-wasm32-wasi@11.24.2", "", { "dependencies": { "@emnapi/core": "1.11.2", "@emnapi/runtime": "1.11.2", "@napi-rs/wasm-runtime": "^1.1.6" }, "cpu": "none" }, "sha512-vHcssMPwO08RTvj/c0iOBz90attxyG3wQJ0dTcyEQK43LRpcdLWZlV5feBhv6Isn6ahbQIzHbCgfa81+RiML0Q=="], + + "@oxc-resolver/binding-win32-arm64-msvc": ["@oxc-resolver/binding-win32-arm64-msvc@11.24.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-uokJqro2iBqkFvJdKQLP7d8/BUmFwESQFVmIJUQKj1Xn1a/LysJoe1vmeECLF5b3jsV8CAL5sEMJXX6SdK9Nhg=="], + + "@oxc-resolver/binding-win32-x64-msvc": ["@oxc-resolver/binding-win32-x64-msvc@11.24.2", "", { "os": "win32", "cpu": "x64" }, "sha512-UqGPmo56KDfLlfXFAFIrNflHT8tFxWGEivWg3Zeyp4Uy2NlKN1FGPr6/BxcLGG3+kZ6Wp14g5Uj+n71boqZfiw=="], + "@oxfmt/binding-android-arm-eabi": ["@oxfmt/binding-android-arm-eabi@0.57.0", "", { "os": "android", "cpu": "arm" }, "sha512-qVBsEO+KugOsCmUHcO8iqNnqc65p7PCKpCs8M66mPZ+Ri+CWbcpoQOEJBg2OTu03+0qu++NK1jj6IzvQVs0Sig=="], "@oxfmt/binding-android-arm64": ["@oxfmt/binding-android-arm64@0.57.0", "", { "os": "android", "cpu": "arm64" }, "sha512-mp6PibWbao3aizijcheOeHQaYEhcUAt8pwLniYbtLfHxL/psFF0BykAwCj+s3c6qIpa8yN8keZICWrqtZ70w8g=="], @@ -155,6 +281,8 @@ "@testing-library/user-event": ["@testing-library/user-event@14.6.1", "", { "peerDependencies": { "@testing-library/dom": ">=7.21.4" } }, "sha512-vq7fv0rnt+QTXgPxr5Hjc210p6YKq2kmdziLgnsZGgLJ9e6VAShx1pACLuRjd/AS/sr7phAR58OIIpf0LlmQNw=="], + "@tybys/wasm-util": ["@tybys/wasm-util@0.10.3", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-F3fo1MYrRJYL3zER0OUOmkutjr1Vp23m7OsSgp7nq4SP6OqX6C/56XFIPAl5bt3zaBRjmW7SGz3u/6LwFpYcOg=="], + "@types/aria-query": ["@types/aria-query@5.0.4", "", {}, "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw=="], "@types/bun": ["@types/bun@1.3.14", "", { "dependencies": { "bun-types": "1.3.14" } }, "sha512-h1hFqFVcvAvD9j9K7ZW7vd82aSA+rTdznZa+5bwvCwqSB1jmmfLcbIWhOLx1/+boy/xmjgCs/OMUL8hRJSmnPw=="], @@ -169,6 +297,8 @@ "@types/react": ["@types/react@19.2.17", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-MXfmqaVPEVgkBT/aY0aGCkRWWtByiYQXo3xdQ8r5RzuFrPiRn8Gar2tQdXSUQ2GKV3bkXckek89V8wQBY2Q/Aw=="], + "@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="], + "@typescript/native": ["typescript@7.0.2", "", { "optionalDependencies": { "@typescript/typescript-aix-ppc64": "7.0.2", "@typescript/typescript-darwin-arm64": "7.0.2", "@typescript/typescript-darwin-x64": "7.0.2", "@typescript/typescript-freebsd-arm64": "7.0.2", "@typescript/typescript-freebsd-x64": "7.0.2", "@typescript/typescript-linux-arm": "7.0.2", "@typescript/typescript-linux-arm64": "7.0.2", "@typescript/typescript-linux-loong64": "7.0.2", "@typescript/typescript-linux-mips64el": "7.0.2", "@typescript/typescript-linux-ppc64": "7.0.2", "@typescript/typescript-linux-riscv64": "7.0.2", "@typescript/typescript-linux-s390x": "7.0.2", "@typescript/typescript-linux-x64": "7.0.2", "@typescript/typescript-netbsd-arm64": "7.0.2", "@typescript/typescript-netbsd-x64": "7.0.2", "@typescript/typescript-openbsd-arm64": "7.0.2", "@typescript/typescript-openbsd-x64": "7.0.2", "@typescript/typescript-sunos-x64": "7.0.2", "@typescript/typescript-win32-arm64": "7.0.2", "@typescript/typescript-win32-x64": "7.0.2" }, "bin": { "tsc": "bin/tsc" } }, "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA=="], "@typescript/typescript-aix-ppc64": ["@typescript/typescript-aix-ppc64@7.0.2", "", { "os": "aix", "cpu": "ppc64" }, "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ=="], @@ -293,6 +423,8 @@ "dom-accessibility-api": ["dom-accessibility-api@0.5.16", "", {}, "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg=="], + "effect": ["effect@4.0.0-beta.102", "", { "dependencies": { "@standard-schema/spec": "^1.1.0", "fast-check": "^4.9.0", "find-my-way-ts": "^0.1.6", "ini": "^7.0.0", "kubernetes-types": "^1.30.0", "msgpackr": "^2.0.4", "multipasta": "^0.2.8", "toml": "^4.1.2", "uuid": "^14.0.1", "yaml": "^2.9.0" } }, "sha512-z8Y+Q76Hh/kjLFZrXu8tGn6e+tDsg45R+UHhxd190pXxD53OGwf/G/zDxXTkse4HJ5mobNZfitLfUCp4fMvu6w=="], + "environment": ["environment@1.1.0", "", {}, "sha512-xUtoPkMggbz0MPyPiIWr1Kp4aeWJjDZ6SMvURhimjdZgsRuDplF5/s9hcgGhyXMhs+6vpnuoiZ2kFiu3FMnS8Q=="], "es-module-lexer": ["es-module-lexer@2.2.0", "", {}, "sha512-3lGxdTXCLfe1MYfTz1y2ksAAUM4NAOP6rPEjxGJVKO7TZ5+tvHCaQWGpC4Y3IXvW3ece0Cz1cIP4FWBxOnGCTQ=="], @@ -305,18 +437,30 @@ "expect-type": ["expect-type@1.4.0", "", {}, "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA=="], + "fast-check": ["fast-check@4.9.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-7ms6T7SybUev/PQITciI0yLM2pOSFy5zpG8Ty7tQofcVaQUvrMXp6CBwqF6fThLCLOrfBtuHAtwq6Yu4XPCllg=="], + + "fd-package-json": ["fd-package-json@2.0.0", "", { "dependencies": { "walk-up-path": "^4.0.0" } }, "sha512-jKmm9YtsNXN789RS/0mSzOC1NUq9mkVd65vbSSVsKdjGvYXBuE4oWe2QOEoFeRmJg+lPuZxpmrfFclNhoRMneQ=="], + "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], + "find-my-way-ts": ["find-my-way-ts@0.1.6", "", {}, "sha512-a85L9ZoXtNAey3Y6Z+eBWW658kO/MwR7zIafkIUPUMf3isZG0NCs2pjW2wtjxAKuJPxMAsHUIP4ZPGv0o5gyTA=="], + + "formatly": ["formatly@0.3.0", "", { "dependencies": { "fd-package-json": "^2.0.0" }, "bin": { "formatly": "bin/index.mjs" } }, "sha512-9XNj/o4wrRFyhSMJOvsuyMwy8aUfBaZ1VrqHVfohyXf0Sw0e+yfKG+xZaY3arGCOMdwFsqObtzVOc1gU9KiT9w=="], + "fsevents": ["fsevents@2.3.3", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="], "get-east-asian-width": ["get-east-asian-width@1.6.0", "", {}, "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA=="], + "get-tsconfig": ["get-tsconfig@4.14.0", "", { "dependencies": { "resolve-pkg-maps": "^1.0.0" } }, "sha512-yTb+8DXzDREzgvYmh6s9vHsSVCHeC0G3PI5bEXNBHtmshPnO+S5O7qgLEOn0I5QvMy6kpZN8K1NKGyilLb93wA=="], + "has-flag": ["has-flag@4.0.0", "", {}, "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="], "html-escaper": ["html-escaper@2.0.2", "", {}, "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg=="], "indent-string": ["indent-string@5.0.0", "", {}, "sha512-m6FAo/spmsW2Ab2fU35JTYwtOKa2yAwXSwgjSv1TJzh4Mh7mC3lzAOVLBprb72XsTrgkEIsl7YrFNAiDiRhIGg=="], + "ini": ["ini@7.0.0", "", {}, "sha512-ifK0CgjALofS5bkrcTy4RaQ9Vx2Knf/eLeIO+NaswQEpH1UblrtTSCIvN71qQDMq0PeQ/SSPojvEJp9vvvfr+w=="], + "ink": ["ink@7.1.0", "", { "dependencies": { "@alcalzone/ansi-tokenize": "^0.3.0", "ansi-escapes": "^7.3.0", "ansi-styles": "^6.2.3", "auto-bind": "^5.0.1", "chalk": "^5.6.2", "cli-boxes": "^4.0.1", "cli-cursor": "^4.0.0", "cli-truncate": "^6.0.0", "code-excerpt": "^4.0.0", "es-toolkit": "^1.45.1", "indent-string": "^5.0.0", "is-in-ci": "^2.0.0", "patch-console": "^2.0.0", "react-reconciler": "^0.33.0", "scheduler": "^0.27.0", "signal-exit": "^3.0.7", "slice-ansi": "^9.0.0", "stack-utils": "^2.0.6", "string-width": "^8.2.0", "terminal-size": "^4.0.1", "type-fest": "^5.5.0", "widest-line": "^6.0.0", "wrap-ansi": "^10.0.0", "ws": "^8.20.0", "yoga-layout": "~3.2.1" }, "peerDependencies": { "@types/react": ">=19.2.0", "react": ">=19.2.0", "react-devtools-core": ">=6.1.2" }, "optionalPeers": ["@types/react", "react-devtools-core"] }, "sha512-VWE6/yeLtFCJBNLflyI2OSylyXK1Rc24LuXup8Qt+icwkmmycFNdbn8IkSp6Frc0h1iA0NOvvi1ajW44U/w3Qg=="], "ink-testing-library": ["ink-testing-library@4.0.0", "", { "peerDependencies": { "@types/react": ">=18.0.0" }, "optionalPeers": ["@types/react"] }, "sha512-yF92kj3pmBvk7oKbSq5vEALO//o7Z9Ck/OaLNlkzXNeYdwfpxMQkSowGTFUCS5MSu9bWfSZMewGpp7bFc66D7Q=="], @@ -331,8 +475,14 @@ "istanbul-reports": ["istanbul-reports@3.2.0", "", { "dependencies": { "html-escaper": "^2.0.0", "istanbul-lib-report": "^3.0.0" } }, "sha512-HGYWWS/ehqTV3xN10i23tkPkpH46MLCIMFNCaaKNavAXTF1RkqxawEPtnjnGZ6XKSInBKkiOA5BKS+aZiY3AvA=="], + "jiti": ["jiti@2.7.0", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ=="], + "js-tokens": ["js-tokens@10.0.0", "", {}, "sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q=="], + "knip": ["knip@6.29.0", "", { "dependencies": { "fdir": "^6.5.0", "formatly": "^0.3.0", "get-tsconfig": "4.14.0", "jiti": "^2.7.0", "oxc-parser": "^0.140.0", "oxc-resolver": "11.24.2", "picomatch": "^4.0.5", "smol-toml": "^1.7.0", "strip-json-comments": "5.0.3", "tinyglobby": "^0.2.17", "unbash": "^4.0.3", "yaml": "^2.9.0", "zod": "^4.4.3" }, "bin": { "knip": "bin/knip.js", "knip-bun": "bin/knip-bun.js" } }, "sha512-A3kXqSBky1tWBAqiU9srdtu0Larhzkuyor0aD/gg+ToiqyBncCCs2Q60sLsxmcKhV0OsKss9LV0hMPpwLv711Q=="], + + "kubernetes-types": ["kubernetes-types@1.30.0", "", {}, "sha512-Dew1okvhM/SQcIa2rcgujNndZwU8VnSapDgdxlYoB84ZlpAD43U6KLAFqYo17ykSFGHNPrg0qry0bP+GJd9v7Q=="], + "lightningcss": ["lightningcss@1.32.0", "", { "dependencies": { "detect-libc": "^2.0.3" }, "optionalDependencies": { "lightningcss-android-arm64": "1.32.0", "lightningcss-darwin-arm64": "1.32.0", "lightningcss-darwin-x64": "1.32.0", "lightningcss-freebsd-x64": "1.32.0", "lightningcss-linux-arm-gnueabihf": "1.32.0", "lightningcss-linux-arm64-gnu": "1.32.0", "lightningcss-linux-arm64-musl": "1.32.0", "lightningcss-linux-x64-gnu": "1.32.0", "lightningcss-linux-x64-musl": "1.32.0", "lightningcss-win32-arm64-msvc": "1.32.0", "lightningcss-win32-x64-msvc": "1.32.0" } }, "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ=="], "lightningcss-android-arm64": ["lightningcss-android-arm64@1.32.0", "", { "os": "android", "cpu": "arm64" }, "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg=="], @@ -369,14 +519,26 @@ "mrmime": ["mrmime@2.0.1", "", {}, "sha512-Y3wQdFg2Va6etvQ5I82yUhGdsKrcYox6p7FfL1LbK2J4V01F9TGlepTIhnK24t7koZibmg82KGglhA1XK5IsLQ=="], + "msgpackr": ["msgpackr@2.0.4", "", { "optionalDependencies": { "msgpackr-extract": "^3.0.4" } }, "sha512-o1C5KRmuRt+apqMr1HuGSqWStZoRBUpEsCsl15uM9VdAF1qHLtvMOU2En747EnTyEl6c4pzPewRMFF31s1CNbA=="], + + "msgpackr-extract": ["msgpackr-extract@3.0.4", "", { "dependencies": { "node-gyp-build-optional-packages": "5.2.2" }, "optionalDependencies": { "@msgpackr-extract/msgpackr-extract-darwin-arm64": "3.0.4", "@msgpackr-extract/msgpackr-extract-darwin-x64": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-arm": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-arm64": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-x64": "3.0.4", "@msgpackr-extract/msgpackr-extract-win32-x64": "3.0.4" }, "bin": { "download-msgpackr-prebuilds": "bin/download-prebuilds.js" } }, "sha512-4kmO/MdyUIkLIvTPr8VHLil4AtoKIoniWPIEk5+CDy0xnWC84azhSFmuJ7PxZdsYtiP5kEeQsORAVIeMgxT+Hw=="], + + "multipasta": ["multipasta@0.2.8", "", {}, "sha512-ZPWuMKyv0cSO29f7hozp+k6+crZbQijV8ipMvxNxRf2SwtYGTX1ZX89Kd20VV4H9Znonx+EQn+iy1wGQsJ+b+Q=="], + "nanoid": ["nanoid@3.3.15", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA=="], + "node-gyp-build-optional-packages": ["node-gyp-build-optional-packages@5.2.2", "", { "dependencies": { "detect-libc": "^2.0.1" }, "bin": { "node-gyp-build-optional-packages": "bin.js", "node-gyp-build-optional-packages-optional": "optional.js", "node-gyp-build-optional-packages-test": "build-test.js" } }, "sha512-s+w+rBWnpTMwSFbaE0UXsRlg7hU4FjekKU4eyAih5T8nJuNZT1nNsskXpxmeqSK9UzkBl6UgRlnKc8hz8IEqOw=="], + "obug": ["obug@2.1.3", "", {}, "sha512-9miFgM2OFba7hB+pRgvtV84pYTBaoTHohvmIgiRt6dRIzbwEOIaNaP+dIlGs2fNFoB0SeISs0Jz5WFVRid6Xyg=="], "on-exit-leak-free": ["on-exit-leak-free@2.1.2", "", {}, "sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA=="], "onetime": ["onetime@5.1.2", "", { "dependencies": { "mimic-fn": "^2.1.0" } }, "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg=="], + "oxc-parser": ["oxc-parser@0.140.0", "", { "dependencies": { "@oxc-project/types": "^0.140.0" }, "optionalDependencies": { "@oxc-parser/binding-android-arm-eabi": "0.140.0", "@oxc-parser/binding-android-arm64": "0.140.0", "@oxc-parser/binding-darwin-arm64": "0.140.0", "@oxc-parser/binding-darwin-x64": "0.140.0", "@oxc-parser/binding-freebsd-x64": "0.140.0", "@oxc-parser/binding-linux-arm-gnueabihf": "0.140.0", "@oxc-parser/binding-linux-arm-musleabihf": "0.140.0", "@oxc-parser/binding-linux-arm64-gnu": "0.140.0", "@oxc-parser/binding-linux-arm64-musl": "0.140.0", "@oxc-parser/binding-linux-ppc64-gnu": "0.140.0", "@oxc-parser/binding-linux-riscv64-gnu": "0.140.0", "@oxc-parser/binding-linux-riscv64-musl": "0.140.0", "@oxc-parser/binding-linux-s390x-gnu": "0.140.0", "@oxc-parser/binding-linux-x64-gnu": "0.140.0", "@oxc-parser/binding-linux-x64-musl": "0.140.0", "@oxc-parser/binding-openharmony-arm64": "0.140.0", "@oxc-parser/binding-wasm32-wasi": "0.140.0", "@oxc-parser/binding-win32-arm64-msvc": "0.140.0", "@oxc-parser/binding-win32-ia32-msvc": "0.140.0", "@oxc-parser/binding-win32-x64-msvc": "0.140.0" } }, "sha512-h6QFWd6lBMfjESqgQ27GjzrSDb0qbznp7VDQqp2zvgsrWut4vcchyMIzOVXvGQ2GMZgKw9RWrFNWv9WqGL0p7Q=="], + + "oxc-resolver": ["oxc-resolver@11.24.2", "", { "optionalDependencies": { "@oxc-resolver/binding-android-arm-eabi": "11.24.2", "@oxc-resolver/binding-android-arm64": "11.24.2", "@oxc-resolver/binding-darwin-arm64": "11.24.2", "@oxc-resolver/binding-darwin-x64": "11.24.2", "@oxc-resolver/binding-freebsd-x64": "11.24.2", "@oxc-resolver/binding-linux-arm-gnueabihf": "11.24.2", "@oxc-resolver/binding-linux-arm-musleabihf": "11.24.2", "@oxc-resolver/binding-linux-arm64-gnu": "11.24.2", "@oxc-resolver/binding-linux-arm64-musl": "11.24.2", "@oxc-resolver/binding-linux-ppc64-gnu": "11.24.2", "@oxc-resolver/binding-linux-riscv64-gnu": "11.24.2", "@oxc-resolver/binding-linux-riscv64-musl": "11.24.2", "@oxc-resolver/binding-linux-s390x-gnu": "11.24.2", "@oxc-resolver/binding-linux-x64-gnu": "11.24.2", "@oxc-resolver/binding-linux-x64-musl": "11.24.2", "@oxc-resolver/binding-openharmony-arm64": "11.24.2", "@oxc-resolver/binding-wasm32-wasi": "11.24.2", "@oxc-resolver/binding-win32-arm64-msvc": "11.24.2", "@oxc-resolver/binding-win32-x64-msvc": "11.24.2" } }, "sha512-FY91FiDBj7ls5MsFS9jN3tjz2o0/zsdSsymlakySaBwVJZorHhkWyICLZMKxlu1R9vYo+sd3z1jwb4J8x7bNDw=="], + "oxfmt": ["oxfmt@0.57.0", "", { "dependencies": { "tinypool": "2.1.0" }, "optionalDependencies": { "@oxfmt/binding-android-arm-eabi": "0.57.0", "@oxfmt/binding-android-arm64": "0.57.0", "@oxfmt/binding-darwin-arm64": "0.57.0", "@oxfmt/binding-darwin-x64": "0.57.0", "@oxfmt/binding-freebsd-x64": "0.57.0", "@oxfmt/binding-linux-arm-gnueabihf": "0.57.0", "@oxfmt/binding-linux-arm-musleabihf": "0.57.0", "@oxfmt/binding-linux-arm64-gnu": "0.57.0", "@oxfmt/binding-linux-arm64-musl": "0.57.0", "@oxfmt/binding-linux-ppc64-gnu": "0.57.0", "@oxfmt/binding-linux-riscv64-gnu": "0.57.0", "@oxfmt/binding-linux-riscv64-musl": "0.57.0", "@oxfmt/binding-linux-s390x-gnu": "0.57.0", "@oxfmt/binding-linux-x64-gnu": "0.57.0", "@oxfmt/binding-linux-x64-musl": "0.57.0", "@oxfmt/binding-openharmony-arm64": "0.57.0", "@oxfmt/binding-win32-arm64-msvc": "0.57.0", "@oxfmt/binding-win32-ia32-msvc": "0.57.0", "@oxfmt/binding-win32-x64-msvc": "0.57.0" }, "peerDependencies": { "svelte": "^5.0.0", "vite-plus": "*" }, "optionalPeers": ["svelte", "vite-plus"], "bin": { "oxfmt": "bin/oxfmt" } }, "sha512-ZB7Bi+rGDSqmVIo9jwcLyFgjxXvQhDdU+jx+ZrVy6VRiVXK2+CHc4hO3J4dUQjHe7V0ymHB+MDuv5z+NhK07HA=="], "oxlint": ["oxlint@1.72.0", "", { "optionalDependencies": { "@oxlint/binding-android-arm-eabi": "1.72.0", "@oxlint/binding-android-arm64": "1.72.0", "@oxlint/binding-darwin-arm64": "1.72.0", "@oxlint/binding-darwin-x64": "1.72.0", "@oxlint/binding-freebsd-x64": "1.72.0", "@oxlint/binding-linux-arm-gnueabihf": "1.72.0", "@oxlint/binding-linux-arm-musleabihf": "1.72.0", "@oxlint/binding-linux-arm64-gnu": "1.72.0", "@oxlint/binding-linux-arm64-musl": "1.72.0", "@oxlint/binding-linux-ppc64-gnu": "1.72.0", "@oxlint/binding-linux-riscv64-gnu": "1.72.0", "@oxlint/binding-linux-riscv64-musl": "1.72.0", "@oxlint/binding-linux-s390x-gnu": "1.72.0", "@oxlint/binding-linux-x64-gnu": "1.72.0", "@oxlint/binding-linux-x64-musl": "1.72.0", "@oxlint/binding-openharmony-arm64": "1.72.0", "@oxlint/binding-win32-arm64-msvc": "1.72.0", "@oxlint/binding-win32-ia32-msvc": "1.72.0", "@oxlint/binding-win32-x64-msvc": "1.72.0" }, "peerDependencies": { "oxlint-tsgolint": ">=0.22.1", "vite-plus": "*" }, "optionalPeers": ["oxlint-tsgolint", "vite-plus"], "bin": { "oxlint": "bin/oxlint" } }, "sha512-1rhdZIP/EvoI91ABIwNU5Q8+bWf8mjrS5UzIOZld4d4bXxJvtlUhlQvaoTogIGin/qdErMOrwaIJvCSIAKTLhA=="], @@ -389,7 +551,7 @@ "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="], - "picomatch": ["picomatch@4.0.4", "", {}, "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A=="], + "picomatch": ["picomatch@4.0.5", "", {}, "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A=="], "pino": ["pino@10.3.1", "", { "dependencies": { "@pinojs/redact": "^0.4.0", "atomic-sleep": "^1.0.0", "on-exit-leak-free": "^2.1.0", "pino-abstract-transport": "^3.0.0", "pino-std-serializers": "^7.0.0", "process-warning": "^5.0.0", "quick-format-unescaped": "^4.0.3", "real-require": "^0.2.0", "safe-stable-stringify": "^2.3.1", "sonic-boom": "^4.0.1", "thread-stream": "^4.0.0" }, "bin": { "pino": "bin.js" } }, "sha512-r34yH/GlQpKZbU1BvFFqOjhISRo1MNx1tWYsYvmj6KIRHSPMT2+yHOEb1SG6NMvRoHRF0a07kCOox/9yakl1vg=="], @@ -405,6 +567,8 @@ "process-warning": ["process-warning@5.0.0", "", {}, "sha512-a39t9ApHNx2L4+HBnQKqxxHNs1r7KF+Intd8Q/g1bUh6q0WIp9voPXJ/x0j+ZL45KF1pJd9+q2jLIRMfvEshkA=="], + "pure-rand": ["pure-rand@8.4.2", "", {}, "sha512-vvuOGgcuPJAirlHvuQw1TrOiw7ptaIXXmIbNuiNOY6lNGJJH49PQ1Kj4nd783nPdQhQdicgOjVI2yI/9BD6/Ng=="], + "quick-format-unescaped": ["quick-format-unescaped@4.0.4", "", {}, "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg=="], "react": ["react@19.2.7", "", {}, "sha512-HNe9WslTbXmFK8o8cmwgAeJFSBvt1bPdHCVKtaaV+WlAN36mpT4hcRpwbf3fY56ar2oIXzsBpOAiIRHAdY0OlQ=="], @@ -415,6 +579,8 @@ "real-require": ["real-require@0.2.0", "", {}, "sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg=="], + "resolve-pkg-maps": ["resolve-pkg-maps@1.0.0", "", {}, "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw=="], + "restore-cursor": ["restore-cursor@4.0.0", "", { "dependencies": { "onetime": "^5.1.0", "signal-exit": "^3.0.2" } }, "sha512-I9fPXU9geO9bHOt9pHHOhOkYerIMsmVaWB0rA2AI9ERh/+x/i7MV5HKBNrg+ljO5eoPVgCcnFuRjJ9uH6I/3eg=="], "safe-stable-stringify": ["safe-stable-stringify@2.5.0", "", {}, "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA=="], @@ -431,6 +597,8 @@ "slice-ansi": ["slice-ansi@9.0.0", "", { "dependencies": { "ansi-styles": "^6.2.3", "is-fullwidth-code-point": "^5.1.0" } }, "sha512-SO/3iYL5S3W57LLEniscOGPZgOqZUPCx6d3dB+52B80yJ0XstzsC/eV8gnA4tM3MHDrKz+OCFSLNjswdSC+/bA=="], + "smol-toml": ["smol-toml@1.7.1", "", {}, "sha512-PPlsspAZ4jbMBu5DMFhfUGDQLu/vrL4SyBROVS37x8ynnVmFIs1VPBz1Co8Xks3TvpIaZXmU85y4DrQ+UyVFoQ=="], + "sonic-boom": ["sonic-boom@4.2.1", "", { "dependencies": { "atomic-sleep": "^1.0.0" } }, "sha512-w6AxtubXa2wTXAUsZMMWERrsIRAdrK0Sc+FUytWvYAhBJLyuI4llrMIC1DtlNSdI99EI86KZum2MMq3EAZlF9Q=="], "source-map-js": ["source-map-js@1.2.1", "", {}, "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA=="], @@ -447,6 +615,8 @@ "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], + "strip-json-comments": ["strip-json-comments@5.0.3", "", {}, "sha512-1tB5mhVo7U+ETBKNf92xT4hrQa3pm0MZ0PQvuDnWgAAGHDsfp4lPSpiS6psrSiet87wyGPh9ft6wmhOMQ0hDiw=="], + "supports-color": ["supports-color@7.2.0", "", { "dependencies": { "has-flag": "^4.0.0" } }, "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw=="], "tagged-tag": ["tagged-tag@1.0.0", "", {}, "sha512-yEFYrVhod+hdNyx7g5Bnkkb0G6si8HJurOoOEgC8B/O0uXLHlaey/65KRv6cuWBNhBgHKAROVpc7QyYqE5gFng=="], @@ -465,20 +635,30 @@ "tinyrainbow": ["tinyrainbow@3.1.0", "", {}, "sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw=="], + "toml": ["toml@4.3.0", "", {}, "sha512-lVb8X9BsPVuH0M4BKeS91tXAmJvCjQ5UIyAbQFaxkKGyUFK2RPkhwaFSQH8vbpl1d23eu/IBH+dwVMHWaq9A5A=="], + "totalist": ["totalist@3.0.1", "", {}, "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ=="], + "tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], + "type-fest": ["type-fest@5.8.0", "", { "dependencies": { "tagged-tag": "^1.0.0" } }, "sha512-YGYEVz3Fm5iy/AybuA0oyNFq7H4CgQNfRp/qfe8nurE1kuCeNm3/vfm9X4Mtl+qLyaKJUh5xrFZwogr41SMjYA=="], "typescript": ["typescript@6.0.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw=="], + "unbash": ["unbash@4.0.4", "", {}, "sha512-60m9IVGbavD6jholbxt0jVBXZkEB/HsMZq7Tyaghseve2/Sf0zQRAIfWsD34sde+DKP2tBxJS2wP88ZM0D1FhA=="], + "undici-types": ["undici-types@8.3.0", "", {}, "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ=="], + "uuid": ["uuid@14.0.1", "", { "bin": { "uuid": "dist-node/bin/uuid" } }, "sha512-6ZxzVpzDXDa3bJWaHilVayA+BH/1zmxCJoVgvmqJnid/gPoKHxUrS/aC/T6LGQtNHT+XHG9fXPJB4d+IrU30Ew=="], + "vite": ["@voidzero-dev/vite-plus-core@0.2.4", "", { "dependencies": { "@oxc-project/runtime": "=0.138.0", "@oxc-project/types": "=0.138.0", "lightningcss": "^1.32.0", "postcss": "^8.5.6" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@arethetypeswrong/core": "^0.18.1", "@types/node": "^20.19.0 || >=22.12.0", "@vitejs/devtools": "^0.3.0", "esbuild": "^0.27.0 || ^0.28.0", "jiti": ">=1.21.0", "less": "^4.0.0", "publint": "^0.3.8", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "typescript": "^5.0.0 || ^6.0.0", "unplugin-unused": "^0.5.0", "unrun": "*", "yaml": "^2.4.2" }, "optionalPeers": ["@arethetypeswrong/core", "@types/node", "@vitejs/devtools", "esbuild", "jiti", "less", "publint", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "typescript", "unplugin-unused", "unrun", "yaml"] }, "sha512-AoAYGPwNO56o9TuCR+KaQGA5XpSnTpn2QYHK0DQ0f8j3wvaMmToeWOJF6STo+XMntMDfiaB7sOsSFNE+hPYyjg=="], "vite-plus": ["vite-plus@0.2.4", "", { "dependencies": { "@oxc-project/types": "=0.138.0", "@oxlint/plugins": "=1.68.0", "@vitest/browser": "4.1.10", "@vitest/browser-preview": "4.1.10", "@vitest/expect": "4.1.10", "@vitest/mocker": "4.1.10", "@vitest/pretty-format": "4.1.10", "@vitest/runner": "4.1.10", "@vitest/snapshot": "4.1.10", "@vitest/spy": "4.1.10", "@vitest/utils": "4.1.10", "@voidzero-dev/vite-plus-core": "0.2.4", "oxfmt": "=0.57.0", "oxlint": "=1.72.0", "oxlint-tsgolint": "=0.24.0", "vitest": "4.1.10" }, "optionalDependencies": { "@voidzero-dev/vite-plus-darwin-arm64": "0.2.4", "@voidzero-dev/vite-plus-darwin-x64": "0.2.4", "@voidzero-dev/vite-plus-linux-arm64-gnu": "0.2.4", "@voidzero-dev/vite-plus-linux-arm64-musl": "0.2.4", "@voidzero-dev/vite-plus-linux-x64-gnu": "0.2.4", "@voidzero-dev/vite-plus-linux-x64-musl": "0.2.4", "@voidzero-dev/vite-plus-win32-arm64-msvc": "0.2.4", "@voidzero-dev/vite-plus-win32-x64-msvc": "0.2.4" }, "peerDependencies": { "@vitest/browser-playwright": "4.1.10", "@vitest/browser-webdriverio": "4.1.10" }, "optionalPeers": ["@vitest/browser-playwright", "@vitest/browser-webdriverio"], "bin": { "oxfmt": "bin/oxfmt", "oxlint": "bin/oxlint", "vp": "bin/vp", "vpr": "bin/vpr" } }, "sha512-gaBBjOXIq9lLRU44oAYdIr99p+JBLX1kxs+l/6LqGgSXwcVKAdDa1boSrOTELqYCkQQ0fpppXUGWi9o6JDT5zw=="], "vitest": ["vitest@4.1.10", "", { "dependencies": { "@vitest/expect": "4.1.10", "@vitest/mocker": "4.1.10", "@vitest/pretty-format": "4.1.10", "@vitest/runner": "4.1.10", "@vitest/snapshot": "4.1.10", "@vitest/spy": "4.1.10", "@vitest/utils": "4.1.10", "es-module-lexer": "^2.0.0", "expect-type": "^1.3.0", "magic-string": "^0.30.21", "obug": "^2.1.1", "pathe": "^2.0.3", "picomatch": "^4.0.3", "std-env": "^4.0.0-rc.1", "tinybench": "^2.9.0", "tinyexec": "^1.0.2", "tinyglobby": "^0.2.15", "tinyrainbow": "^3.1.0", "vite": "^6.0.0 || ^7.0.0 || ^8.0.0", "why-is-node-running": "^2.3.0" }, "peerDependencies": { "@edge-runtime/vm": "*", "@opentelemetry/api": "^1.9.0", "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0", "@vitest/browser-playwright": "4.1.10", "@vitest/browser-preview": "4.1.10", "@vitest/browser-webdriverio": "4.1.10", "@vitest/coverage-istanbul": "4.1.10", "@vitest/coverage-v8": "4.1.10", "@vitest/ui": "4.1.10", "happy-dom": "*", "jsdom": "*" }, "optionalPeers": ["@edge-runtime/vm", "@opentelemetry/api", "@types/node", "@vitest/browser-playwright", "@vitest/browser-preview", "@vitest/browser-webdriverio", "@vitest/coverage-istanbul", "@vitest/coverage-v8", "@vitest/ui", "happy-dom", "jsdom"], "bin": { "vitest": "./vitest.mjs" } }, "sha512-R9jUTe5S4Qb0HCd4TNqpC7oGcrMssMRGXLW80ubjWsW9VH5GF8y1Y0SFLY9AbqSk6nt0PnOx4H4WNJYZ13GUPw=="], + "walk-up-path": ["walk-up-path@4.0.0", "", {}, "sha512-3hu+tD8YzSLGuFYtPRb48vdhKMi0KQV5sn+uWr8+7dMEq/2G/dtLrdDinkLjqq5TIbIBjYJ4Ax/n3YiaW7QM8A=="], + "why-is-node-running": ["why-is-node-running@2.3.0", "", { "dependencies": { "siginfo": "^2.0.0", "stackback": "0.0.2" }, "bin": { "why-is-node-running": "cli.js" } }, "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w=="], "widest-line": ["widest-line@6.0.0", "", { "dependencies": { "string-width": "^8.1.0" } }, "sha512-U89AsyEeAsyoF0zVJBkG9zBgekjgjK7yk9sje3F4IQpXBJ10TF6ByLlIfjMhcmHMJgHZI4KHt4rdNfktzxIAMA=="], @@ -487,16 +667,24 @@ "ws": ["ws@8.21.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g=="], + "yaml": ["yaml@2.9.0", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA=="], + "yoga-layout": ["yoga-layout@3.2.1", "", {}, "sha512-0LPOt3AxKqMdFBZA3HBAt/t/8vIKq7VaQYbuA8WxCgung+p9TVyKRYdpvCb80HcdTN2NkbIKbhNwKUfm3tQywQ=="], "zod": ["zod@4.4.3", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="], "@babel/code-frame/js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="], + "oxc-parser/@oxc-project/types": ["@oxc-project/types@0.140.0", "", {}, "sha512-h5LUOzGArYemnW1NMz/DuuQhBi96J6JL2Bk8zE4kvqxB5Sg3jxmCiH4uyOWHDkiKSt5vWlG4FIwCR/DbstcNRQ=="], + "pretty-format/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], "pretty-format/ansi-styles": ["ansi-styles@5.2.0", "", {}, "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA=="], "thread-stream/real-require": ["real-require@1.0.0", "", {}, "sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g=="], + + "tinyglobby/picomatch": ["picomatch@4.0.4", "", {}, "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A=="], + + "vitest/picomatch": ["picomatch@4.0.4", "", {}, "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A=="], } } diff --git a/knip.ts b/knip.ts new file mode 100644 index 0000000..c619902 --- /dev/null +++ b/knip.ts @@ -0,0 +1,27 @@ +/// + +import type { KnipConfig } from "knip"; + +const effectReferenceManifest = new URL("./repos/effect/package.json", import.meta.url); +const effectReferenceExists = await Bun.file(effectReferenceManifest).exists(); +const ignoredReferenceFiles = effectReferenceExists ? ["repos/**"] : []; + +export default { + entry: [ + "src/app/cli/entrypoint.ts", + "src/app/cli/module-entrypoint.ts", + "src/tooling/oxlint/oxlint-plugin.ts", + "src/**/*.test.{ts,tsx}", + "scripts/**/*.test.ts", + "test/domains/**/*.test.ts", + "test/e2e/**/*.e2e.test.ts", + "test/e2e/fixtures/**/*.ts", + ], + project: ["src/**/*.{ts,tsx}", "scripts/**/*.ts", "test/**/*.ts"], + ignore: ignoredReferenceFiles, + ignoreDependencies: ["@effect/language-service", "@effect/tsgo", "vite"], + ignoreUnresolved: ["bun-types"], + ignoreIssues: { + "src/domains/tools/runtime/tool-sdk.ts": ["exports", "types"], + }, +} satisfies KnipConfig; diff --git a/package.json b/package.json index 5ef23a6..850ec87 100644 --- a/package.json +++ b/package.json @@ -27,18 +27,24 @@ "prepack": "vp run build" }, "dependencies": { + "@effect/platform-bun": "4.0.0-beta.102", "@types/bun": "1.3.14", "commander": "15.0.0", + "effect": "4.0.0-beta.102", "ink": "^7.1.0", "react": "^19.2.7", "typescript": "6.0.3", "zod": "4.4.3" }, "devDependencies": { + "@effect/tsgo": "0.24.3", + "@effect/vitest": "4.0.0-beta.102", + "@oxlint/plugins": "1.68.0", "@types/react": "^19.0.0", "@typescript/native": "npm:typescript@7.0.2", "@vitest/coverage-v8": "4.1.10", "ink-testing-library": "^4.0.0", + "knip": "6.29.0", "pino": "^10.3.1", "vite": "npm:@voidzero-dev/vite-plus-core@0.2.4", "vite-plus": "0.2.4" diff --git a/repos/effect b/repos/effect new file mode 160000 index 0000000..de2a9a6 --- /dev/null +++ b/repos/effect @@ -0,0 +1 @@ +Subproject commit de2a9a69099993087e57c64df58537c765ac0224 diff --git a/scripts/bench.ts b/scripts/bench.ts index 1faf067..86795e3 100644 --- a/scripts/bench.ts +++ b/scripts/bench.ts @@ -1,3 +1,7 @@ -import { RigBenchmarkSuiteClass } from "./lib/benchmark"; +import { BunRuntime } from "@effect/platform-bun"; +import { Effect } from "effect"; +import { RigBenchmarkService } from "./lib/benchmark"; -await new RigBenchmarkSuiteClass().run(); +BunRuntime.runMain( + RigBenchmarkService.use((service) => service.run).pipe(Effect.provide(RigBenchmarkService.layer)), +); diff --git a/scripts/lib/benchmark.ts b/scripts/lib/benchmark.ts index cb3bec4..34563ff 100644 --- a/scripts/lib/benchmark.ts +++ b/scripts/lib/benchmark.ts @@ -1,17 +1,22 @@ import { mkdir, mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; -type BenchmarkRuntime = "bun" | "node"; +const BenchmarkRuntime = Schema.Literals(["bun", "node"]); +type BenchmarkRuntime = typeof BenchmarkRuntime.Type; -class BenchmarkCommandClass { - constructor( - readonly name: string, - readonly args: string[], - readonly env: Record = {}, - readonly p50BudgetMs: Partial> = {}, - ) {} -} +export class BenchmarkError extends Schema.TaggedErrorClass()("BenchmarkError", { + operation: Schema.String, + cause: Schema.Defect(), +}) {} + +class BenchmarkCommand extends Schema.Class("BenchmarkCommand")({ + name: Schema.String, + args: Schema.Array(Schema.String), + env: Schema.Record(Schema.String, Schema.Union([Schema.String, Schema.Undefined])), + p50BudgetMs: Schema.Record(Schema.String, Schema.Finite), +}) {} type BenchmarkResult = { name: string; @@ -21,312 +26,300 @@ type BenchmarkResult = { mean: number; }; -class RigBenchmarkRunnerClass { - constructor( - private readonly entrypoint: string, - private readonly iterations = 20, - private readonly warmups = 3, - ) {} - - async run(command: BenchmarkCommandClass, runtime: BenchmarkRuntime): Promise { - for (let index = 0; index < this.warmups; index++) this.spawn(command, runtime); - - const times: number[] = []; - for (let index = 0; index < this.iterations; index++) { - const started = performance.now(); - this.spawn(command, runtime); - times.push(performance.now() - started); - } - - times.sort((left, right) => left - right); - return { - name: command.name, - min: times[0] ?? 0, - p50: times[Math.floor(times.length * 0.5)] ?? 0, - p95: times[Math.floor(times.length * 0.95)] ?? 0, - mean: times.reduce((total, value) => total + value, 0) / times.length, - }; +export class RigBenchmarkService extends Context.Service< + RigBenchmarkService, + { + readonly run: Effect.Effect; } +>()("@rendotdev/rig/scripts/RigBenchmarkService", { + make: Effect.gen(function* () { + const entrypoint = process.env.RIG_BENCH_ENTRY ?? "dist/bin.mjs"; + const iterations = Number(process.env.RIG_BENCH_ITERATIONS ?? 20); + const warmups = 3; - spawn(command: BenchmarkCommandClass, runtime: BenchmarkRuntime): void { - const executable = runtime === "bun" ? process.execPath : "node"; - const result = Bun.spawnSync({ - cmd: [executable, this.entrypoint, ...command.args], - env: { ...process.env, ...command.env }, - stdout: "pipe", - stderr: "pipe", - }); + const attempt = (operation: string, run: () => Promise) => + Effect.tryPromise({ + try: run, + catch: (cause) => new BenchmarkError({ operation, cause }), + }); - if (result.exitCode !== 0) { - throw new Error(`${command.name} failed under ${runtime}: ${result.stderr.toString()}`); - } - } -} + const makeCommand = ( + name: string, + args: string[], + env: Record = {}, + p50BudgetMs: Partial> = {}, + ) => new BenchmarkCommand({ name, args, env, p50BudgetMs }); -class BenchmarkReporterClass { - print(result: BenchmarkResult, runtime: BenchmarkRuntime, budgetMs?: number): void { - const budget = budgetMs === undefined ? "" : ` budget=${budgetMs.toFixed(0)}ms`; - console.log( - `${result.name.padEnd(28)} min=${result.min.toFixed(1)}ms p50=${result.p50.toFixed(1)}ms p95=${result.p95.toFixed(1)}ms mean=${result.mean.toFixed(1)}ms${budget}`, - ); - if (budgetMs !== undefined && result.p50 > budgetMs && process.env.RIG_BENCH_ENFORCE !== "0") { - throw new Error( - `${result.name} exceeded its ${runtime} p50 budget: ${result.p50.toFixed(1)}ms > ${budgetMs.toFixed(1)}ms`, - ); - } - } -} - -export class RigBenchmarkSuiteClass { - private readonly entrypoint = process.env.RIG_BENCH_ENTRY ?? "dist/bin.mjs"; - private readonly iterations = Number(process.env.RIG_BENCH_ITERATIONS ?? 20); - private readonly runner = new RigBenchmarkRunnerClass(this.entrypoint, this.iterations); - private readonly reporter = new BenchmarkReporterClass(); + const spawn = Effect.fn("RigBenchmarkService.spawn")(function* ( + command: BenchmarkCommand, + runtime: BenchmarkRuntime, + ) { + return yield* Effect.try({ + try: () => { + const executable = runtime === "bun" ? process.execPath : "node"; + const result = Bun.spawnSync({ + cmd: [executable, entrypoint, ...command.args], + env: { ...process.env, ...command.env }, + stdout: "pipe", + stderr: "pipe", + }); + if (result.exitCode !== 0) { + throw new Error(`${command.name} failed under ${runtime}: ${result.stderr.toString()}`); + } + }, + catch: (cause) => new BenchmarkError({ operation: `run ${command.name}`, cause }), + }); + }); - async run(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-bench-home-")); - try { - const environment = this.environment(home); - this.runner.spawn( - new BenchmarkCommandClass("setup", ["create", "sample"], environment), - "bun", - ); - await Promise.all([ - this.createNestedBenchmarkTools(home), - this.createCollectionBenchmarkTool(home), - this.createLoggingBenchmarkTool(home), - this.createSearchBenchmarkTools(home), - ]); + const benchmark = Effect.fn("RigBenchmarkService.benchmark")(function* ( + command: BenchmarkCommand, + runtime: BenchmarkRuntime, + ) { + for (let index = 0; index < warmups; index++) yield* spawn(command, runtime); + const times: number[] = []; + for (let index = 0; index < iterations; index++) { + const started = performance.now(); + yield* spawn(command, runtime); + times.push(performance.now() - started); + } + times.sort((left, right) => left - right); + return { + name: command.name, + min: times[0] ?? 0, + p50: times[Math.floor(times.length * 0.5)] ?? 0, + p95: times[Math.floor(times.length * 0.95)] ?? 0, + mean: times.reduce((total, value) => total + value, 0) / times.length, + } satisfies BenchmarkResult; + }); - const commands = this.commands(environment); - console.log(`Entrypoint: ${this.entrypoint}`); - console.log(`Iterations: ${this.iterations}`); - await this.runtimes().reduce(async (previousRuntime, runtime) => { - await previousRuntime; - console.log(`\nRuntime: ${runtime}`); - await commands.reduce(async (previousCommand, command) => { - await previousCommand; - this.reporter.print( - await this.runner.run(command, runtime), - runtime, - command.p50BudgetMs[runtime], + const report = Effect.fn("RigBenchmarkService.report")(function* ( + result: BenchmarkResult, + runtime: BenchmarkRuntime, + budgetMs?: number, + ) { + return yield* Effect.try({ + try: () => { + const budget = budgetMs === undefined ? "" : ` budget=${budgetMs.toFixed(0)}ms`; + console.log( + `${result.name.padEnd(28)} min=${result.min.toFixed(1)}ms p50=${result.p50.toFixed(1)}ms p95=${result.p95.toFixed(1)}ms mean=${result.mean.toFixed(1)}ms${budget}`, ); - }, Promise.resolve()); - }, Promise.resolve()); - } finally { - await rm(home, { recursive: true, force: true }); - } - } - - private commands(environment: Record): BenchmarkCommandClass[] { - const searchToolCount = this.searchToolCount(); - const commands = [ - new BenchmarkCommandClass("config path", ["config", "path"], environment, { - bun: 60, - node: 130, - }), - new BenchmarkCommandClass(`list (${searchToolCount + 5} tools)`, ["list"], environment, { - bun: 70 + searchToolCount * 0.25, - node: 150 + searchToolCount * 0.3, - }), - new BenchmarkCommandClass( - `find (${searchToolCount} tools)`, - ["find", "resize local image width"], - environment, - { bun: 80 + searchToolCount * 0.3, node: 160 + searchToolCount * 0.4 }, - ), - new BenchmarkCommandClass("help command", ["help", "sample.example"], environment, { - bun: 90, - node: 150, - }), - new BenchmarkCommandClass("run command", ["run", "sample.example", "Agent"], environment, { - bun: 100, - node: 180, - }), - new BenchmarkCommandClass( - "nested run (50)", - ["run", "caller.call", "--input", JSON.stringify({ count: 50 })], - environment, - { bun: 110, node: 190 }, - ), - new BenchmarkCommandClass( - "collection unused (1000)", - ["run", "collection-bench.noop"], - environment, - { bun: 100, node: 180 }, - ), - new BenchmarkCommandClass( - "collection count (1000)", - ["run", "collection-bench.count"], - environment, - { bun: 130, node: 230 }, - ), - new BenchmarkCommandClass( - "structured logs (1000)", - ["run", "logging-bench.write", "1000"], - { ...environment, RIG_LOG: "1" }, - { bun: 120, node: 200 }, - ), - ]; - - if (process.env.RIG_BENCH_TYPECHECK === "1") { - commands.push(new BenchmarkCommandClass("typecheck", ["typecheck", "sample"], environment)); - } - return commands; - } + const exceedsEnforcedBudget = + budgetMs !== undefined && + result.p50 > budgetMs && + process.env.RIG_BENCH_ENFORCE !== "0"; + if (exceedsEnforcedBudget) { + throw new Error( + `${result.name} exceeded its ${runtime} p50 budget: ${result.p50.toFixed(1)}ms > ${budgetMs.toFixed(1)}ms`, + ); + } + }, + catch: (cause) => new BenchmarkError({ operation: `report ${result.name}`, cause }), + }); + }); - private environment(home: string): Record { - return { + const environment = (home: string): Record => ({ RIG_HOME: home, RIG_AGENT_SYNC: "0", RIG_UPDATE_CHECK: "0", RIG_LOG: "0", + }); + + const searchToolCount = () => Number(process.env.RIG_BENCH_TOOLS ?? 100); + + const runtimes = (): BenchmarkRuntime[] => { + const requested = (process.env.RIG_BENCH_RUNTIMES ?? "bun,node") + .split(",") + .map((runtime) => runtime.trim()) + .filter(Boolean); + const invalid = requested.find((runtime) => runtime !== "bun" && runtime !== "node"); + if (invalid) throw new Error(`Unsupported benchmark runtime: ${invalid}`); + return [...new Set(requested)] as BenchmarkRuntime[]; }; - } - private runtimes(): BenchmarkRuntime[] { - const requested = (process.env.RIG_BENCH_RUNTIMES ?? "bun,node") - .split(",") - .map((runtime) => runtime.trim()) - .filter(Boolean); - const invalid = requested.find((runtime) => runtime !== "bun" && runtime !== "node"); - if (invalid) throw new Error(`Unsupported benchmark runtime: ${invalid}`); - return [...new Set(requested)] as BenchmarkRuntime[]; - } + const commands = (env: Record): BenchmarkCommand[] => { + const toolCount = searchToolCount(); + const values = [ + makeCommand("config path", ["config", "path"], env, { bun: 60, node: 130 }), + makeCommand(`list (${toolCount + 5} tools)`, ["list"], env, { + bun: 70 + toolCount * 0.25, + node: 150 + toolCount * 0.3, + }), + makeCommand(`find (${toolCount} tools)`, ["find", "resize local image width"], env, { + bun: 80 + toolCount * 0.3, + node: 160 + toolCount * 0.4, + }), + makeCommand("help command", ["help", "sample.example"], env, { bun: 90, node: 150 }), + makeCommand("run command", ["run", "sample.example", "Agent"], env, { + bun: 100, + node: 180, + }), + makeCommand( + "nested run (50)", + ["run", "caller.call", "--input", JSON.stringify({ count: 50 })], + env, + { bun: 110, node: 190 }, + ), + makeCommand("collection unused (1000)", ["run", "collection-bench.noop"], env, { + bun: 100, + node: 180, + }), + makeCommand("collection count (1000)", ["run", "collection-bench.count"], env, { + bun: 130, + node: 230, + }), + makeCommand( + "structured logs (1000)", + ["run", "logging-bench.write", "1000"], + { ...env, RIG_LOG: "1" }, + { bun: 120, node: 200 }, + ), + ]; + if (process.env.RIG_BENCH_TYPECHECK === "1") { + values.push(makeCommand("typecheck", ["typecheck", "sample"], env)); + } + return values; + }; - private searchToolCount(): number { - return Number(process.env.RIG_BENCH_TOOLS ?? 100); - } + const writeTool = Effect.fn("RigBenchmarkService.writeTool")(function* ( + path: string, + source: string, + ) { + return yield* attempt(`write ${path}`, () => Bun.write(path, source)); + }); - private async createNestedBenchmarkTools(home: string): Promise { - const toolsDir = join(home, "rig", "tools"); - const calleeDir = join(toolsDir, "callee"); - const callerDir = join(toolsDir, "caller"); - await Promise.all([ - mkdir(calleeDir, { recursive: true }), - mkdir(callerDir, { recursive: true }), - ]); - await Promise.all([ - Bun.write( - join(calleeDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ + const createNestedTools = Effect.fn("RigBenchmarkService.createNestedTools")(function* ( + home: string, + ) { + const toolsDir = join(home, "rig", "tools"); + const calleeDir = join(toolsDir, "callee"); + const callerDir = join(toolsDir, "caller"); + yield* attempt("create nested tool directories", () => + Promise.all([ + mkdir(calleeDir, { recursive: true }), + mkdir(callerDir, { recursive: true }), + ]).then(() => undefined), + ); + yield* Effect.all([ + writeTool( + join(calleeDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ description: "Nested benchmark callee.", - commands: { - read: rig.defineCommand({ - description: "Return one.", - input: rig.z.object({}), - output: rig.z.number(), - run: () => 1, - }), - }, + commands: { read: rig.defineCommand({ description: "Return one.", input: rig.z.object({}), output: rig.z.number(), run: () => 1 }) }, }); `, - ), - Bun.write( - join(callerDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ + ), + writeTool( + join(callerDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ description: "Nested benchmark caller.", - commands: { - call: rig.defineCommand({ - description: "Call another tool repeatedly.", - input: rig.z.object({ count: rig.z.number() }), - output: rig.z.array(rig.z.number()), - run: async (context) => { - const values = []; - for (let index = 0; index < context.input.count; index++) { - values.push(await context.rig.run({ command: "callee.read" })); - } - return values; - }, - }), - }, + commands: { call: rig.defineCommand({ + description: "Call another tool repeatedly.", input: rig.z.object({ count: rig.z.number() }), output: rig.z.array(rig.z.number()), + run: async (context) => { const values = []; for (let index = 0; index < context.input.count; index++) values.push(await context.rig.run({ command: "callee.read" })); return values; }, + }) }, }); `, - ), - ]); - } + ), + ]); + }); - private async createCollectionBenchmarkTool(home: string): Promise { - const toolDir = join(home, "rig", "tools", "collection-bench"); - await Bun.write( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - description: "Collection benchmark tool.", - collections: { documents: {} }, + const createCollectionTool = Effect.fn("RigBenchmarkService.createCollectionTool")(function* ( + home: string, + ) { + const toolDir = join(home, "rig", "tools", "collection-bench"); + yield* writeTool( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + description: "Collection benchmark tool.", collections: { documents: {} }, commands: { - noop: rig.defineCommand({ - description: "Return without using collections.", - input: rig.z.object({}), - output: rig.z.number(), - run: () => 1, - }), - count: rig.defineCommand({ - description: "Count collection entries.", - input: rig.z.object({}), - output: rig.z.number(), - run: (context) => context.collections.documents.count(), - }), + noop: rig.defineCommand({ description: "Return without using collections.", input: rig.z.object({}), output: rig.z.number(), run: () => 1 }), + count: rig.defineCommand({ description: "Count collection entries.", input: rig.z.object({}), output: rig.z.number(), run: (context) => context.collections.documents.count() }), }, }); `, - ); - await Promise.all( - Array.from({ length: 1_000 }, (_, index) => - Bun.write( - join(toolDir, "documents", `${index}.md`), - `---\nvalue: ${index}\n---\n\nBenchmark document ${index}.\n`, + ); + yield* Effect.all( + Array.from({ length: 1_000 }, (_, index) => + writeTool( + join(toolDir, "documents", `${index}.md`), + `---\nvalue: ${index}\n---\n\nBenchmark document ${index}.\n`, + ), ), - ), - ); - } + { concurrency: "unbounded" }, + ); + }); - private async createLoggingBenchmarkTool(home: string): Promise { - await Bun.write( - join(home, "rig", "tools", "logging-bench", "index.rig.ts"), - `export default (rig) => rig.defineTool({ + const createLoggingTool = Effect.fn("RigBenchmarkService.createLoggingTool")(function* ( + home: string, + ) { + return yield* writeTool( + join(home, "rig", "tools", "logging-bench", "index.rig.ts"), + `export default (rig) => rig.defineTool({ description: "Logging benchmark tool.", - commands: { - write: rig.defineCommand({ - description: "Write structured log records.", - input: rig.z.object({ count: rig.z.coerce.number() }), - output: rig.z.number(), - run: (context) => { - for (let index = 0; index < context.input.count; index++) { - context.log.info({ index }, "Benchmark log record."); - } - return context.input.count; - }, - }), - }, + commands: { write: rig.defineCommand({ + description: "Write structured log records.", input: rig.z.object({ count: rig.z.coerce.number() }), output: rig.z.number(), + run: (context) => { for (let index = 0; index < context.input.count; index++) context.log.info({ index }, "Benchmark log record."); return context.input.count; }, + }) }, }); `, - ); - } + ); + }); - private async createSearchBenchmarkTools(home: string): Promise { - await Promise.all( - Array.from({ length: this.searchToolCount() }, (_, index) => { - const name = `search-${index}`; - return Bun.write( - join(home, "rig", "tools", name, "index.rig.ts"), - `export default (rig) => rig.defineTool({ + const createSearchTools = Effect.fn("RigBenchmarkService.createSearchTools")(function* ( + home: string, + ) { + yield* Effect.all( + Array.from({ length: searchToolCount() }, (_, index) => { + const name = `search-${index}`; + return writeTool( + join(home, "rig", "tools", name, "index.rig.ts"), + `export default (rig) => rig.defineTool({ description: "Resize, sharpen, and inspect local image documents.", - commands: { - convert: rig.defineCommand({ - description: "Convert image ${index} with width, quality, and output format options.", - input: rig.z.object({ - input: rig.z.string().describe("Local image path"), - width: rig.z.number().optional(), - format: rig.z.string().optional(), - }), - output: rig.z.string(), - run: (context) => context.input.input, - }), - }, + commands: { convert: rig.defineCommand({ + description: "Convert image ${index} with width, quality, and output format options.", + input: rig.z.object({ input: rig.z.string().describe("Local image path"), width: rig.z.number().optional(), format: rig.z.string().optional() }), + output: rig.z.string(), run: (context) => context.input.input, + }) }, }); `, - ); - }), - ); - } + ); + }), + { concurrency: "unbounded" }, + ); + }); + + const run = Effect.acquireUseRelease( + attempt("create benchmark home", () => mkdtemp(join(tmpdir(), "rig-bench-home-"))), + (home) => + Effect.gen(function* () { + const env = environment(home); + yield* spawn(makeCommand("setup", ["create", "sample"], env), "bun"); + yield* Effect.all([ + createNestedTools(home), + createCollectionTool(home), + createLoggingTool(home), + createSearchTools(home), + ]); + const commandList = commands(env); + yield* Effect.sync(() => { + console.log(`Entrypoint: ${entrypoint}`); + console.log(`Iterations: ${iterations}`); + }); + for (const runtime of runtimes()) { + yield* Effect.sync(() => console.log(`\nRuntime: ${runtime}`)); + for (const command of commandList) { + yield* report( + yield* benchmark(command, runtime), + runtime, + command.p50BudgetMs[runtime], + ); + } + } + }), + (home) => Effect.promise(() => rm(home, { recursive: true, force: true })), + ).pipe(Effect.withSpan("RigBenchmarkService.run")); + + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(RigBenchmarkService, RigBenchmarkService.make); } diff --git a/scripts/lib/release.test.ts b/scripts/lib/release.test.ts index f5685ae..3c6e7b2 100644 --- a/scripts/lib/release.test.ts +++ b/scripts/lib/release.test.ts @@ -1,30 +1,56 @@ +import { Effect } from "effect"; import { describe, expect, test } from "vite-plus/test"; -import { SemverVersionClass, VersionBumpResolverClass } from "./release"; +import { ReleaseService } from "./release"; -describe("release versions", () => { - const versions = new SemverVersionClass(0, 0, 0); - const bumps = new VersionBumpResolverClass(); +const runRelease = ( + use: (service: ReleaseService["Service"]) => Effect.Effect, +): Result => + Effect.runSync(ReleaseService.use(use).pipe(Effect.provide(ReleaseService.layer), Effect.orDie)); +describe("release versions", () => { test("parses and orders stable and prerelease versions", () => { - expect(versions.parse("0.0.37-beta.0").toString()).toBe("0.0.37-beta.0"); - expect( - versions.parse("0.0.37-beta.1").compare(versions.parse("0.0.37-beta.0")), - ).toBeGreaterThan(0); - expect(versions.parse("0.0.37").compare(versions.parse("0.0.37-beta.9"))).toBeGreaterThan(0); - expect(versions.parse("0.0.37-beta.0").compare(versions.parse("0.0.36"))).toBeGreaterThan(0); + runRelease((versions) => + Effect.gen(function* () { + const beta0 = yield* versions.parseVersion("0.0.37-beta.0"); + const beta1 = yield* versions.parseVersion("0.0.37-beta.1"); + const stable = yield* versions.parseVersion("0.0.37"); + const previous = yield* versions.parseVersion("0.0.36"); + expect(yield* versions.renderVersion(beta0)).toBe("0.0.37-beta.0"); + expect(yield* versions.compareVersions(beta1, beta0)).toBeGreaterThan(0); + expect( + yield* versions.compareVersions(stable, yield* versions.parseVersion("0.0.37-beta.9")), + ).toBeGreaterThan(0); + expect(yield* versions.compareVersions(beta0, previous)).toBeGreaterThan(0); + }), + ); }); test("bumps stable and repeated beta versions", () => { - expect(bumps.next("0.0.36", "beta")).toBe("0.0.37-beta.0"); - expect(bumps.next("0.0.37-beta.0", "beta")).toBe("0.0.37-beta.1"); - expect(bumps.next("0.0.37-beta.1", "patch")).toBe("0.0.37"); + runRelease((versions) => + Effect.gen(function* () { + expect(yield* versions.nextVersion("0.0.36", "beta")).toBe("0.0.37-beta.0"); + expect(yield* versions.nextVersion("0.0.37-beta.0", "beta")).toBe("0.0.37-beta.1"); + expect(yield* versions.nextVersion("0.0.37-beta.1", "patch")).toBe("0.0.37"); + }), + ); }); test("accepts increasing explicit prereleases and rejects invalid versions", () => { - expect(bumps.next("0.0.36", "0.0.37-beta.0")).toBe("0.0.37-beta.0"); - expect(() => bumps.next("0.0.37-beta.1", "0.0.37-beta.0")).toThrow( - "Next version must be greater", + const next = runRelease((versions) => versions.nextVersion("0.0.36", "0.0.37-beta.0")); + expect(next).toBe("0.0.37-beta.0"); + + const invalidNext = Effect.runSync( + ReleaseService.use((versions) => + Effect.flip(versions.nextVersion("0.0.37-beta.1", "0.0.37-beta.0")), + ).pipe(Effect.provide(ReleaseService.layer)), + ); + expect(invalidNext.message).toContain("Next version must be greater"); + + const invalidVersion = Effect.runSync( + ReleaseService.use((versions) => Effect.flip(versions.parseVersion("0.0.37-beta.01"))).pipe( + Effect.provide(ReleaseService.layer), + ), ); - expect(() => versions.parse("0.0.37-beta.01")).toThrow("leading zeroes"); + expect(invalidVersion.message).toContain("leading zeroes"); }); }); diff --git a/scripts/lib/release.ts b/scripts/lib/release.ts index f4aa65a..eac76b9 100644 --- a/scripts/lib/release.ts +++ b/scripts/lib/release.ts @@ -1,4 +1,9 @@ -class ReleaseErrorClass extends Error {} +import { Context, Effect, Layer, Predicate, Schema } from "effect"; + +export class ReleaseError extends Schema.TaggedErrorClass()("ReleaseError", { + message: Schema.String, + cause: Schema.optional(Schema.Defect()), +}) {} type ReleaseOptions = { bump: string; @@ -14,285 +19,361 @@ type PackageManifest = { [key: string]: unknown; }; -class ReleaseOptionsParserClass { - parse(args: string[]): ReleaseOptions | "help" { - const options: ReleaseOptions = { - bump: "patch", - checks: true, - dryRun: false, - push: true, - tag: true, - }; - const positional: string[] = []; - - for (const arg of args) { - if (arg === "--help" || arg === "-h") return "help"; - if (arg === "--dry-run") options.dryRun = true; - else if (arg === "--no-push") options.push = false; - else if (arg === "--no-tag") options.tag = false; - else if (arg === "--skip-checks") options.checks = false; - else if (arg.startsWith("-")) throw new ReleaseErrorClass(`Unknown release option: ${arg}`); - else positional.push(arg); - } - - if (positional.length > 1) { - throw new ReleaseErrorClass(`Expected one version bump, received: ${positional.join(", ")}`); - } - - options.bump = positional[0] ?? options.bump; - return options; +export class SemverVersion extends Schema.Class("SemverVersion")({ + major: Schema.Finite, + minor: Schema.Finite, + patch: Schema.Finite, + prerelease: Schema.Array(Schema.String), +}) {} + +export class ReleaseService extends Context.Service< + ReleaseService, + { + readonly parseVersion: (value: string) => Effect.Effect; + readonly compareVersions: (left: SemverVersion, right: SemverVersion) => Effect.Effect; + readonly renderVersion: (version: SemverVersion) => Effect.Effect; + readonly nextVersion: ( + currentValue: string, + bump: string, + ) => Effect.Effect; + readonly run: (args: string[]) => Effect.Effect; } - - usage(): string { - return [ - "Usage: vp run release -- [beta|patch|minor|major|x.y.z[-prerelease]] [options]", - "", - "By default this commits package.json, pushes an annotated vX tag, and lets GitHub Actions run publish.yml from CI. It never runs npm publish locally.", - "", - "Options:", - " --dry-run Print the planned release without changing files.", - " --no-push Commit and tag locally, but do not push or start CI.", - " --no-tag Commit the version bump without creating a git tag or starting publish.yml.", - " --skip-checks Skip vp run validate.", - "", - "Examples:", - " vp run release:beta", - " vp run release:patch", - " vp run release -- minor", - " vp run release -- 0.1.0-beta.0 --no-push", - ].join("\n"); - } -} - -export class SemverVersionClass { - constructor( - readonly major: number, - readonly minor: number, - readonly patch: number, - readonly prerelease: readonly string[] = [], - ) {} - - parse(value: string): SemverVersionClass { - const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/.exec(value); - if (!match) { - throw new ReleaseErrorClass(`Version must use semver: ${value}`); - } - const prerelease = match[4]?.split(".") ?? []; - if (prerelease.some((identifier) => /^\d+$/.test(identifier) && /^0\d+/.test(identifier))) { - throw new ReleaseErrorClass( - `Numeric prerelease identifiers cannot have leading zeroes: ${value}`, - ); - } - return new SemverVersionClass(Number(match[1]), Number(match[2]), Number(match[3]), prerelease); - } - - bump(kind: string): SemverVersionClass { - if (kind === "major") return new SemverVersionClass(this.major + 1, 0, 0); - if (kind === "minor") return new SemverVersionClass(this.major, this.minor + 1, 0); - if (kind === "patch") { - return this.prerelease.length > 0 - ? new SemverVersionClass(this.major, this.minor, this.patch) - : new SemverVersionClass(this.major, this.minor, this.patch + 1); - } - if (kind === "beta") { - if (this.prerelease[0] === "beta") { - const sequence = Number(this.prerelease[1] ?? "0"); - return new SemverVersionClass(this.major, this.minor, this.patch, [ - "beta", - String(Number.isSafeInteger(sequence) ? sequence + 1 : 0), - ]); +>()("@rendotdev/rig/scripts/ReleaseService", { + make: Effect.gen(function* () { + const failure = (message: string, cause?: unknown) => new ReleaseError({ message, cause }); + + const parseOptions = (args: string[]): ReleaseOptions | "help" => { + const options: ReleaseOptions = { + bump: "patch", + checks: true, + dryRun: false, + push: true, + tag: true, + }; + const positional: string[] = []; + for (const arg of args) { + const isHelpOption = arg === "--help" || arg === "-h"; + if (isHelpOption) return "help"; + if (arg === "--dry-run") options.dryRun = true; + else if (arg === "--no-push") options.push = false; + else if (arg === "--no-tag") options.tag = false; + else if (arg === "--skip-checks") options.checks = false; + else if (arg.startsWith("-")) throw failure(`Unknown release option: ${arg}`); + else positional.push(arg); } - return new SemverVersionClass(this.major, this.minor, this.patch + 1, ["beta", "0"]); - } - throw new ReleaseErrorClass( - `Version bump must be beta, patch, minor, major, or explicit semver: ${kind}`, - ); - } - - compare(other: SemverVersionClass): number { - if (this.major !== other.major) return this.major - other.major; - if (this.minor !== other.minor) return this.minor - other.minor; - if (this.patch !== other.patch) return this.patch - other.patch; - if (this.prerelease.length === 0) return other.prerelease.length === 0 ? 0 : 1; - if (other.prerelease.length === 0) return -1; - - const length = Math.max(this.prerelease.length, other.prerelease.length); - for (let index = 0; index < length; index++) { - const left = this.prerelease[index]; - const right = other.prerelease[index]; - if (left === undefined) return -1; - if (right === undefined) return 1; - if (left === right) continue; - const leftNumeric = /^\d+$/.test(left); - const rightNumeric = /^\d+$/.test(right); - if (leftNumeric && rightNumeric) return Number(left) - Number(right); - if (leftNumeric) return -1; - if (rightNumeric) return 1; - return left.localeCompare(right); - } - return 0; - } - - toString(): string { - const core = `${this.major}.${this.minor}.${this.patch}`; - return this.prerelease.length > 0 ? `${core}-${this.prerelease.join(".")}` : core; - } -} - -const semverVersions = new SemverVersionClass(0, 0, 0); + if (positional.length > 1) { + throw failure(`Expected one version bump, received: ${positional.join(", ")}`); + } + options.bump = positional[0] ?? options.bump; + return options; + }; -export class VersionBumpResolverClass { - next(currentValue: string, bump: string): string { - const current = semverVersions.parse(currentValue); - if (["beta", "major", "minor", "patch"].includes(bump)) { - return current.bump(bump).toString(); - } + const usage = () => + [ + "Usage: vp run release -- [beta|patch|minor|major|x.y.z[-prerelease]] [options]", + "", + "By default this commits package.json, pushes an annotated vX tag, and lets GitHub Actions run publish.yml from CI. It never runs npm publish locally.", + "", + "Options:", + " --dry-run Print the planned release without changing files.", + " --no-push Commit and tag locally, but do not push or start CI.", + " --no-tag Commit the version bump without creating a git tag or starting publish.yml.", + " --skip-checks Skip vp run validate.", + "", + "Examples:", + " vp run release:beta", + " vp run release:patch", + " vp run release -- minor", + " vp run release -- 0.1.0-beta.0 --no-push", + ].join("\n"); + + const parseVersionValue = (value: string): SemverVersion => { + const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/.exec(value); + if (!match) throw failure(`Version must use semver: ${value}`); + const prerelease = match[4]?.split(".") ?? []; + if (prerelease.some((identifier) => /^\d+$/.test(identifier) && /^0\d+/.test(identifier))) { + throw failure(`Numeric prerelease identifiers cannot have leading zeroes: ${value}`); + } + return new SemverVersion({ + major: Number(match[1]), + minor: Number(match[2]), + patch: Number(match[3]), + prerelease, + }); + }; - const explicit = semverVersions.parse(bump); - if (explicit.compare(current) <= 0) { - throw new ReleaseErrorClass(`Next version must be greater than ${current}: ${explicit}`); - } - return explicit.toString(); - } -} + const compareVersionValues = (left: SemverVersion, right: SemverVersion): number => { + if (left.major !== right.major) return left.major - right.major; + if (left.minor !== right.minor) return left.minor - right.minor; + if (left.patch !== right.patch) return left.patch - right.patch; + if (left.prerelease.length === 0) return right.prerelease.length === 0 ? 0 : 1; + if (right.prerelease.length === 0) return -1; + const length = Math.max(left.prerelease.length, right.prerelease.length); + for (let index = 0; index < length; index++) { + const leftPart = left.prerelease[index]; + const rightPart = right.prerelease[index]; + if (leftPart === undefined) return -1; + if (rightPart === undefined) return 1; + if (leftPart === rightPart) continue; + const leftNumeric = /^\d+$/.test(leftPart); + const rightNumeric = /^\d+$/.test(rightPart); + const hasNumericParts = leftNumeric && rightNumeric; + if (hasNumericParts) return Number(leftPart) - Number(rightPart); + if (leftNumeric) return -1; + if (rightNumeric) return 1; + return leftPart.localeCompare(rightPart); + } + return 0; + }; -class PackageManifestFileClass { - constructor(private readonly path: string) {} + const renderVersionValue = (version: SemverVersion): string => { + const core = `${version.major}.${version.minor}.${version.patch}`; + return version.prerelease.length > 0 ? `${core}-${version.prerelease.join(".")}` : core; + }; - async read(): Promise { - const data = JSON.parse(await Bun.file(this.path).text()); - if (typeof data !== "object" || data === null || Array.isArray(data)) { - throw new ReleaseErrorClass(`${this.path} must contain a JSON object.`); - } - return data as PackageManifest; - } + const bumpVersion = (version: SemverVersion, kind: string): SemverVersion => { + if (kind === "major") { + return new SemverVersion({ major: version.major + 1, minor: 0, patch: 0, prerelease: [] }); + } + if (kind === "minor") { + return new SemverVersion({ + major: version.major, + minor: version.minor + 1, + patch: 0, + prerelease: [], + }); + } + if (kind === "patch") { + return new SemverVersion({ + major: version.major, + minor: version.minor, + patch: version.prerelease.length > 0 ? version.patch : version.patch + 1, + prerelease: [], + }); + } + if (kind === "beta") { + if (version.prerelease[0] === "beta") { + const sequence = Number(version.prerelease[1] ?? "0"); + return new SemverVersion({ + major: version.major, + minor: version.minor, + patch: version.patch, + prerelease: ["beta", String(Number.isSafeInteger(sequence) ? sequence + 1 : 0)], + }); + } + return new SemverVersion({ + major: version.major, + minor: version.minor, + patch: version.patch + 1, + prerelease: ["beta", "0"], + }); + } + throw failure(`Version bump must be beta, patch, minor, major, or explicit semver: ${kind}`); + }; - async write(manifest: PackageManifest): Promise { - await Bun.write(this.path, `${JSON.stringify(manifest, null, 2)}\n`); - } -} + const nextVersionValue = (currentValue: string, bump: string): string => { + const current = parseVersionValue(currentValue); + if (["beta", "major", "minor", "patch"].includes(bump)) { + return renderVersionValue(bumpVersion(current, bump)); + } + const explicit = parseVersionValue(bump); + if (compareVersionValues(explicit, current) <= 0) { + throw failure( + `Next version must be greater than ${renderVersionValue(current)}: ${renderVersionValue(explicit)}`, + ); + } + return renderVersionValue(explicit); + }; -class CommandRunnerClass { - run(command: string[]): void { - console.log(`$ ${this.render(command)}`); - const result = Bun.spawnSync({ - cmd: command, - stdout: "inherit", - stderr: "inherit", - stdin: "inherit", + const parseVersion = Effect.fn("ReleaseService.parseVersion")(function* (value: string) { + return yield* Effect.try({ + try: () => parseVersionValue(value), + catch: (cause) => + cause instanceof ReleaseError + ? cause + : failure(`Could not parse version: ${value}`, cause), + }); }); - if (result.exitCode !== 0) { - throw new ReleaseErrorClass( - `Command failed with exit code ${result.exitCode}: ${this.render(command)}`, - ); - } - } - - capture(command: string[]): string { - const result = Bun.spawnSync({ cmd: command, stdout: "pipe", stderr: "pipe" }); - if (result.exitCode !== 0) { - throw new ReleaseErrorClass( - result.stderr.toString().trim() || `Command failed: ${this.render(command)}`, - ); - } - return result.stdout.toString().trim(); - } - - private render(command: string[]): string { - return command.map((part) => (/[\s"']/.test(part) ? JSON.stringify(part) : part)).join(" "); - } -} - -class GitReleaseRepositoryClass { - constructor(private readonly runner: CommandRunnerClass) {} - - ensureClean(): void { - const status = this.runner.capture(["git", "status", "--porcelain"]); - if (status) throw new ReleaseErrorClass("Release requires a clean git working tree."); - } - ensureTagMissing(tag: string): void { - const existing = this.runner.capture(["git", "tag", "--list", tag]); - if (existing) throw new ReleaseErrorClass(`Tag already exists: ${tag}`); - } - - currentBranch(): string { - const branch = this.runner.capture(["git", "branch", "--show-current"]); - if (!branch) throw new ReleaseErrorClass("Could not determine the current git branch."); - return branch; - } -} - -class ReleaseCommandClass { - private readonly optionsParser = new ReleaseOptionsParserClass(); - private readonly packageFile = new PackageManifestFileClass("package.json"); - private readonly runner = new CommandRunnerClass(); - private readonly repository = new GitReleaseRepositoryClass(this.runner); - private readonly versions = new VersionBumpResolverClass(); - - async run(args: string[]): Promise { - const options = this.optionsParser.parse(args); - if (options === "help") { - console.log(this.optionsParser.usage()); - return; - } + const compareVersions = Effect.fn("ReleaseService.compareVersions")(function* ( + left: SemverVersion, + right: SemverVersion, + ) { + return compareVersionValues(left, right); + }); - const manifest = await this.packageFile.read(); - if (typeof manifest.version !== "string") - throw new ReleaseErrorClass("package.json needs a version."); - const nextVersion = this.versions.next(manifest.version, options.bump); - const tagName = `v${nextVersion}`; - const packageName = manifest.name ?? "package"; + const renderVersion = Effect.fn("ReleaseService.renderVersion")(function* ( + version: SemverVersion, + ) { + return renderVersionValue(version); + }); - console.log(`Release ${packageName}: ${manifest.version} -> ${nextVersion}`); - if (options.dryRun) { - console.log(`Dry run only. ${this.renderDryRunPlan(options, tagName)}`); - return; - } + const nextVersion = Effect.fn("ReleaseService.nextVersion")(function* ( + currentValue: string, + bump: string, + ) { + return yield* Effect.try({ + try: () => nextVersionValue(currentValue, bump), + catch: (cause) => + cause instanceof ReleaseError + ? cause + : failure(`Could not resolve next version from ${currentValue}.`, cause), + }); + }); - this.repository.ensureClean(); - if (options.tag) this.repository.ensureTagMissing(tagName); - if (options.checks) this.runChecks(); + const renderCommand = (command: string[]) => + command.map((part) => (/\s|["']/.test(part) ? JSON.stringify(part) : part)).join(" "); + + const executeCommand = Effect.fn("ReleaseService.executeCommand")(function* ( + command: string[], + ) { + return yield* Effect.try({ + try: () => { + console.log(`$ ${renderCommand(command)}`); + const result = Bun.spawnSync({ + cmd: command, + stdout: "inherit", + stderr: "inherit", + stdin: "inherit", + }); + if (result.exitCode !== 0) { + throw failure( + `Command failed with exit code ${result.exitCode}: ${renderCommand(command)}`, + ); + } + }, + catch: (cause) => + cause instanceof ReleaseError + ? cause + : failure(`Command failed: ${renderCommand(command)}`, cause), + }); + }); - manifest.version = nextVersion; - await this.packageFile.write(manifest); - this.runner.run(["vp", "fmt", "package.json"]); - this.runner.run(["git", "add", "package.json"]); - this.runner.run(["git", "commit", "-m", `Release ${tagName}`]); + const captureCommand = Effect.fn("ReleaseService.captureCommand")(function* ( + command: string[], + ) { + return yield* Effect.try({ + try: () => { + const result = Bun.spawnSync({ cmd: command, stdout: "pipe", stderr: "pipe" }); + if (result.exitCode !== 0) { + throw failure( + result.stderr.toString().trim() || `Command failed: ${renderCommand(command)}`, + ); + } + return result.stdout.toString().trim(); + }, + catch: (cause) => + cause instanceof ReleaseError + ? cause + : failure(`Command failed: ${renderCommand(command)}`, cause), + }); + }); - if (options.tag) this.runner.run(["git", "tag", "-a", tagName, "-m", `Release ${tagName}`]); - if (options.push) this.push(options, tagName); + const readManifest = Effect.tryPromise({ + try: async () => { + const data = JSON.parse(await Bun.file("package.json").text()) as unknown; + if (!Predicate.isObject(data)) throw failure("package.json must contain a JSON object."); + return data as PackageManifest; + }, + catch: (cause) => + cause instanceof ReleaseError ? cause : failure("Could not read package.json.", cause), + }).pipe(Effect.withSpan("ReleaseService.readManifest")); + + const writeManifest = Effect.fn("ReleaseService.writeManifest")(function* ( + manifest: PackageManifest, + ) { + return yield* Effect.tryPromise({ + try: () => + Bun.write("package.json", `${JSON.stringify(manifest, null, 2)}\n`).then(() => undefined), + catch: (cause) => failure("Could not write package.json.", cause), + }); + }); - console.log(`Release ${tagName} is ready. ${this.renderCiStatus(options, tagName)}`); - } + const ensureClean = Effect.gen(function* () { + const status = yield* captureCommand(["git", "status", "--porcelain"]); + if (status) return yield* failure("Release requires a clean git working tree."); + return undefined; + }).pipe(Effect.withSpan("ReleaseService.ensureClean")); - private renderDryRunPlan(options: ReleaseOptions, tagName: string): string { - if (!options.push) return "Would leave the release commit and tag local."; - if (!options.tag) return "Would push the release commit without starting publish.yml."; - return `Would push ${tagName}; GitHub Actions would run publish.yml from CI.`; - } + const ensureTagMissing = Effect.fn("ReleaseService.ensureTagMissing")(function* (tag: string) { + const existing = yield* captureCommand(["git", "tag", "--list", tag]); + if (existing) return yield* failure(`Tag already exists: ${tag}`); + return undefined; + }); - private renderCiStatus(options: ReleaseOptions, tagName: string): string { - if (!options.push) return "Release commit and tag are local only."; - if (!options.tag) return "Pushed the release commit without starting publish.yml."; - return `Pushed ${tagName}; GitHub Actions will run publish.yml from CI.`; - } + const currentBranch = Effect.gen(function* () { + const branch = yield* captureCommand(["git", "branch", "--show-current"]); + if (!branch) return yield* failure("Could not determine the current git branch."); + return branch; + }).pipe(Effect.withSpan("ReleaseService.currentBranch")); - private runChecks(): void { - this.runner.run(["vp", "run", "validate"]); - } + const renderDryRunPlan = (options: ReleaseOptions, tagName: string): string => { + if (!options.push) return "Would leave the release commit and tag local."; + if (!options.tag) return "Would push the release commit without starting publish.yml."; + return `Would push ${tagName}; GitHub Actions would run publish.yml from CI.`; + }; - private push(options: ReleaseOptions, tagName: string): void { - this.runner.run(["git", "push", "origin", this.repository.currentBranch()]); - if (options.tag) this.runner.run(["git", "push", "origin", tagName]); - } -} + const renderCiStatus = (options: ReleaseOptions, tagName: string): string => { + if (!options.push) return "Release commit and tag are local only."; + if (!options.tag) return "Pushed the release commit without starting publish.yml."; + return `Pushed ${tagName}; GitHub Actions will run publish.yml from CI.`; + }; -export class ReleaseEntrypointClass { - run(): void { - new ReleaseCommandClass().run(Bun.argv.slice(2)).catch((error) => { - console.error(error instanceof Error ? error.message : String(error)); - process.exit(1); + const run = Effect.fn("ReleaseService.run")(function* (args: string[]) { + const options = yield* Effect.try({ + try: () => parseOptions(args), + catch: (cause) => + cause instanceof ReleaseError + ? cause + : failure("Could not parse release options.", cause), + }); + if (options === "help") { + yield* Effect.sync(() => console.log(usage())); + return undefined; + } + const manifest = yield* readManifest; + if (typeof manifest.version !== "string") { + return yield* failure("package.json needs a version."); + } + const next = yield* Effect.try({ + try: () => nextVersionValue(manifest.version!, options.bump), + catch: (cause) => + cause instanceof ReleaseError ? cause : failure("Could not resolve next version.", cause), + }); + const tagName = `v${next}`; + const packageName = manifest.name ?? "package"; + yield* Effect.sync(() => + console.log(`Release ${packageName}: ${manifest.version} -> ${next}`), + ); + if (options.dryRun) { + yield* Effect.sync(() => + console.log(`Dry run only. ${renderDryRunPlan(options, tagName)}`), + ); + return undefined; + } + yield* ensureClean; + if (options.tag) yield* ensureTagMissing(tagName); + if (options.checks) yield* executeCommand(["vp", "run", "validate"]); + manifest.version = next; + yield* writeManifest(manifest); + yield* executeCommand(["vp", "fmt", "package.json"]); + yield* executeCommand(["git", "add", "package.json"]); + yield* executeCommand(["git", "commit", "-m", `Release ${tagName}`]); + if (options.tag) { + yield* executeCommand(["git", "tag", "-a", tagName, "-m", `Release ${tagName}`]); + } + if (options.push) { + yield* executeCommand(["git", "push", "origin", yield* currentBranch]); + if (options.tag) yield* executeCommand(["git", "push", "origin", tagName]); + } + yield* Effect.sync(() => + console.log(`Release ${tagName} is ready. ${renderCiStatus(options, tagName)}`), + ); + return undefined; }); - } + + return { parseVersion, compareVersions, renderVersion, nextVersion, run } as const; + }), +}) { + static readonly layer = Layer.effect(ReleaseService, ReleaseService.make); } diff --git a/scripts/lib/smoke.ts b/scripts/lib/smoke.ts index 62fb1f0..1449f8f 100644 --- a/scripts/lib/smoke.ts +++ b/scripts/lib/smoke.ts @@ -1,48 +1,87 @@ import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; -export class SmokeScriptClass { - private readonly cliPath = join(import.meta.dir, "..", "..", "dist", "bin.mjs"); - private home = ""; +export class SmokeScriptError extends Schema.TaggedErrorClass()( + "SmokeScriptError", + { + operation: Schema.String, + cause: Schema.Defect(), + }, +) {} - async run(): Promise { - this.home = await mkdtemp(join(tmpdir(), "rig-smoke-home-")); - try { - const env = { HOME: this.home, RIG_HOME: this.home }; - await this.rig(["init"], env); - await this.rig(["create", "sample"], env); - await this.rig(["list"], env); - await this.rig(["ls"], env); - await this.rig(["help", "sample.example"], env); - await this.rig(["inspect", "sample.example"], env); - await this.rig(["run", "sample.example", "--dry-run", "smoke"], env); - const stdout = await this.rig(["run", "sample.example", "smoke"], env); - const parsed = JSON.parse(stdout); - if (parsed.errors?.length !== 0 || parsed.data?.text !== "smoke") { - throw new Error(`Unexpected smoke output: ${stdout}`); - } - console.log("Smoke OK"); - } finally { - await rm(this.home, { recursive: true, force: true }); - } +export class SmokeScriptService extends Context.Service< + SmokeScriptService, + { + readonly run: Effect.Effect; } +>()("@rendotdev/rig/scripts/SmokeScriptService", { + make: Effect.gen(function* () { + const cliPath = join(import.meta.dir, "..", "..", "dist", "bin.mjs"); - private async rig(args: string[], env: Record): Promise { - const proc = Bun.spawn(["node", this.cliPath, ...args], { - cwd: this.home, - env: { ...process.env, ...env }, - stdout: "pipe", - stderr: "pipe", + const attempt = (operation: string, run: () => Promise) => + Effect.tryPromise({ + try: run, + catch: (cause) => new SmokeScriptError({ operation, cause }), + }); + + const runRig = Effect.fn("SmokeScriptService.runRig")(function* ( + home: string, + args: ReadonlyArray, + env: Readonly>, + ) { + return yield* attempt(`rig ${args.join(" ")}`, async () => { + const process = Bun.spawn(["node", cliPath, ...args], { + cwd: home, + env: { ...globalThis.process.env, ...env }, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([ + new Response(process.stdout).text(), + new Response(process.stderr).text(), + process.exited, + ]); + if (exitCode !== 0) { + throw new Error(`Command failed: rig ${args.join(" ")}\n${stderr}\n${stdout}`); + } + return stdout; + }); }); - const [stdout, stderr, exitCode] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]); - if (exitCode !== 0) { - throw new Error(`Command failed: rig ${args.join(" ")}\n${stderr}\n${stdout}`); - } - return stdout; - } + + const run = Effect.acquireUseRelease( + attempt("create smoke home", () => mkdtemp(join(tmpdir(), "rig-smoke-home-"))), + (home) => + Effect.gen(function* () { + const env = { HOME: home, RIG_HOME: home }; + yield* runRig(home, ["init"], env); + yield* runRig(home, ["create", "sample"], env); + yield* runRig(home, ["list"], env); + yield* runRig(home, ["ls"], env); + yield* runRig(home, ["help", "sample.example"], env); + yield* runRig(home, ["inspect", "sample.example"], env); + yield* runRig(home, ["run", "sample.example", "--dry-run", "smoke"], env); + const stdout = yield* runRig(home, ["run", "sample.example", "smoke"], env); + const parsed = yield* Effect.try({ + try: () => JSON.parse(stdout) as { errors?: unknown[]; data?: { text?: string } }, + catch: (cause) => new SmokeScriptError({ operation: "parse smoke output", cause }), + }); + const hasUnexpectedOutput = parsed.errors?.length !== 0 || parsed.data?.text !== "smoke"; + if (hasUnexpectedOutput) { + return yield* new SmokeScriptError({ + operation: "validate smoke output", + cause: new Error(`Unexpected smoke output: ${stdout}`), + }); + } + yield* Effect.sync(() => console.log("Smoke OK")); + return undefined; + }), + (home) => Effect.promise(() => rm(home, { recursive: true, force: true })), + ).pipe(Effect.withSpan("SmokeScriptService.run")); + + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(SmokeScriptService, SmokeScriptService.make); } diff --git a/scripts/release.ts b/scripts/release.ts index 28ba446..55b4d8d 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -1,3 +1,9 @@ -import { ReleaseEntrypointClass } from "./lib/release"; +import { BunRuntime } from "@effect/platform-bun"; +import { Effect } from "effect"; +import { ReleaseService } from "./lib/release"; -new ReleaseEntrypointClass().run(); +BunRuntime.runMain( + ReleaseService.use((service) => service.run(Bun.argv.slice(2))).pipe( + Effect.provide(ReleaseService.layer), + ), +); diff --git a/scripts/smoke.ts b/scripts/smoke.ts index 01c40fd..13ffc0f 100644 --- a/scripts/smoke.ts +++ b/scripts/smoke.ts @@ -1,3 +1,7 @@ -import { SmokeScriptClass } from "./lib/smoke"; +import { BunRuntime } from "@effect/platform-bun"; +import { Effect } from "effect"; +import { SmokeScriptService } from "./lib/smoke"; -await new SmokeScriptClass().run(); +BunRuntime.runMain( + SmokeScriptService.use((service) => service.run).pipe(Effect.provide(SmokeScriptService.layer)), +); diff --git a/src/agents/sync.ts b/src/agents/sync.ts deleted file mode 100644 index f75cf53..0000000 --- a/src/agents/sync.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../generated/agent/agent-instruction-sync"; diff --git a/src/app/cli/cli-application.ts b/src/app/cli/cli-application.ts new file mode 100644 index 0000000..b0d4311 --- /dev/null +++ b/src/app/cli/cli-application.ts @@ -0,0 +1,80 @@ +import { Cause, Context, Effect, Layer } from "effect"; +import { RigError } from "../../providers/errors/rig-error"; +import { + type CliCommandController, + cliCommandRegistrationLayer, + CliCronCommandRegistrationService, + CliDevCommandRegistrationService, + CliGeneralCommandRegistrationService, + CliToolExecutionCommandRegistrationService, + CliToolReadCommandRegistrationService, + CliToolWriteCommandRegistrationService, +} from "./cli-command-registration"; +import { CliGeneralOperationsService, cliGeneralOperationsLayer } from "./cli-general-operations"; +import { CliPlatformService, cliPlatformLayer } from "./cli-platform"; + +export class CliCommandService extends Context.Service< + CliCommandService, + { readonly run: (argv: string[]) => Effect.Effect } +>()("@rendotdev/rig/application/CliCommandService", { + make: Effect.gen(function* () { + const general = yield* CliGeneralOperationsService; + const generalRegistration = yield* CliGeneralCommandRegistrationService; + const toolReadRegistration = yield* CliToolReadCommandRegistrationService; + const toolWriteRegistration = yield* CliToolWriteCommandRegistrationService; + const toolExecutionRegistration = yield* CliToolExecutionCommandRegistrationService; + const cronRegistration = yield* CliCronCommandRegistrationService; + const devRegistration = yield* CliDevCommandRegistrationService; + const platform = yield* CliPlatformService; + const normalizeError = (cause: unknown): RigError => { + if (cause instanceof RigError) return cause; + return new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + }; + const execute = Effect.fn("CliCommandService.execute")(function* (argv: string[]) { + const program = new (yield* Effect.tryPromise({ + try: () => import("commander"), + catch: normalizeError, + })).Command(); + let pending: Effect.Effect | undefined; + let generatedSyncRequested = false; + const controller: CliCommandController = { + schedule: (effect, sync = false) => { + pending = effect; + generatedSyncRequested ||= sync; + }, + }; + program + .name("rig") + .description("Local typed command runtime for agents.") + .version(yield* general.version) + .addHelpCommand(false); + yield* generalRegistration.configure(program, controller); + yield* toolReadRegistration.configure(program, controller); + yield* toolWriteRegistration.configure(program, controller); + yield* toolExecutionRegistration.configure(program, controller); + yield* cronRegistration.configure(program, controller); + yield* devRegistration.configure(program, controller); + if (argv.slice(2).length === 0) controller.schedule(general.defaultStatus, true); + else yield* Effect.tryPromise({ try: () => program.parseAsync(argv), catch: normalizeError }); + if (pending) yield* pending; + if (generatedSyncRequested) yield* general.syncGeneratedFiles; + }); + const run = Effect.fn("CliCommandService.run")(function* (argv: string[]) { + return yield* execute(argv).pipe( + Effect.catchCause((cause) => platform.fail(Cause.squash(cause))), + ); + }); + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(CliCommandService, CliCommandService.make); +} + +export const cliCommandLayer = CliCommandService.layer.pipe( + Layer.provide( + Layer.mergeAll(cliCommandRegistrationLayer, cliGeneralOperationsLayer, cliPlatformLayer), + ), +); diff --git a/src/app/cli/cli-command-registration.ts b/src/app/cli/cli-command-registration.ts new file mode 100644 index 0000000..a31269a --- /dev/null +++ b/src/app/cli/cli-command-registration.ts @@ -0,0 +1,353 @@ +import type { Command } from "commander"; +import { Context, Effect, Layer } from "effect"; +import { + type CliCronAddOptions, + CliCronOperationsService, + cliCronOperationsLayer, + CliDevOperationsService, + cliDevOperationsLayer, +} from "./cli-cron-dev-operations"; +import { CliGeneralOperationsService, cliGeneralOperationsLayer } from "./cli-general-operations"; +import { + type CliFindOptions, + type CliRunOptions, + CliToolExecutionOperationsService, + cliToolExecutionOperationsLayer, + CliToolReadOperationsService, + cliToolReadOperationsLayer, + CliToolWriteOperationsService, + cliToolWriteOperationsLayer, +} from "./cli-tool-operations"; +import { CliUpdateOperationsService, cliUpdateOperationsLayer } from "./cli-update-operations"; + +export type CliCommandController = Readonly<{ + schedule: (effect: Effect.Effect, generatedSyncRequested?: boolean) => void; +}>; + +export class CliGeneralCommandRegistrationService extends Context.Service< + CliGeneralCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliGeneralCommandRegistrationService", { + make: Effect.gen(function* () { + const operations = yield* CliGeneralOperationsService; + const configure = Effect.fn("CliGeneralCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + program + .command("init") + .description("Initialize config and sync tools to AGENTS.md/CLAUDE.md.") + .action(() => controller.schedule(operations.defaultStatus, true)); + program + .command("doctor") + .description("Check local Rig setup.") + .action(() => controller.schedule(operations.doctor, true)); + const config = program.command("config").description("Manage Rig config."); + config + .command("show") + .description("Print config JSON.") + .action(() => controller.schedule(operations.showConfig)); + config + .command("path") + .description("Print absolute config path.") + .action(() => controller.schedule(operations.showConfigPath)); + program + .command("help") + .argument("[target]", "Topic, tool name, or command id (..)") + .description("Print topic help, tool docs, or general help.") + .action((target?: string) => + controller.schedule(operations.help(target, program.helpInformation())), + ); + const registry = program.command("registry").description("Manage tool registries."); + registry + .command("list") + .description("List registries as JSON.") + .action(() => controller.schedule(operations.listRegistries)); + registry + .command("create") + .argument("[path]") + .description("Add a custom registry. Defaults to the current directory.") + .action((path?: string) => controller.schedule(operations.addRegistry(path), true)); + registry + .command("remove") + .argument("") + .description("Remove a custom registry.") + .action((path: string) => controller.schedule(operations.removeRegistry(path), true)); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliGeneralCommandRegistrationService, + CliGeneralCommandRegistrationService.make, + ); +} + +export class CliToolReadCommandRegistrationService extends Context.Service< + CliToolReadCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolReadCommandRegistrationService", { + make: Effect.gen(function* () { + const operations = yield* CliToolReadOperationsService; + const configure = Effect.fn("CliToolReadCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + program + .command("list") + .alias("ls") + .description("List discovered tools and commands.") + .option("--json", "Print full JSON metadata.") + .action((options: { json?: boolean }) => + controller.schedule(operations.list(Boolean(options.json))), + ); + program + .command("find") + .argument("", "Natural-language description of the command to find.") + .description("Find commands with local typo-tolerant fuzzy search.") + .option("--limit ", "Maximum results to return, from 1 to 50.", "5") + .option("--tool ", "Search within one tool.") + .option("--json", "Print ranked result metadata as JSON.") + .action((query: string, options: CliFindOptions) => + controller.schedule(operations.find(query, options)), + ); + program + .command("inspect") + .argument("", "Tool name or command id (..)") + .description("Print tool or command metadata as JSON.") + .action((target: string) => controller.schedule(operations.inspect(target))); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolReadCommandRegistrationService, + CliToolReadCommandRegistrationService.make, + ); +} + +export class CliToolWriteCommandRegistrationService extends Context.Service< + CliToolWriteCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolWriteCommandRegistrationService", { + make: Effect.gen(function* () { + const operations = yield* CliToolWriteOperationsService; + const configure = Effect.fn("CliToolWriteCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + program + .command("create") + .argument("") + .description("Create a starter tool in the base registry.") + .action((name: string) => controller.schedule(operations.create(name), true)); + program + .command("edit") + .argument("") + .description("Print the TypeScript file path for a tool.") + .action((name: string) => controller.schedule(operations.edit(name))); + program + .command("remove") + .argument("") + .description("Remove a local tool directory.") + .action((name: string) => controller.schedule(operations.remove(name), true)); + program + .command("env") + .argument("", "Tool name.") + .argument( + "[assignments...]", + "Use KEY=VALUE to set values, or remove KEY [KEY...] to remove values.", + ) + .description("Show, write, or remove tool .env values using its env schema.") + .action((target: string, assignments: string[]) => + controller.schedule(operations.env(target, assignments)), + ); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolWriteCommandRegistrationService, + CliToolWriteCommandRegistrationService.make, + ); +} + +export class CliToolExecutionCommandRegistrationService extends Context.Service< + CliToolExecutionCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolExecutionCommandRegistrationService", { + make: Effect.gen(function* () { + const operations = yield* CliToolExecutionOperationsService; + const configure = Effect.fn("CliToolExecutionCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + program + .command("run") + .argument("", "Command id, formatted as ..") + .argument("[args...]", "Command arguments.") + .description("Run a tool command.") + .option("--input ", "JSON input string.") + .option("--input-file ", "Read JSON input from a file.") + .option("--dry-run", "Validate input and show what would run without executing.") + .option("--query ", "Print one field from the JSON envelope, such as data.output.") + .option("--as ", "Attach this command's data to a pipeline context under the given id.") + .option("--pipe", "Read a Rig pipeline context from stdin for @id.path references.") + .action((commandId: string, args: string[], options: CliRunOptions) => + controller.schedule(operations.run(commandId, args, options)), + ); + program + .command("typecheck") + .argument("[tool]") + .description("Type-check local tool files with the injected Rig tool runtime types.") + .action((tool?: string) => controller.schedule(operations.typecheck(tool))); + program + .command("migrate") + .description("Inspect local tools for Rig tool API migrations.") + .option("--json", "Print the migration report as JSON.") + .action((options: { json?: boolean }) => + controller.schedule(operations.migrate(Boolean(options.json))), + ); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolExecutionCommandRegistrationService, + CliToolExecutionCommandRegistrationService.make, + ); +} + +export class CliCronCommandRegistrationService extends Context.Service< + CliCronCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliCronCommandRegistrationService", { + make: Effect.gen(function* () { + const operations = yield* CliCronOperationsService; + const configure = Effect.fn("CliCronCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + const cron = program.command("cron").description("Manage scheduled Rig tool commands."); + cron + .command("list") + .description("List scheduled Rig tool commands as JSON.") + .action(() => controller.schedule(operations.list)); + cron + .command("add") + .argument("", "Unique job name, using letters, numbers, hyphens, or underscores.") + .argument("", "Command id, formatted as ..") + .argument("", "Cron expression or nickname, such as @weekly.") + .description("Schedule a Rig tool command with fixed JSON input.") + .option("--input ", "JSON input string.") + .option("--input-file ", "Read JSON input from a file.") + .action((name: string, command: string, schedule: string, options: CliCronAddOptions) => + controller.schedule(operations.add(name, command, schedule, options)), + ); + cron + .command("remove") + .argument("", "Cron job name.") + .description("Remove a scheduled Rig tool command.") + .action((name: string) => controller.schedule(operations.remove(name))); + cron + .command("run") + .argument("", "Cron job name.") + .description("Run a scheduled Rig tool command now.") + .action((name: string) => controller.schedule(operations.run(name))); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliCronCommandRegistrationService, + CliCronCommandRegistrationService.make, + ); +} + +export class CliDevCommandRegistrationService extends Context.Service< + CliDevCommandRegistrationService, + { + readonly configure: (program: Command, controller: CliCommandController) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliDevCommandRegistrationService", { + make: Effect.gen(function* () { + const dev = yield* CliDevOperationsService; + const update = yield* CliUpdateOperationsService; + const configure = Effect.fn("CliDevCommandRegistrationService.configure")(function* ( + program: Command, + controller: CliCommandController, + ) { + const command = program.command("dev").description("Local development helpers."); + command + .command("link") + .description("Link this checkout as the local rig command for development.") + .option("--bin-dir ", "Directory where the rig shim should be written.") + .option("--force", "Overwrite an existing non-Rig shim.") + .action((options: { binDir?: string; force?: boolean }) => + controller.schedule(dev.link(options)), + ); + command + .command("unlink") + .description("Remove the local rig development shim.") + .option("--bin-dir ", "Directory where the rig shim was written.") + .option("--force", "Remove even if the file is not a Rig dev shim.") + .action((options: { binDir?: string; force?: boolean }) => + controller.schedule(dev.unlink(options)), + ); + command + .command("status") + .description("Show local rig development shim status as JSON.") + .option("--bin-dir ", "Directory where the rig shim should be checked.") + .action((options: { binDir?: string }) => controller.schedule(dev.status(options))); + program + .command("update") + .description("Update rig to the latest published version.") + .action(() => controller.schedule(update.update)); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect( + CliDevCommandRegistrationService, + CliDevCommandRegistrationService.make, + ); +} + +const cliGeneralCommandRegistrationLayer = CliGeneralCommandRegistrationService.layer.pipe( + Layer.provide(cliGeneralOperationsLayer), +); +const cliToolReadCommandRegistrationLayer = CliToolReadCommandRegistrationService.layer.pipe( + Layer.provide(cliToolReadOperationsLayer), +); +const cliToolWriteCommandRegistrationLayer = CliToolWriteCommandRegistrationService.layer.pipe( + Layer.provide(cliToolWriteOperationsLayer), +); +const cliToolExecutionCommandRegistrationLayer = + CliToolExecutionCommandRegistrationService.layer.pipe( + Layer.provide(cliToolExecutionOperationsLayer), + ); +const cliCronCommandRegistrationLayer = CliCronCommandRegistrationService.layer.pipe( + Layer.provide(cliCronOperationsLayer), +); +const cliDevCommandRegistrationLayer = CliDevCommandRegistrationService.layer.pipe( + Layer.provide(Layer.merge(cliDevOperationsLayer, cliUpdateOperationsLayer)), +); + +export const cliCommandRegistrationLayer = Layer.mergeAll( + cliGeneralCommandRegistrationLayer, + cliToolReadCommandRegistrationLayer, + cliToolWriteCommandRegistrationLayer, + cliToolExecutionCommandRegistrationLayer, + cliCronCommandRegistrationLayer, + cliDevCommandRegistrationLayer, +); diff --git a/src/app/cli/cli-cron-dev-operations.ts b/src/app/cli/cli-cron-dev-operations.ts new file mode 100644 index 0000000..11dc2e4 --- /dev/null +++ b/src/app/cli/cli-cron-dev-operations.ts @@ -0,0 +1,101 @@ +import { fileURLToPath, pathToFileURL } from "node:url"; +import { Context, Effect, Layer } from "effect"; +import type { CronAddOptions } from "../../domains/scheduling/types/cron"; +import { RigCronService, rigCronLayer } from "../../domains/scheduling/runtime/rig-cron"; +import { DevLinkService, devLinkLayer } from "../../domains/updates/runtime/dev-link"; +import { CliEnvironmentConfigService, CliPlatformService, cliPlatformLayer } from "./cli-platform"; + +export type CliCronAddOptions = Readonly< + Omit +>; + +export class CliCronOperationsService extends Context.Service< + CliCronOperationsService, + { + readonly list: Effect.Effect; + readonly add: ( + name: string, + command: string, + schedule: string, + options: CliCronAddOptions, + ) => Effect.Effect; + readonly remove: (name: string) => Effect.Effect; + readonly run: (name: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliCronOperationsService", { + make: Effect.gen(function* () { + const cron = yield* RigCronService; + const platform = yield* CliPlatformService; + const environment = yield* CliEnvironmentConfigService; + const moduleUrl = pathToFileURL(environment.argv[1] ?? fileURLToPath(import.meta.url)).href; + const list = cron.list.pipe( + Effect.flatMap(platform.printJson), + Effect.withSpan("CliCronOperationsService.list"), + ); + const add = Effect.fn("CliCronOperationsService.add")(function* ( + name: string, + command: string, + schedule: string, + options: CliCronAddOptions, + ) { + yield* platform.printJson( + yield* cron.add({ name, command, schedule, ...options, moduleUrl }), + ); + }); + const remove = Effect.fn("CliCronOperationsService.remove")(function* (name: string) { + yield* platform.printJson(yield* cron.remove(name)); + }); + const run = Effect.fn("CliCronOperationsService.run")(function* (name: string) { + const result = yield* cron.run(name); + yield* platform.printJson(result.envelope); + yield* platform.setExitCode(result.exitCode); + }); + return { list, add, remove, run } as const; + }), +}) { + static readonly layer = Layer.effect(CliCronOperationsService, CliCronOperationsService.make); +} + +export class CliDevOperationsService extends Context.Service< + CliDevOperationsService, + { + readonly link: (options: { binDir?: string; force?: boolean }) => Effect.Effect; + readonly unlink: (options: { + binDir?: string; + force?: boolean; + }) => Effect.Effect; + readonly status: (options: { binDir?: string }) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliDevOperationsService", { + make: Effect.gen(function* () { + const devLink = yield* DevLinkService; + const platform = yield* CliPlatformService; + const link = Effect.fn("CliDevOperationsService.link")(function* (options: { + binDir?: string; + force?: boolean; + }) { + yield* platform.log(yield* devLink.renderLink(yield* devLink.link(options))); + }); + const unlink = Effect.fn("CliDevOperationsService.unlink")(function* (options: { + binDir?: string; + force?: boolean; + }) { + yield* platform.log(yield* devLink.renderUnlink(yield* devLink.unlink(options))); + }); + const status = Effect.fn("CliDevOperationsService.status")(function* (options: { + binDir?: string; + }) { + yield* platform.printJson(yield* devLink.status(options)); + }); + return { link, unlink, status } as const; + }), +}) { + static readonly layer = Layer.effect(CliDevOperationsService, CliDevOperationsService.make); +} + +export const cliCronOperationsLayer = CliCronOperationsService.layer.pipe( + Layer.provide(Layer.mergeAll(rigCronLayer, cliPlatformLayer, CliEnvironmentConfigService.layer)), +); +export const cliDevOperationsLayer = CliDevOperationsService.layer.pipe( + Layer.provide(Layer.merge(devLinkLayer, cliPlatformLayer)), +); diff --git a/src/app/cli/cli-general-operations.test.ts b/src/app/cli/cli-general-operations.test.ts new file mode 100644 index 0000000..dce9b7e --- /dev/null +++ b/src/app/cli/cli-general-operations.test.ts @@ -0,0 +1,83 @@ +import { describe, expect, it } from "vite-plus/test"; +import { Effect, Layer } from "effect"; +import { RegistryService } from "../../domains/registry/service/registry"; +import { AgentInstructionSyncService } from "../../domains/tools/runtime/agent-instruction-sync"; +import { RuntimeSupportService } from "../../domains/tools/runtime/runtime-support"; +import type { AgentInstructionSyncResult } from "../../domains/tools/types/agent-instruction"; +import { + type ToolRuntimeInstructionSyncResult, + ToolRuntimeInstructionSyncService, +} from "../../domains/tools/runtime/tool-runtime-instruction-sync"; +import { RigError } from "../../providers/errors/rig-error"; +import { CliEnvironmentConfigService } from "./cli-platform"; +import { CliGeneratedSyncOperationsService } from "./cli-general-operations"; + +const syncLayer = (params: { + agentSync: string; + tool: Effect.Effect; + agent: Effect.Effect; +}) => + CliGeneratedSyncOperationsService.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(CliEnvironmentConfigService, { + env: { RIG_AGENT_SYNC: params.agentSync }, + argv: [], + }), + Layer.succeed(RegistryService, { + list: Effect.succeed({ + baseRegistryDir: "/tmp/rig", + customRegistries: [], + registries: [], + }), + add: () => Effect.die("Registry add is unavailable in this test."), + remove: () => Effect.die("Registry remove is unavailable in this test."), + }), + Layer.succeed(RuntimeSupportService, { ensure: () => Effect.void }), + Layer.succeed(ToolRuntimeInstructionSyncService, { sync: params.tool }), + Layer.succeed(AgentInstructionSyncService, { + sync: params.agent, + discoverTargets: Effect.succeed([]), + renderBlock: () => Effect.succeed(""), + }), + ), + ), + ); + +describe("CliGeneratedSyncOperationsService", () => { + it("propagates tool instruction sync failures", async () => { + const expected = new RigError({ code: "INTERNAL_ERROR", message: "tool sync failed" }); + const error = await Effect.runPromise( + CliGeneratedSyncOperationsService.use((service) => service.sync).pipe( + Effect.provide( + syncLayer({ + agentSync: "0", + tool: Effect.fail(expected), + agent: Effect.succeed({ skipped: true, targets: [] }), + }), + ), + Effect.flip, + ), + ); + + expect(error).toBe(expected); + }); + + it("propagates agent instruction sync failures when enabled", async () => { + const expected = new RigError({ code: "INTERNAL_ERROR", message: "agent sync failed" }); + const error = await Effect.runPromise( + CliGeneratedSyncOperationsService.use((service) => service.sync).pipe( + Effect.provide( + syncLayer({ + agentSync: "1", + tool: Effect.succeed({ tools: [] }), + agent: Effect.fail(expected), + }), + ), + Effect.flip, + ), + ); + + expect(error).toBe(expected); + }); +}); diff --git a/src/app/cli/cli-general-operations.ts b/src/app/cli/cli-general-operations.ts new file mode 100644 index 0000000..8b5c62a --- /dev/null +++ b/src/app/cli/cli-general-operations.ts @@ -0,0 +1,348 @@ +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { + RigConfigStoreService, + rigConfigStoreLayer, +} from "../../domains/settings/service/config-store"; +import { + RigPathsConfigService, + rigPathsConfigLayer, + rigPathsLayer, +} from "../../providers/paths/rig-paths"; +import type { RigDirectoryMigrationResult } from "../../domains/settings/types/directory-migration"; +import { + AgentInstructionSyncService, + agentInstructionSyncLayer, +} from "../../domains/tools/runtime/agent-instruction-sync"; +import { + ToolRuntimeInstructionSyncService, + toolRuntimeInstructionSyncLayer, +} from "../../domains/tools/runtime/tool-runtime-instruction-sync"; +import { RuntimeSupportService } from "../../domains/tools/runtime/runtime-support"; +import { RigError } from "../../providers/errors/rig-error"; +import { RegistryService, registryLayer } from "../../domains/registry/service/registry"; +import { + ToolDiscoveryService, + toolDiscoveryLayer, +} from "../../domains/registry/service/tool-discovery"; +import { RigLoggerService, rigLoggerLayer } from "../../providers/logging/rig-logger"; +import { NpmUpdateCheckService } from "../../domains/updates/service/npm-update-check"; +import { npmUpdateCheckLayer } from "../../domains/updates/runtime/npm-update-check"; +import { ToolApiMigrationService } from "../../domains/tools/runtime/tool-api-migration"; +import { HelpTopicsService, helpTopicsLayer } from "../../domains/tools/service/help-topics"; +import { ToolHelpService } from "../../domains/tools/runtime/tool-help"; +import { toolServicesLayer } from "../../domains/tools/runtime/tool-services-layer"; +import { CliEnvironmentConfigService, CliPlatformService, cliPlatformLayer } from "./cli-platform"; +import { CliVersionService, cliVersionLayer } from "./cli-version"; + +class CliHelpOperationsService extends Context.Service< + CliHelpOperationsService, + { readonly help: (target: string | undefined, fallback: string) => Effect.Effect } +>()("@rendotdev/rig/application/CliHelpOperationsService", { + make: Effect.gen(function* () { + const topics = yield* HelpTopicsService; + const toolHelp = yield* ToolHelpService; + const version = yield* CliVersionService; + const platform = yield* CliPlatformService; + const help = Effect.fn("CliHelpOperationsService.help")(function* ( + target: string | undefined, + fallback: string, + ) { + if (!target) { + const readmePath = join(yield* version.packageRoot, "README.md"); + yield* platform.log( + (yield* platform.exists(readmePath)) ? yield* platform.readText(readmePath) : fallback, + ); + return; + } + if (target === "topics") { + yield* platform.log(yield* topics.renderList); + return; + } + const topic = yield* topics.render(target); + yield* platform.log(topic ?? (yield* toolHelp.render(target))); + }); + return { help } as const; + }), +}) { + static readonly layer = Layer.effect(CliHelpOperationsService, CliHelpOperationsService.make); +} + +class CliStatusNoticeService extends Context.Service< + CliStatusNoticeService, + { + readonly migration: (result: RigDirectoryMigrationResult | undefined) => Effect.Effect; + readonly notices: (currentVersion: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliStatusNoticeService", { + make: Effect.gen(function* () { + const updates = yield* NpmUpdateCheckService; + const toolMigrations = yield* ToolApiMigrationService; + const platform = yield* CliPlatformService; + const migration = Effect.fn("CliStatusNoticeService.migration")(function* ( + result: RigDirectoryMigrationResult | undefined, + ) { + if (!result) return; + if (result.status === "migrated") { + yield* platform.log("Rig moved its home folder:"); + yield* platform.log(` From: ${result.legacyDir}`); + yield* platform.log(` To: ${result.currentDir}`); + if (result.configUpdated) yield* platform.log(" Updated base registry: ~/rig/tools"); + yield* platform.log(""); + return; + } + yield* platform.log("Rig home folder migration needs your attention:"); + yield* platform.log(` Old folder: ${result.legacyDir}`); + yield* platform.log(` New folder: ${result.currentDir}`); + yield* platform.log(` Reason: ${result.reason}`); + yield* platform.log( + "Move the files you want to keep into the new folder, then remove the old folder.", + ); + yield* platform.log( + "This migration prompt is versioned; Rig will not show it again after this run.", + ); + yield* platform.log(""); + }); + const notices = Effect.fn("CliStatusNoticeService.notices")(function* (currentVersion: string) { + const report = yield* toolMigrations.inspect(); + if (!report.ready) yield* platform.log(`\n${yield* toolMigrations.renderCli(report)}`); + const notice = yield* updates + .check(currentVersion) + .pipe(Effect.orElseSucceed(() => undefined)); + if (notice) yield* platform.log(`\n${notice.message}`); + }); + return { migration, notices } as const; + }), +}) { + static readonly layer = Layer.effect(CliStatusNoticeService, CliStatusNoticeService.make); +} + +class CliStatusOperationsService extends Context.Service< + CliStatusOperationsService, + { + readonly defaultStatus: Effect.Effect; + readonly doctor: Effect.Effect; + } +>()("@rendotdev/rig/application/CliStatusOperationsService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const paths = yield* RigPathsConfigService; + const registry = yield* RegistryService; + const discovery = yield* ToolDiscoveryService; + const logger = yield* RigLoggerService; + const notices = yield* CliStatusNoticeService; + const version = yield* CliVersionService; + const platform = yield* CliPlatformService; + const defaultStatus = Effect.gen(function* () { + yield* config.ensure; + const migrationResult = yield* config.migrationResult; + yield* notices.migration(migrationResult); + if (migrationResult?.status === "manual") yield* config.acknowledgeMigrationPrompt; + const registries = yield* registry.list; + const tools = yield* discovery.discover(); + const currentVersion = yield* version.current; + const log = yield* logger.app("cli"); + yield* Effect.sync(() => + log.info({ version: currentVersion, tools: tools.length }, "Default status rendered."), + ); + yield* platform.log("Rig is ready.\n"); + yield* platform.log(`Version: ${currentVersion}`); + yield* platform.log(`Config: ${paths.configPath}`); + yield* platform.log(`Base registry: ${registries.registries[0]?.path}`); + const custom = registries.registries.filter((entry) => entry.kind === "custom"); + if (custom.length > 0) { + yield* platform.log("Custom registries:"); + for (const entry of custom) yield* platform.log(` ${entry.path}`); + } + yield* platform.log(`Tools found: ${tools.length}`); + yield* platform.log("\nNext steps:"); + yield* platform.log(" rig list"); + yield* platform.log('\nRun "rig doctor" if you want to verify your setup.'); + yield* notices.notices(currentVersion); + }).pipe(Effect.withSpan("CliStatusOperationsService.defaultStatus")); + const doctor = Effect.gen(function* () { + yield* config.ensure; + const migrationResult = yield* config.migrationResult; + yield* notices.migration(migrationResult); + if (migrationResult?.status === "manual") yield* config.acknowledgeMigrationPrompt; + const registries = yield* registry.list; + const tools = yield* discovery.discover(); + const log = yield* logger.app("doctor"); + yield* Effect.sync(() => + log.info( + { registries: registries.registries.length, tools: tools.length }, + "Doctor check completed.", + ), + ); + yield* platform.log("Rig doctor\n"); + yield* platform.log(`Config: OK ${paths.configPath}`); + yield* platform.log(`Runtime SDK: OK ${paths.runtimeSdkPath}`); + yield* platform.log(`Registries: ${registries.registries.length}`); + for (const entry of registries.registries) + yield* platform.log(` ${entry.kind}: ${entry.path}`); + yield* platform.log(`Tools: ${tools.length}`); + yield* platform.log("\nStatus: OK"); + yield* notices.notices(yield* version.current); + }).pipe(Effect.withSpan("CliStatusOperationsService.doctor")); + return { defaultStatus, doctor } as const; + }), +}) { + static readonly layer = Layer.effect(CliStatusOperationsService, CliStatusOperationsService.make); +} + +export class CliGeneratedSyncOperationsService extends Context.Service< + CliGeneratedSyncOperationsService, + { readonly sync: Effect.Effect } +>()("@rendotdev/rig/application/CliGeneratedSyncOperationsService", { + make: Effect.gen(function* () { + const environment = yield* CliEnvironmentConfigService; + const tools = yield* ToolRuntimeInstructionSyncService; + const agents = yield* AgentInstructionSyncService; + const runtime = yield* RuntimeSupportService; + const registry = yield* RegistryService; + const sync = Effect.gen(function* () { + const state = yield* registry.list; + yield* runtime.ensure(state.registries.map((entry) => entry.path)); + yield* tools.sync; + if (environment.env.RIG_AGENT_SYNC !== "0") yield* agents.sync; + }).pipe(Effect.withSpan("CliGeneratedSyncOperationsService.sync")); + return { sync } as const; + }), +}) { + static readonly layer = Layer.effect( + CliGeneratedSyncOperationsService, + CliGeneratedSyncOperationsService.make, + ); +} + +export class CliGeneralOperationsService extends Context.Service< + CliGeneralOperationsService, + { + readonly version: Effect.Effect; + readonly defaultStatus: Effect.Effect; + readonly doctor: Effect.Effect; + readonly help: (target: string | undefined, fallback: string) => Effect.Effect; + readonly showConfig: Effect.Effect; + readonly showConfigPath: Effect.Effect; + readonly listRegistries: Effect.Effect; + readonly addRegistry: (path?: string) => Effect.Effect; + readonly removeRegistry: (path: string) => Effect.Effect; + readonly syncGeneratedFiles: Effect.Effect; + } +>()("@rendotdev/rig/application/CliGeneralOperationsService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const paths = yield* RigPathsConfigService; + const registry = yield* RegistryService; + const helpOperations = yield* CliHelpOperationsService; + const status = yield* CliStatusOperationsService; + const sync = yield* CliGeneratedSyncOperationsService; + const versionService = yield* CliVersionService; + const platform = yield* CliPlatformService; + const version = versionService.current.pipe( + Effect.withSpan("CliGeneralOperationsService.version"), + ); + const defaultStatus = status.defaultStatus.pipe( + Effect.withSpan("CliGeneralOperationsService.defaultStatus"), + ); + const doctor = status.doctor.pipe(Effect.withSpan("CliGeneralOperationsService.doctor")); + const help = Effect.fn("CliGeneralOperationsService.help")(function* ( + target: string | undefined, + fallback: string, + ) { + yield* helpOperations.help(target, fallback); + }); + const showConfig = config.ensure.pipe( + Effect.flatMap(platform.printJson), + Effect.withSpan("CliGeneralOperationsService.showConfig"), + ); + const showConfigPath = platform + .log(paths.configPath) + .pipe(Effect.withSpan("CliGeneralOperationsService.showConfigPath")); + const listRegistries = registry.list.pipe( + Effect.flatMap(platform.printJson), + Effect.withSpan("CliGeneralOperationsService.listRegistries"), + ); + const addRegistry = Effect.fn("CliGeneralOperationsService.addRegistry")(function* ( + path?: string, + ) { + yield* platform.printJson(yield* registry.add(path ?? (yield* platform.cwd))); + }); + const removeRegistry = Effect.fn("CliGeneralOperationsService.removeRegistry")(function* ( + path: string, + ) { + yield* platform.printJson(yield* registry.remove(path)); + }); + const syncGeneratedFiles = sync.sync.pipe( + Effect.withSpan("CliGeneralOperationsService.syncGeneratedFiles"), + ); + return { + version, + defaultStatus, + doctor, + help, + showConfig, + showConfigPath, + listRegistries, + addRegistry, + removeRegistry, + syncGeneratedFiles, + } as const; + }), +}) { + static readonly layer = Layer.effect( + CliGeneralOperationsService, + CliGeneralOperationsService.make, + ); +} + +const cliHelpOperationsLayer = CliHelpOperationsService.layer.pipe( + Layer.provide( + Layer.mergeAll(helpTopicsLayer, toolServicesLayer, cliVersionLayer, cliPlatformLayer), + ), +); +const cliStatusNoticeLayer = CliStatusNoticeService.layer.pipe( + Layer.provide(Layer.mergeAll(npmUpdateCheckLayer, toolServicesLayer, cliPlatformLayer)), +); +const cliToolDiscoveryLayer = toolDiscoveryLayer.pipe( + Layer.provide(Layer.merge(registryLayer, rigPathsLayer)), +); +const cliStatusOperationsLayer = CliStatusOperationsService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigConfigStoreLayer, + rigPathsConfigLayer, + registryLayer, + cliToolDiscoveryLayer, + rigLoggerLayer, + cliStatusNoticeLayer, + cliVersionLayer, + cliPlatformLayer, + ), + ), +); +const cliGeneratedSyncOperationsLayer = CliGeneratedSyncOperationsService.layer.pipe( + Layer.provide( + Layer.mergeAll( + CliEnvironmentConfigService.layer, + toolRuntimeInstructionSyncLayer, + agentInstructionSyncLayer, + registryLayer, + toolServicesLayer, + ), + ), +); +export const cliGeneralOperationsLayer = CliGeneralOperationsService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigConfigStoreLayer, + rigPathsConfigLayer, + registryLayer, + cliHelpOperationsLayer, + cliStatusOperationsLayer, + cliGeneratedSyncOperationsLayer, + cliVersionLayer, + cliPlatformLayer, + ), + ), +); diff --git a/src/app/cli/cli-platform.ts b/src/app/cli/cli-platform.ts new file mode 100644 index 0000000..81bd2a8 --- /dev/null +++ b/src/app/cli/cli-platform.ts @@ -0,0 +1,101 @@ +import { existsSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { + RigError, + RigErrorNormalizationService, + rigErrorNormalizationLayer, +} from "../../providers/errors/rig-error"; + +export class CliEnvironmentConfigService extends Context.Service< + CliEnvironmentConfigService, + { readonly env: NodeJS.ProcessEnv; readonly argv: readonly string[] } +>()("@rendotdev/rig/application/CliEnvironmentConfigService", { + make: Effect.sync(() => ({ env: process.env, argv: process.argv })), +}) { + static readonly layer = Layer.effect( + CliEnvironmentConfigService, + CliEnvironmentConfigService.make, + ); +} + +export class CliPlatformService extends Context.Service< + CliPlatformService, + { + readonly cwd: Effect.Effect; + readonly exists: (path: string) => Effect.Effect; + readonly readText: (path: string) => Effect.Effect; + readonly log: (value: string) => Effect.Effect; + readonly error: (value: string) => Effect.Effect; + readonly printJson: (value: unknown) => Effect.Effect; + readonly printQueryResult: (value: unknown) => Effect.Effect; + readonly setExitCode: (code: number) => Effect.Effect; + readonly fail: (cause: unknown) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliPlatformService", { + make: Effect.gen(function* () { + const errors = yield* RigErrorNormalizationService; + const toReadError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const cwd = Effect.sync(() => process.cwd()).pipe(Effect.withSpan("CliPlatformService.cwd")); + const exists = Effect.fn("CliPlatformService.exists")(function* (path: string) { + return existsSync(path); + }); + const readText = Effect.fn("CliPlatformService.readText")(function* (path: string) { + return yield* Effect.tryPromise({ try: () => readFile(path, "utf8"), catch: toReadError }); + }); + const log = Effect.fn("CliPlatformService.log")(function* (value: string) { + yield* Effect.sync(() => console.log(value)); + }); + const error = Effect.fn("CliPlatformService.error")(function* (value: string) { + yield* Effect.sync(() => console.error(value)); + }); + const printJson = Effect.fn("CliPlatformService.printJson")(function* (value: unknown) { + yield* log(JSON.stringify(value, null, 2)); + }); + const printQueryResult = Effect.fn("CliPlatformService.printQueryResult")(function* ( + value: unknown, + ) { + const isScalarValue = + typeof value === "string" || typeof value === "number" || typeof value === "boolean"; + if (isScalarValue) { + yield* log(String(value)); + return; + } + yield* printJson(value); + }); + const setExitCode = Effect.fn("CliPlatformService.setExitCode")(function* (code: number) { + yield* Effect.sync(() => { + process.exitCode = code; + }); + }); + const fail = Effect.fn("CliPlatformService.fail")(function* (cause: unknown) { + const rigError = yield* errors.normalize(cause); + yield* error(`${rigError.code}: ${rigError.message}`); + if (rigError.details !== undefined) yield* error(JSON.stringify(rigError.details, null, 2)); + yield* setExitCode(1); + }); + return { + cwd, + exists, + readText, + log, + error, + printJson, + printQueryResult, + setExitCode, + fail, + } as const; + }), +}) { + static readonly layer = Layer.effect(CliPlatformService, CliPlatformService.make); +} + +export const cliPlatformLayer = CliPlatformService.layer.pipe( + Layer.provide(rigErrorNormalizationLayer), +); diff --git a/src/app/cli/cli-tool-operations.ts b/src/app/cli/cli-tool-operations.ts new file mode 100644 index 0000000..1ec3a3e --- /dev/null +++ b/src/app/cli/cli-tool-operations.ts @@ -0,0 +1,188 @@ +import { Context, Effect, Layer } from "effect"; +import { ToolIdentifierService } from "../../domains/tools/service/tool-identifier"; +import { ToolRunnerService } from "../../domains/tools/runtime/tool-runner"; +import { ToolEnvService } from "../../domains/tools/runtime/tool-env"; +import { ToolFilesService } from "../../domains/tools/runtime/tool-files"; +import { ToolTypecheckService } from "../../domains/tools/runtime/tool-typecheck"; +import { ToolApiMigrationService } from "../../domains/tools/runtime/tool-api-migration"; +import { ToolFindService } from "../../domains/tools/runtime/tool-find"; +import { ToolInspectorService } from "../../domains/tools/runtime/tool-inspector"; +import { ToolListService } from "../../domains/tools/runtime/tool-list"; +import { toolServicesLayer } from "../../domains/tools/runtime/tool-services-layer"; +import { CliPlatformService, cliPlatformLayer } from "./cli-platform"; +import { pipelineInputLayer, PipelineInputService } from "./pipeline-context"; + +export type CliFindOptions = Readonly<{ limit?: string; tool?: string; json?: boolean }>; +export type CliRunOptions = Readonly>; + +export class CliToolReadOperationsService extends Context.Service< + CliToolReadOperationsService, + { + readonly list: (json: boolean) => Effect.Effect; + readonly find: (query: string, options: CliFindOptions) => Effect.Effect; + readonly inspect: (target: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolReadOperationsService", { + make: Effect.gen(function* () { + const platform = yield* CliPlatformService; + const listService = yield* ToolListService; + const findService = yield* ToolFindService; + const inspector = yield* ToolInspectorService; + const list = Effect.fn("CliToolReadOperationsService.list")(function* (json: boolean) { + const data = yield* listService.list(); + yield* json ? platform.printJson(data) : platform.log(yield* listService.renderPlain(data)); + }); + const find = Effect.fn("CliToolReadOperationsService.find")(function* ( + query: string, + options: CliFindOptions, + ) { + const data = yield* findService.find(query, options); + yield* options.json + ? platform.printJson(data) + : platform.log(yield* findService.render(data)); + }); + const inspect = Effect.fn("CliToolReadOperationsService.inspect")(function* (target: string) { + yield* platform.printJson(yield* inspector.inspect(target)); + }); + return { list, find, inspect } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolReadOperationsService, + CliToolReadOperationsService.make, + ); +} + +export class CliToolWriteOperationsService extends Context.Service< + CliToolWriteOperationsService, + { + readonly create: (name: string) => Effect.Effect; + readonly edit: (name: string) => Effect.Effect; + readonly remove: (name: string) => Effect.Effect; + readonly env: (target: string, assignments: string[]) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolWriteOperationsService", { + make: Effect.gen(function* () { + const platform = yield* CliPlatformService; + const files = yield* ToolFilesService; + const environment = yield* ToolEnvService; + const create = Effect.fn("CliToolWriteOperationsService.create")(function* (name: string) { + const result = yield* files.create(name); + yield* platform.log(`Created tool ${result.name}`); + yield* platform.log(`\nTool directory: ${result.toolDir}`); + yield* platform.log(`Tool file: ${result.toolPath}`); + yield* platform.log("\nFiles:"); + for (const file of result.files) yield* platform.log(` ${file}`); + yield* platform.log(`\nEdit: ${result.toolPath}`); + yield* platform.log("\nTry:"); + yield* platform.log(` rig help ${result.name}`); + yield* platform.log(` rig help ${result.id}`); + yield* platform.log(` rig run ${result.id} test`); + }); + const edit = Effect.fn("CliToolWriteOperationsService.edit")(function* (name: string) { + yield* platform.log((yield* files.path(name)).toolPath); + }); + const remove = Effect.fn("CliToolWriteOperationsService.remove")(function* (name: string) { + const result = yield* files.remove(name); + yield* platform.log(`Removed tool ${result.name}`); + yield* platform.log(`Tool directory: ${result.toolDir}`); + }); + const env = Effect.fn("CliToolWriteOperationsService.env")(function* ( + target: string, + assignments: string[], + ) { + yield* platform.printJson(yield* environment.configure(target, assignments)); + }); + return { create, edit, remove, env } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolWriteOperationsService, + CliToolWriteOperationsService.make, + ); +} + +export class CliToolExecutionOperationsService extends Context.Service< + CliToolExecutionOperationsService, + { + readonly run: ( + commandId: string, + args: string[], + options: CliRunOptions, + ) => Effect.Effect; + readonly typecheck: (tool?: string) => Effect.Effect; + readonly migrate: (json: boolean) => Effect.Effect; + } +>()("@rendotdev/rig/application/CliToolExecutionOperationsService", { + make: Effect.gen(function* () { + const platform = yield* CliPlatformService; + const runner = yield* ToolRunnerService; + const identifiers = yield* ToolIdentifierService; + const pipeline = yield* PipelineInputService; + const typechecker = yield* ToolTypecheckService; + const migrations = yield* ToolApiMigrationService; + const run = Effect.fn("CliToolExecutionOperationsService.run")(function* ( + commandId: string, + args: string[], + options: CliRunOptions, + ) { + const target = yield* identifiers.parseCommandTarget(commandId); + const pipeContext = options.pipe ? yield* pipeline.read : {}; + const result = yield* runner.run(target.tool, target.command, { + args, + input: options.input as string | undefined, + inputFile: options.inputFile as string | undefined, + dryRun: Boolean(options.dryRun), + pipeContext, + }); + const envelope = yield* pipeline.attach( + result.envelope, + pipeContext, + options.as as string | undefined, + ); + const query = options.query as string | undefined; + yield* query + ? platform.printQueryResult(yield* pipeline.query(envelope, query)) + : platform.printJson(envelope); + yield* platform.setExitCode(result.exitCode); + }); + const typecheck = Effect.fn("CliToolExecutionOperationsService.typecheck")(function* ( + tool?: string, + ) { + const result = yield* typechecker.typecheck(tool); + yield* platform.printJson(result); + yield* platform.setExitCode(result.exitCode); + }); + const migrate = Effect.fn("CliToolExecutionOperationsService.migrate")(function* ( + json: boolean, + ) { + const report = yield* migrations.inspect(); + yield* json ? platform.printJson(report) : platform.log(yield* migrations.renderCli(report)); + yield* platform.setExitCode(report.unsupported.length > 0 ? 2 : 0); + }); + return { run, typecheck, migrate } as const; + }), +}) { + static readonly layer = Layer.effect( + CliToolExecutionOperationsService, + CliToolExecutionOperationsService.make, + ); +} + +const cliToolReadDependenciesLayer = Layer.mergeAll(cliPlatformLayer, toolServicesLayer); +const cliToolWriteDependenciesLayer = Layer.mergeAll(cliPlatformLayer, toolServicesLayer); +const cliToolExecutionDependenciesLayer = Layer.mergeAll( + cliPlatformLayer, + pipelineInputLayer, + toolServicesLayer, +); + +export const cliToolReadOperationsLayer = CliToolReadOperationsService.layer.pipe( + Layer.provide(cliToolReadDependenciesLayer), +); +export const cliToolWriteOperationsLayer = CliToolWriteOperationsService.layer.pipe( + Layer.provide(cliToolWriteDependenciesLayer), +); +export const cliToolExecutionOperationsLayer = CliToolExecutionOperationsService.layer.pipe( + Layer.provide(cliToolExecutionDependenciesLayer), +); diff --git a/src/app/cli/cli-update-operations.ts b/src/app/cli/cli-update-operations.ts new file mode 100644 index 0000000..e05afe4 --- /dev/null +++ b/src/app/cli/cli-update-operations.ts @@ -0,0 +1,89 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../providers/errors/rig-error"; +import { RigUpdaterService, rigUpdaterLayer } from "../../domains/updates/runtime/rig-updater"; +import { + type CommandUiReporter, + CommandUiRendererService, + commandUiRendererLayer, +} from "./command-ui"; + +type CliUpdateResult = Readonly<{ status: "ready" | "skipped"; currentVersion: string }>; + +export class CliUpdateOperationsService extends Context.Service< + CliUpdateOperationsService, + { readonly update: Effect.Effect } +>()("@rendotdev/rig/application/CliUpdateOperationsService", { + make: Effect.gen(function* () { + const updater = yield* RigUpdaterService; + const ui = yield* CommandUiRendererService; + const execute = Effect.fn("CliUpdateOperationsService.execute")(function* ( + report: CommandUiReporter, + ) { + const currentVersion = yield* updater.currentVersion; + report.complete({ label: `Current version: ${currentVersion}` }); + report("Checking for updates"); + const plan = yield* updater.plan; + if (plan.status === "skipped") { + report.complete({ + label: "Update unavailable", + detail: ` ${plan.reason}`, + mutedDetail: true, + }); + return { status: "skipped" as const, currentVersion }; + } + if (plan.status === "ready") { + report.complete({ + label: `Update available: ${plan.currentVersion} to ${plan.latestVersion}`, + }); + report("Updating Rig"); + const result = yield* updater.update(plan); + if (result.status !== "updated") { + return yield* new RigError({ + code: "INTERNAL_ERROR", + message: "Rig did not complete the planned update.", + }); + } + report.complete({ + label: `Updated Rig: ${result.previousVersion} to ${result.version}`, + detail: yield* ui.formatUpdateCommandOutputGroups({ + steps: [result.step], + outputs: [result.output], + }), + mutedDetail: true, + }); + } else { + report.complete({ label: `Already up to date: ${plan.version}` }); + } + report("Synchronizing runtime and agent instructions"); + const sync = yield* updater.sync(plan); + report.complete({ + label: "Synchronized runtime and agent instructions", + detail: yield* ui.formatUpdateCommandOutputGroups({ + steps: [sync.step], + outputs: [sync.output], + }), + mutedDetail: true, + }); + return { status: "ready" as const, currentVersion }; + }); + const renderSuccess = (result: CliUpdateResult) => + result.status === "skipped" + ? `Rig ${result.currentVersion} was left unchanged.` + : "Rig is ready to use."; + const update = ui + .run({ + label: "Preparing Rig update", + successLabel: "Update finished", + execute, + renderSuccess, + }) + .pipe(Effect.asVoid, Effect.withSpan("CliUpdateOperationsService.update")); + return { update } as const; + }), +}) { + static readonly layer = Layer.effect(CliUpdateOperationsService, CliUpdateOperationsService.make); +} + +export const cliUpdateOperationsLayer = CliUpdateOperationsService.layer.pipe( + Layer.provide(Layer.merge(rigUpdaterLayer, commandUiRendererLayer)), +); diff --git a/src/app/cli/cli-version.test.ts b/src/app/cli/cli-version.test.ts new file mode 100644 index 0000000..06eca70 --- /dev/null +++ b/src/app/cli/cli-version.test.ts @@ -0,0 +1,39 @@ +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect } from "effect"; +import { afterEach, describe, expect, it } from "vite-plus/test"; +import { CliVersionService, cliVersionLayer } from "./cli-version"; + +const originalPackageRoot = process.env.RIG_PACKAGE_ROOT; +const temporaryDirectories: string[] = []; + +afterEach(async () => { + if (originalPackageRoot === undefined) delete process.env.RIG_PACKAGE_ROOT; + else process.env.RIG_PACKAGE_ROOT = originalPackageRoot; + await Promise.all( + temporaryDirectories.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); +}); + +const currentVersion = () => + Effect.runPromise( + CliVersionService.use((service) => service.current).pipe(Effect.provide(cliVersionLayer)), + ); + +describe("CliVersionService", () => { + it("reads the package version and falls back for invalid manifests", async () => { + process.env.RIG_PACKAGE_ROOT = process.cwd(); + const packageJson = JSON.parse(await readFile(join(process.cwd(), "package.json"), "utf8")) as { + version: string; + }; + expect(await currentVersion()).toBe(packageJson.version); + + const packageRoot = await mkdtemp(join(tmpdir(), "rig-version-")); + temporaryDirectories.push(packageRoot); + process.env.RIG_PACKAGE_ROOT = packageRoot; + expect(await currentVersion()).toBe("0.0.0"); + await writeFile(join(packageRoot, "package.json"), '{"version":123}\n', "utf8"); + expect(await currentVersion()).toBe("0.0.0"); + }); +}); diff --git a/src/app/cli/cli-version.ts b/src/app/cli/cli-version.ts new file mode 100644 index 0000000..e5350f7 --- /dev/null +++ b/src/app/cli/cli-version.ts @@ -0,0 +1,36 @@ +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { PackageRootService, packageRootLayer } from "../../providers/package/package-root"; +import { CliPlatformService, cliPlatformLayer } from "./cli-platform"; + +export class CliVersionService extends Context.Service< + CliVersionService, + { + readonly packageRoot: Effect.Effect; + readonly current: Effect.Effect; + } +>()("@rendotdev/rig/application/CliVersionService", { + make: Effect.gen(function* () { + const packageRootService = yield* PackageRootService; + const platform = yield* CliPlatformService; + const packageRoot = packageRootService + .find(import.meta.url) + .pipe(Effect.withSpan("CliVersionService.packageRoot")); + const current = Effect.gen(function* () { + const root = yield* packageRoot; + const source = yield* platform.readText(join(root, "package.json")); + const parsed = yield* Effect.try(() => JSON.parse(source) as { version?: unknown }); + return typeof parsed.version === "string" ? parsed.version : "0.0.0"; + }).pipe( + Effect.orElseSucceed(() => "0.0.0"), + Effect.withSpan("CliVersionService.current"), + ); + return { packageRoot, current } as const; + }), +}) { + static readonly layer = Layer.effect(CliVersionService, CliVersionService.make); +} + +export const cliVersionLayer = CliVersionService.layer.pipe( + Layer.provide(Layer.merge(packageRootLayer, cliPlatformLayer)), +); diff --git a/src/cli.test.ts b/src/app/cli/cli.test.ts similarity index 78% rename from src/cli.test.ts rename to src/app/cli/cli.test.ts index da60ec3..fc11cc0 100644 --- a/src/cli.test.ts +++ b/src/app/cli/cli.test.ts @@ -2,11 +2,13 @@ import { afterEach, describe, expect, test, vi } from "vite-plus/test"; import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { BunRuntimeBootstrapClass, CliApplicationClass, isCliEntrypoint } from "./cli"; -import { RigPathsClass } from "./config/paths"; -import { RegistryConfigServiceClass } from "./registry/registry"; -import { ToolCreatorClass } from "./tools/create"; +import { Effect, Layer } from "effect"; +import { CliCommandService, cliCommandLayer } from "./cli-application"; +import { CliPlatformService, cliPlatformLayer } from "./cli-platform"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, +} from "../../providers/paths/rig-paths"; class CliWorkspaceStore { private readonly paths: string[] = []; @@ -58,7 +60,14 @@ class CliHarness { if (this.updateCheck) delete process.env.RIG_UPDATE_CHECK; else process.env.RIG_UPDATE_CHECK = "0"; process.env.RIG_LOG = "0"; - await new CliApplicationClass().run(["node", "rig", ...args]); + const layer = cliCommandLayer.pipe( + Layer.provide(Layer.merge(RigPathsOptionsConfigService.layer, RigPathsPlatformService.layer)), + ); + await Effect.runPromise( + CliCommandService.use((service) => service.run(["node", "rig", ...args])).pipe( + Effect.provide(layer), + ), + ); return this.output; } @@ -94,10 +103,11 @@ describe("cli application", () => { test("prints update notices from default status", async () => { const home = await workspaces.create(); - const paths = new RigPathsClass({ homeDir: home }); - await mkdir(paths.rigDir, { recursive: true }); + const rigDirectory = join(home, "rig"); + const updateCheckCachePath = join(rigDirectory, "update-check.json"); + await mkdir(rigDirectory, { recursive: true }); await writeFile( - paths.updateCheckCachePath, + updateCheckCachePath, `${JSON.stringify({ checkedAt: Date.now(), latestVersion: "999.0.0" })}\n`, "utf8", ); @@ -239,7 +249,8 @@ describe("cli application", () => { test("reports versioned tool API migrations with an agent-ready prompt", async () => { const home = await workspaces.create(); const cli = new CliHarness(home); - await new ToolCreatorClass({ homeDir: home }).create("current"); + await cli.run(["create", "current"]); + cli.logs.length = 0; expect(await cli.run(["migrate"])).toContain("All tools use Rig tool API v2."); @@ -297,7 +308,6 @@ describe("cli application", () => { const project = join(home, "project"); await mkdir(join(project, ".git"), { recursive: true }); await writeFile(join(project, "AGENTS.md"), "# Agent notes\n", "utf8"); - await new RegistryConfigServiceClass({ homeDir: home }).add(join(project, "rig-tools")); process.chdir(project); const cli = new CliHarness(home, true); @@ -394,8 +404,9 @@ describe("cli application", () => { test("runs and typechecks tools", async () => { const home = await workspaces.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); const cli = new CliHarness(home); + await cli.run(["create", "sample"]); + cli.logs.length = 0; expect(await cli.run(["run", "sample.example", "--input", '{"text":"cli"}'])).toContain( '"text": "cli"', @@ -456,8 +467,9 @@ describe("cli application", () => { }, ); vi.stubGlobal("Bun", { ...(originalBun as Record), cron }); - await new ToolCreatorClass({ homeDir: home }).create("sample"); const cli = new CliHarness(home); + await cli.run(["create", "sample"]); + cli.logs.length = 0; expect(await cli.run(["cron", "list"])).toContain('"cronJobs": []'); expect( @@ -496,93 +508,12 @@ describe("cli application", () => { ); }); - test("resolves and runs the system Bun runtime bootstrap", async () => { - const home = await workspaces.create(); - const bunPath = join(home, "bin", "bun"); - const entrypoint = join(home, "dist", "rig.mjs"); - const calls: { command: string; args: string[]; env?: NodeJS.ProcessEnv }[] = []; - const spawn = ((command: string, args: string[], options: { env?: NodeJS.ProcessEnv }) => { - calls.push({ command, args, env: options.env }); - return { status: calls.length === 1 ? 7 : null }; - }) as never; - - const bootstrap = new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: { RIG_BUN_PATH: bunPath }, bunGlobal: () => undefined }, - ); - expect(bootstrap.resolveBunPath()).toBe(bunPath); - expect(bootstrap.shouldBootstrap()).toBe(true); - expect( - bootstrap.run({ - metaUrl: pathToFileURL(entrypoint).href, - argv: ["node", "rig", "list"], - }), - ).toBe(7); - expect(bootstrap.autoInstallFlag()).toBe("--install=fallback"); - expect(calls[0]).toMatchObject({ - command: bunPath, - args: ["--install=fallback", entrypoint, "list"], - env: { RIG_BUN_BOOTSTRAPPED: "1" }, - }); - expect(bootstrap.run({ metaUrl: pathToFileURL(entrypoint).href, argv: ["node", "rig"] })).toBe( - 1, - ); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: join(home, "missing") }, - { - spawn, - env: {}, - bunGlobal: () => undefined, - }, - ).run({ metaUrl: pathToFileURL(entrypoint).href, argv: ["node", "rig"] }), - ).toBe(1); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: { RIG_BUN_BOOTSTRAPPED: "1" }, bunGlobal: () => undefined }, - ).run({ metaUrl: pathToFileURL(entrypoint).href, argv: ["node", "rig"] }), - ).toBeUndefined(); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: { RIG_BUN_BOOTSTRAPPED: "1" }, bunGlobal: () => undefined }, - ).shouldBootstrap(), - ).toBe(false); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: { RIG_DISABLE_BUN_BOOTSTRAP: "1" }, bunGlobal: () => undefined }, - ).shouldBootstrap(), - ).toBe(false); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: {}, bunGlobal: () => ({ version: "test" }) }, - ).shouldBootstrap(), - ).toBe(false); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: { RIG_BUN_PATH: bunPath }, bunGlobal: () => undefined }, - ).resolveBunPath(), - ).toBe(bunPath); - expect( - new BunRuntimeBootstrapClass( - { packageRoot: home }, - { spawn, env: {}, bunGlobal: () => undefined }, - ).resolveBunPath(), - ).toBe("bun"); - expect( - new BunRuntimeBootstrapClass({ packageRoot: home }, { spawn, env: {} }).shouldBootstrap(), - ).toBe((globalThis as typeof globalThis & { Bun?: unknown }).Bun === undefined); - }); - test("prints queried run output and pipeline ids", async () => { const home = await workspaces.create(); const cli = new CliHarness(home); - await new ToolCreatorClass({ homeDir: home }).create("sample"); + await cli.run(["create", "sample"]); + cli.logs.length = 0; expect(await cli.run(["run", "sample.example", "text=hello", "--query", "data.text"])).toBe( "hello", @@ -600,54 +531,35 @@ describe("cli application", () => { expect(output).toContain('"text": "hello"'); }); - test("handles errors, entrypoint checks, and version fallbacks", async () => { + test("renders typed command failures through the CLI platform", async () => { const home = await workspaces.create(); const cli = new CliHarness(home); - vi.spyOn(process, "exit").mockImplementation(((code?: string | number | null) => { - throw new Error(`exit:${code}`); - }) as never); - await expect(cli.run(["help", "missing"])).rejects.toThrow("exit:1"); + await cli.run(["help", "missing"]); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("TOOL_NOT_FOUND: Tool not found: missing"); expect(cli.errorOutput).toContain('"name": "missing"'); - await expect(cli.run(["run", "sample", "x"])).rejects.toThrow("exit:1"); + await cli.run(["run", "sample", "x"]); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("Command id must use .: sample"); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await expect(cli.run(["run", "sample.example", "text=x", "--as", "bad.id"])).rejects.toThrow( - "exit:1", - ); + await cli.run(["create", "sample"]); + cli.logs.length = 0; + await cli.run(["run", "sample.example", "text=x", "--as", "bad.id"]); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("Pipeline id is invalid: bad.id"); - await expect( - cli.run(["run", "sample.example", "text=x", "--query", "data.missing"]), - ).rejects.toThrow("exit:1"); + await cli.run(["run", "sample.example", "text=x", "--query", "data.missing"]); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("Query is missing: data.missing"); - await expect( - cli.run(["run", "sample.example", "text=x", "--query", "data.text.extra"]), - ).rejects.toThrow("exit:1"); + await cli.run(["run", "sample.example", "text=x", "--query", "data.text.extra"]); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("Query cannot access: data.text.extra"); - const app = new CliApplicationClass() as unknown as { - printError(error: unknown): never; - version(): string; - }; - expect(() => app.printError(new Error("plain failure"))).toThrow("exit:1"); + await Effect.runPromise( + CliPlatformService.use((service) => service.fail(new Error("plain failure"))).pipe( + Effect.provide(cliPlatformLayer), + ), + ); + expect(process.exitCode).toBe(1); expect(cli.errorOutput).toContain("INTERNAL_ERROR: plain failure"); - - process.env.RIG_PACKAGE_ROOT = process.cwd(); - const packageJson = JSON.parse(await readFile(join(process.cwd(), "package.json"), "utf8")) as { - version: string; - }; - expect(app.version()).toBe(packageJson.version); - - const packageRoot = await workspaces.create(); - process.env.RIG_PACKAGE_ROOT = packageRoot; - expect(app.version()).toBe("0.0.0"); - await writeFile(join(packageRoot, "package.json"), '{"version":123}\n', "utf8"); - expect(app.version()).toBe("0.0.0"); - - const entrypoint = join(packageRoot, "cli.ts"); - expect(isCliEntrypoint(pathToFileURL(entrypoint).href, entrypoint)).toBe(true); - expect(isCliEntrypoint(import.meta.url, "")).toBe(false); - expect(isCliEntrypoint(import.meta.url, entrypoint)).toBe(false); }); }); diff --git a/src/app/cli/command-ui.test.tsx b/src/app/cli/command-ui.test.tsx new file mode 100644 index 0000000..6680672 --- /dev/null +++ b/src/app/cli/command-ui.test.tsx @@ -0,0 +1,301 @@ +import { cleanup, render } from "ink-testing-library"; +import { stripVTControlCharacters } from "node:util"; +import { Effect, Layer } from "effect"; +import type { ReactElement } from "react"; +import { afterEach, describe, expect, it } from "vite-plus/test"; +import { RigError } from "../../providers/errors/rig-error"; +import { + CommandUiComponent, + CommandUiInteractiveService, + CommandUiNonInteractiveService, + CommandUiPlatformService, + CommandUiRendererService, + commandUiRendererLayer, + type CommandUiRunOptions, +} from "./command-ui"; + +afterEach(cleanup); + +type RendererDependencies = Readonly<{ + isInteractive: boolean; + write: (value: string) => void; + render: (tree: ReactElement) => { + rerender: (tree: ReactElement) => void; + unmount: () => void; + waitUntilRenderFlush: () => Promise; + }; +}>; + +const rendererLayer = (dependencies: RendererDependencies) => { + const platformLayer = Layer.succeed(CommandUiPlatformService, { + isInteractive: Effect.succeed(dependencies.isInteractive), + write: Effect.fn("CommandUiPlatformService.write")(function* (value) { + dependencies.write(value); + }), + render: Effect.fn("CommandUiPlatformService.render")(function* (tree) { + return dependencies.render(tree); + }), + }); + const executionLayer = Layer.merge( + CommandUiInteractiveService.layer, + CommandUiNonInteractiveService.layer, + ).pipe(Layer.provide(platformLayer)); + return CommandUiRendererService.layer.pipe( + Layer.provide(Layer.merge(platformLayer, executionLayer)), + ); +}; + +const runRenderer = ( + dependencies: RendererDependencies, + params: CommandUiRunOptions, +) => + Effect.runPromise( + CommandUiRendererService.use((service) => service.run(params)).pipe( + Effect.provide(rendererLayer(dependencies)), + ), + ); + +class CommandUiTestRenderer { + public current?: ReactElement; + public readonly states: ReactElement[] = []; + public unmounted = false; + public flushed = false; + + public render(tree: ReactElement) { + this.capture(tree); + return { + rerender: (nextTree: ReactElement) => this.capture(nextTree), + unmount: () => { + this.unmounted = true; + }, + waitUntilRenderFlush: async () => { + this.flushed = true; + }, + }; + } + + public lastFrame(): string | undefined { + const frame = this.current ? render(this.current).lastFrame() : undefined; + return frame ? stripVTControlCharacters(frame) : undefined; + } + + private capture(tree: ReactElement): void { + this.current = tree; + this.states.push(tree); + } +} + +describe("CommandUiComponent", () => { + it("renders loading, success, and error states", () => { + const view = render(); + expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe("⠋ Updating Rig"); + + view.rerender( + , + ); + expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe("✔ Updated Rig\nRig is ready."); + + view.rerender( + , + ); + expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe( + "✖ Updating Rig\nRegistry failed.", + ); + }); + + it("renders completed items and hides an empty current state", () => { + const view = render( + , + ); + + expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe( + "✔ Checked version\n✔ Updated Rig\n npm install\n✔ Synchronized\nDone", + ); + }); +}); + +describe("CommandUiRenderer", () => { + it("prints one stable non-interactive result with completed details", async () => { + const writes: string[] = []; + const dependencies = { + isInteractive: false, + write(value: string) { + writes.push(String(value)); + }, + render() { + throw new Error("Non-interactive output must not mount Ink."); + }, + }; + + const result = await runRenderer(dependencies, { + label: "Preparing Rig update", + successLabel: "Update finished", + execute: (report) => + Effect.sync(() => { + report("Checking for updates"); + report.complete({ label: "Current version: 0.0.41" }); + report.complete({ + label: "Updated Rig", + detail: " npm install\n changed 1 package", + mutedDetail: true, + }); + return "ready"; + }), + renderSuccess(value) { + return value === "ready" ? "Rig is ready." : ""; + }, + }); + + expect(result).toBe("ready"); + expect(writes).toEqual([ + [ + "✔ Current version: 0.0.41", + "✔ Updated Rig", + " npm install", + " changed 1 package", + "✔ Update finished", + "Rig is ready.", + "", + ].join("\n"), + ]); + }); + + it("uses the initial label and omits an empty success detail", async () => { + const writes: string[] = []; + const dependencies = { + isInteractive: false, + write(value: string) { + writes.push(String(value)); + }, + render() { + throw new Error("Non-interactive output must not mount Ink."); + }, + }; + + await runRenderer(dependencies, { + label: "Checking Rig", + execute: () => Effect.void, + renderSuccess() { + return ""; + }, + }); + + expect(writes).toEqual(["✔ Checking Rig\n"]); + }); + + it("renders interactive progress and success", async () => { + const TestRenderer = new CommandUiTestRenderer(); + const dependencies = { + isInteractive: true, + write: () => undefined, + render: (tree: ReactElement) => TestRenderer.render(tree), + }; + + const result = await runRenderer(dependencies, { + label: "Preparing Rig update", + successLabel: "Update finished", + execute: (report) => + Effect.sync(() => { + report("Checking for updates"); + report.complete({ label: "Checked for updates" }); + return 42; + }), + renderSuccess(value) { + return `Result: ${value}`; + }, + }); + + expect(result).toBe(42); + expect(TestRenderer.states).toHaveLength(4); + expect(TestRenderer.lastFrame()).toBe("✔ Checked for updates\n✔ Update finished\nResult: 42"); + expect(TestRenderer.flushed).toBe(true); + expect(TestRenderer.unmounted).toBe(true); + }); + + it("renders Error failures against the active label", async () => { + const TestRenderer = new CommandUiTestRenderer(); + const dependencies = { + isInteractive: true, + write: () => undefined, + render: (tree: ReactElement) => TestRenderer.render(tree), + }; + + await expect( + runRenderer(dependencies, { + label: "Preparing Rig update", + execute: (report) => + Effect.sync(() => report("Updating Rig")).pipe( + Effect.andThen( + Effect.fail( + new RigError({ code: "INTERNAL_ERROR", message: "Registry unavailable" }), + ), + ), + ), + renderSuccess() { + return "unreachable"; + }, + }), + ).rejects.toThrow("Registry unavailable"); + + expect(TestRenderer.lastFrame()).toBe("✖ Updating Rig\nRegistry unavailable"); + expect(TestRenderer.unmounted).toBe(true); + }); + + it("renders non-Error failures against the initial label after completion", async () => { + const TestRenderer = new CommandUiTestRenderer(); + const dependencies = { + isInteractive: true, + write: () => undefined, + render: (tree: ReactElement) => TestRenderer.render(tree), + }; + + await expect( + runRenderer(dependencies, { + label: "Preparing Rig update", + execute: (report) => + Effect.sync(() => report.complete({ label: "Checked version" })).pipe( + Effect.andThen(Effect.fail("offline")), + ), + renderSuccess() { + return "unreachable"; + }, + }), + ).rejects.toBe("offline"); + + expect(TestRenderer.lastFrame()).toBe("✔ Checked version\n✖ Preparing Rig update\noffline"); + }); + + it("formats subprocess output and omits empty groups", async () => { + expect( + await Effect.runPromise( + CommandUiRendererService.use((service) => + service.formatUpdateCommandOutputGroups({ + steps: [ + { command: "/runtime/bin/npm", args: ["install", "@rendotdev/rig"] }, + { command: "/runtime/bin/rig", args: ["init"] }, + ], + outputs: ["changed 1 package\nready\n", ""], + }), + ).pipe(Effect.provide(commandUiRendererLayer)), + ), + ).toBe(" npm install @rendotdev/rig\n changed 1 package\n ready"); + expect( + await Effect.runPromise( + CommandUiRendererService.use((service) => + service.formatUpdateCommandOutputGroups({ + steps: [{ command: "rig", args: ["init"] }], + outputs: [], + }), + ).pipe(Effect.provide(commandUiRendererLayer)), + ), + ).toBeUndefined(); + }); +}); diff --git a/src/app/cli/command-ui.tsx b/src/app/cli/command-ui.tsx new file mode 100644 index 0000000..ed22ce8 --- /dev/null +++ b/src/app/cli/command-ui.tsx @@ -0,0 +1,314 @@ +import { basename } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; +import { Text, render as renderInk, useAnimation, type Instance } from "ink"; +import type { ReactElement } from "react"; +import type { RigUpdateStep } from "../../domains/updates/types/updates"; +import { TerminalColors, TerminalIcons } from "./terminal-theme"; + +type CommandUiState = "loading" | "success" | "error"; +type CommandUiCompletedItem = { label: string; detail?: string; mutedDetail?: boolean }; + +export type CommandUiReporter = ((label: string) => void) & { + complete: (params: CommandUiCompletedItem) => void; +}; + +type CommandUiProps = { + state: CommandUiState; + label: string; + detail?: string; + completed?: CommandUiCompletedItem[]; +}; + +export function CommandUiComponent(props: CommandUiProps) { + const { frame } = useAnimation({ interval: 80, isActive: props.state === "loading" }); + const symbol = + props.state === "loading" + ? TerminalIcons.loading({ frame }) + : props.state === "success" + ? TerminalIcons.success + : TerminalIcons.error; + const color = + props.state === "success" + ? TerminalColors.success + : props.state === "error" + ? TerminalColors.error + : TerminalColors.loading; + const hasCompleted = Boolean(props.completed?.length); + const current = props.label + ? `${symbol} ${props.label}${props.detail ? `\n${props.detail}` : ""}` + : ""; + + return ( + + {props.completed?.map((item, index) => ( + + {index > 0 ? "\n" : ""} + + {TerminalIcons.success} {item.label} + + {item.detail ? ( + + {`\n${item.detail}`} + + ) : null} + + ))} + {hasCompleted && current ? "\n" : ""} + {current ? {current} : null} + + ); +} + +type CommandUiInstance = Pick; + +export class CommandUiError extends Schema.TaggedErrorClass()("CommandUiError", { + cause: Schema.Defect(), +}) {} + +export type CommandUiRunOptions = { + label: string; + successLabel?: string; + execute: (report: CommandUiReporter) => Effect.Effect; + renderSuccess: (result: Result) => string; +}; + +export class CommandUiPlatformService extends Context.Service< + CommandUiPlatformService, + { + readonly isInteractive: Effect.Effect; + readonly write: (value: string) => Effect.Effect; + readonly render: (tree: ReactElement) => Effect.Effect; + } +>()("@rendotdev/rig/application/CommandUiPlatformService", { + make: Effect.gen(function* () { + const isInteractive = Effect.sync(() => Boolean(process.stdout.isTTY)).pipe( + Effect.withSpan("CommandUiPlatformService.isInteractive"), + ); + const write = Effect.fn("CommandUiPlatformService.write")(function* (value: string) { + yield* Effect.try({ + try: () => { + process.stdout.write(value); + }, + catch: (cause) => new CommandUiError({ cause }), + }); + }); + const render = Effect.fn("CommandUiPlatformService.render")(function* (tree: ReactElement) { + return yield* Effect.try({ + try: () => renderInk(tree, { stdout: process.stdout, patchConsole: false }), + catch: (cause) => new CommandUiError({ cause }), + }); + }); + return { isInteractive, write, render } as const; + }), +}) { + static readonly layer = Layer.effect(CommandUiPlatformService, CommandUiPlatformService.make); +} + +export class CommandUiNonInteractiveService extends Context.Service< + CommandUiNonInteractiveService, + { + readonly run: ( + params: CommandUiRunOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/application/CommandUiNonInteractiveService", { + make: Effect.gen(function* () { + const platform = yield* CommandUiPlatformService; + const createReporter = (params: { + onUpdate: (label: string) => void; + onComplete: (item: CommandUiCompletedItem) => void; + }): CommandUiReporter => { + const report = ((label: string) => params.onUpdate(label)) as CommandUiReporter; + report.complete = (item) => params.onComplete(item); + return report; + }; + + const formatCompleted = (completed: CommandUiCompletedItem[]) => + completed + .map( + (item) => + `${TerminalIcons.success} ${item.label}${item.detail ? `\n${item.detail}` : ""}`, + ) + .join("\n"); + + const run = Effect.fn("CommandUiNonInteractiveService.run")(function* ( + params: CommandUiRunOptions, + ) { + const completed: CommandUiCompletedItem[] = []; + const reporter = createReporter({ + onUpdate: () => undefined, + onComplete: (item) => completed.push(item), + }); + const result = yield* params.execute(reporter); + const output = [ + formatCompleted(completed), + `${TerminalIcons.success} ${params.successLabel ?? params.label}`, + params.renderSuccess(result), + ] + .filter(Boolean) + .join("\n"); + yield* platform.write(`${output}\n`); + return result; + }); + + return { run } as const; + }), +}) { + static readonly layer = Layer.effect( + CommandUiNonInteractiveService, + CommandUiNonInteractiveService.make, + ); +} + +export class CommandUiInteractiveService extends Context.Service< + CommandUiInteractiveService, + { + readonly run: ( + params: CommandUiRunOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/application/CommandUiInteractiveService", { + make: Effect.gen(function* () { + const platform = yield* CommandUiPlatformService; + const createReporter = (params: { + onUpdate: (label: string) => void; + onComplete: (item: CommandUiCompletedItem) => void; + }): CommandUiReporter => { + const report = ((label: string) => params.onUpdate(label)) as CommandUiReporter; + report.complete = (item) => params.onComplete(item); + return report; + }; + + const run = Effect.fn("CommandUiInteractiveService.run")(function* ( + params: CommandUiRunOptions, + ) { + const completed: CommandUiCompletedItem[] = []; + const instance = yield* platform.render( + , + ); + let currentLabel = params.label; + const reporter = createReporter({ + onUpdate: (label) => { + currentLabel = label; + instance.rerender( + , + ); + }, + onComplete: (item) => { + completed.push(item); + currentLabel = ""; + instance.rerender(); + }, + }); + return yield* params.execute(reporter).pipe( + Effect.tap((result) => + Effect.tryPromise({ + try: async () => { + instance.rerender( + , + ); + await instance.waitUntilRenderFlush(); + }, + catch: (cause) => new CommandUiError({ cause }), + }), + ), + Effect.tapError((error) => + Effect.tryPromise({ + try: async () => { + instance.rerender( + , + ); + await instance.waitUntilRenderFlush(); + }, + catch: (cause) => new CommandUiError({ cause }), + }), + ), + Effect.ensuring(Effect.sync(() => instance.unmount())), + ); + }); + + return { run } as const; + }), +}) { + static readonly layer = Layer.effect( + CommandUiInteractiveService, + CommandUiInteractiveService.make, + ); +} + +export class CommandUiRendererService extends Context.Service< + CommandUiRendererService, + { + readonly run: ( + params: CommandUiRunOptions, + ) => Effect.Effect; + readonly formatUpdateCommandOutputGroups: (params: { + steps: RigUpdateStep[]; + outputs: string[]; + }) => Effect.Effect; + } +>()("@rendotdev/rig/application/CommandUiRendererService", { + make: Effect.gen(function* () { + const platform = yield* CommandUiPlatformService; + const interactive = yield* CommandUiInteractiveService; + const nonInteractive = yield* CommandUiNonInteractiveService; + + const renderUpdateOutput = (params: { + steps: RigUpdateStep[]; + outputs: string[]; + }): string | undefined => { + const groups = params.steps.flatMap((step, index) => { + const output = params.outputs[index]?.trim(); + if (!output) return []; + const command = [basename(step.command), ...step.args].join(" "); + return [` ${command}`, ...output.split("\n").map((line) => ` ${line}`)]; + }); + return groups.length > 0 ? groups.join("\n") : undefined; + }; + + const formatUpdateCommandOutputGroups = Effect.fn( + "CommandUiRendererService.formatUpdateCommandOutputGroups", + )(function* (params: { steps: RigUpdateStep[]; outputs: string[] }) { + return renderUpdateOutput(params); + }); + + const run = Effect.fn("CommandUiRendererService.run")(function* ( + params: CommandUiRunOptions, + ) { + return (yield* platform.isInteractive) + ? yield* interactive.run(params) + : yield* nonInteractive.run(params); + }); + + return { run, formatUpdateCommandOutputGroups } as const; + }), +}) { + static readonly layer = Layer.effect(CommandUiRendererService, CommandUiRendererService.make); +} + +const commandUiExecutionLayer = Layer.merge( + CommandUiInteractiveService.layer, + CommandUiNonInteractiveService.layer, +).pipe(Layer.provide(CommandUiPlatformService.layer)); + +export const commandUiRendererLayer = CommandUiRendererService.layer.pipe( + Layer.provide(Layer.merge(CommandUiPlatformService.layer, commandUiExecutionLayer)), +); diff --git a/src/app/cli/composition-root.test.ts b/src/app/cli/composition-root.test.ts new file mode 100644 index 0000000..bc31356 --- /dev/null +++ b/src/app/cli/composition-root.test.ts @@ -0,0 +1,99 @@ +import { Effect, Layer } from "effect"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { CliCommandService } from "./cli-application"; +import { CliPlatformService } from "./cli-platform"; +import { CliApplicationService, runCli } from "./composition-root"; +import { RuntimeBootstrapService } from "./runtime-bootstrap"; + +const applicationLayer = (params: { + bootstrap: (input: { metaUrl: string; argv: string[] }) => Effect.Effect; + command: (argv: string[]) => Effect.Effect; + setExitCode?: (code: number) => Effect.Effect; +}) => { + const bootstrapLayer = Layer.succeed(RuntimeBootstrapService, { + run: Effect.fn("RuntimeBootstrapService.run")(params.bootstrap), + shouldBootstrap: Effect.succeed(false), + resolveBunPath: Effect.succeed("bun"), + autoInstallFlag: Effect.succeed("--install=fallback"), + isEntrypoint: Effect.fn("RuntimeBootstrapService.isEntrypoint")(function* () { + return true; + }), + }); + const commandLayer = Layer.succeed(CliCommandService, { + run: Effect.fn("CliCommandService.run")(params.command), + }); + const platformLayer = Layer.succeed(CliPlatformService, { + cwd: Effect.succeed("/workspace"), + exists: Effect.fn("CliPlatformService.exists")(function* () { + return true; + }), + readText: Effect.fn("CliPlatformService.readText")(function* () { + return ""; + }), + log: Effect.fn("CliPlatformService.log")(function* () {}), + error: Effect.fn("CliPlatformService.error")(function* () {}), + printJson: Effect.fn("CliPlatformService.printJson")(function* () {}), + printQueryResult: Effect.fn("CliPlatformService.printQueryResult")(function* () {}), + setExitCode: params.setExitCode ?? Effect.fn("CliPlatformService.setExitCode")(function* () {}), + fail: Effect.fn("CliPlatformService.fail")(function* () { + return yield* Effect.die("unexpected failure"); + }), + }); + return CliApplicationService.layer.pipe( + Layer.provide(Layer.mergeAll(bootstrapLayer, commandLayer, platformLayer)), + ); +}; + +describe("CLI composition root", () => { + it("runs the application when Bun bootstrap is unnecessary", async () => { + // @effect-diagnostics-next-line effectSucceedWithVoid:off + const bootstrap = vi.fn(() => Effect.succeed(undefined)); + const command = vi.fn(() => Effect.void); + const params = { metaUrl: "file:///rig.ts", argv: ["bun", "rig", "list"] }; + + await Effect.runPromise( + runCli(params).pipe(Effect.provide(applicationLayer({ bootstrap, command }))), + ); + + expect(bootstrap).toHaveBeenCalledWith(params); + expect(command).toHaveBeenCalledWith(params.argv); + }); + + it("exits with the bootstrap result before running the application", async () => { + const command = vi.fn(() => Effect.void); + const setExitCode = vi.fn(() => Effect.void); + const effect = runCli({ metaUrl: "file:///rig.ts", argv: ["node", "rig"] }).pipe( + Effect.provide( + applicationLayer({ bootstrap: () => Effect.succeed(7), command, setExitCode }), + ), + ); + + await Effect.runPromise(effect); + expect(setExitCode).toHaveBeenCalledWith(7); + expect(command).not.toHaveBeenCalled(); + }); + + it("uses the injected entrypoint matcher", async () => { + const layer = Layer.succeed(RuntimeBootstrapService, { + run: Effect.fn("RuntimeBootstrapService.run")(function* () { + return undefined; + }), + shouldBootstrap: Effect.succeed(false), + resolveBunPath: Effect.succeed("bun"), + autoInstallFlag: Effect.succeed("--install=fallback"), + isEntrypoint: Effect.fn("RuntimeBootstrapService.isEntrypoint")( + function* (metaUrl, argvPath) { + return metaUrl === `file://${argvPath}`; + }, + ), + }); + + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => + service.isEntrypoint("file:///rig.ts", "/rig.ts"), + ).pipe(Effect.provide(layer)), + ), + ).toBe(true); + }); +}); diff --git a/src/app/cli/composition-root.ts b/src/app/cli/composition-root.ts new file mode 100644 index 0000000..57c830c --- /dev/null +++ b/src/app/cli/composition-root.ts @@ -0,0 +1,60 @@ +import { Context, Effect, Layer } from "effect"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, +} from "../../providers/paths/rig-paths"; +import { RigError } from "../../providers/errors/rig-error"; +import { CliCommandService, cliCommandLayer } from "./cli-application"; +import { CliPlatformService, cliPlatformLayer } from "./cli-platform"; +import { RuntimeBootstrapService, runtimeBootstrapLayer } from "./runtime-bootstrap"; + +export type CliCompositionRootParams = Readonly<{ + metaUrl: string; + argv: string[]; +}>; + +export class CliApplicationService extends Context.Service< + CliApplicationService, + { readonly run: (params: CliCompositionRootParams) => Effect.Effect } +>()("@rendotdev/rig/application/CliApplicationService", { + make: Effect.gen(function* () { + const runtimeBootstrap = yield* RuntimeBootstrapService; + const command = yield* CliCommandService; + const platform = yield* CliPlatformService; + const run = Effect.fn("CliApplicationService.run")(function* ( + params: CliCompositionRootParams, + ) { + const bootstrapCode = yield* runtimeBootstrap.run(params); + if (bootstrapCode !== undefined) return yield* platform.setExitCode(bootstrapCode); + return yield* command.run(params.argv); + }); + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(CliApplicationService, CliApplicationService.make); +} + +const cliApplicationDependenciesLayer = Layer.mergeAll( + runtimeBootstrapLayer, + cliCommandLayer, + cliPlatformLayer, +); +const cliPathEnvironmentLayer = Layer.merge( + RigPathsOptionsConfigService.layer, + RigPathsPlatformService.layer, +); +const cliApplicationLayer = CliApplicationService.layer.pipe( + Layer.provide(cliApplicationDependenciesLayer), + Layer.provide(cliPathEnvironmentLayer), +); + +export const runCli = Effect.fn("CliApplicationService.runCli")(function* ( + params: CliCompositionRootParams, +) { + const application = yield* CliApplicationService; + yield* application.run(params); +}); + +export function runCliMain(params: CliCompositionRootParams): Promise { + return Effect.runPromise(runCli(params).pipe(Effect.provide(cliApplicationLayer))); +} diff --git a/src/app/cli/entrypoint.ts b/src/app/cli/entrypoint.ts new file mode 100644 index 0000000..6fd2ade --- /dev/null +++ b/src/app/cli/entrypoint.ts @@ -0,0 +1,16 @@ +#!/usr/bin/env bun +import { existsSync, realpathSync } from "node:fs"; +import { pathToFileURL } from "node:url"; +import { runCliMain } from "./composition-root"; + +const entrypointPath = process.argv[1]; +const isEntrypoint = Boolean( + import.meta.main || + (entrypointPath && + existsSync(entrypointPath) && + import.meta.url === pathToFileURL(realpathSync(entrypointPath)).href), +); +/* v8 ignore next 3 */ +if (isEntrypoint) { + await runCliMain({ metaUrl: import.meta.url, argv: process.argv }); +} diff --git a/src/app/cli/module-entrypoint.ts b/src/app/cli/module-entrypoint.ts new file mode 100644 index 0000000..6fd2ade --- /dev/null +++ b/src/app/cli/module-entrypoint.ts @@ -0,0 +1,16 @@ +#!/usr/bin/env bun +import { existsSync, realpathSync } from "node:fs"; +import { pathToFileURL } from "node:url"; +import { runCliMain } from "./composition-root"; + +const entrypointPath = process.argv[1]; +const isEntrypoint = Boolean( + import.meta.main || + (entrypointPath && + existsSync(entrypointPath) && + import.meta.url === pathToFileURL(realpathSync(entrypointPath)).href), +); +/* v8 ignore next 3 */ +if (isEntrypoint) { + await runCliMain({ metaUrl: import.meta.url, argv: process.argv }); +} diff --git a/src/app/cli/pipeline-context.test.ts b/src/app/cli/pipeline-context.test.ts new file mode 100644 index 0000000..2eb78c0 --- /dev/null +++ b/src/app/cli/pipeline-context.test.ts @@ -0,0 +1,90 @@ +import { Effect, Layer } from "effect"; +import { describe, expect, it } from "vite-plus/test"; +import { PipelineInputPlatformService, PipelineInputService } from "./pipeline-context"; + +const inputLayer = (text: string, parseJson: (text: string) => unknown = JSON.parse) => + PipelineInputService.layer.pipe( + Layer.provide( + Layer.succeed(PipelineInputPlatformService, { + readStdin: Effect.succeed(text), + parseJson: Effect.fn("PipelineInputPlatformService.parseJson")(function* (value) { + return parseJson(value); + }), + }), + ), + ); + +const runInput = ( + text: string, + use: (service: PipelineInputService["Service"]) => Effect.Effect, +) => Effect.runPromise(PipelineInputService.use(use).pipe(Effect.provide(inputLayer(text)))); + +describe("pipeline context", () => { + it("reads pipeline context and previous data from stdin", async () => { + await expect( + runInput( + JSON.stringify({ pipe: { first: 1 }, data: { value: 2 } }), + (service) => service.read, + ), + ).resolves.toEqual({ first: 1, prev: { value: 2 } }); + }); + + it("handles envelopes without inherited pipeline context", async () => { + await expect( + runInput(JSON.stringify({ data: null }), (service) => service.read), + ).resolves.toEqual({ + prev: null, + }); + await expect( + runInput(JSON.stringify({ pipe: "invalid", data: 2 }), (service) => service.read), + ).resolves.toEqual({ prev: 2 }); + await expect(runInput("null", (service) => service.read)).rejects.toThrow( + "Query cannot access: data", + ); + }); + + it("returns an empty context for empty stdin", async () => { + const layer = PipelineInputService.layer.pipe( + Layer.provide( + Layer.succeed(PipelineInputPlatformService, { + readStdin: Effect.succeed(" "), + parseJson: () => Effect.die("must not parse"), + }), + ), + ); + await expect( + Effect.runPromise( + PipelineInputService.use((service) => service.read).pipe(Effect.provide(layer)), + ), + ).resolves.toEqual({}); + }); + + it("queries object and array paths and reports inaccessible values", async () => { + await expect( + runInput("", (service) => service.query({ items: [{ name: "rig" }] }, "items.0.name")), + ).resolves.toBe("rig"); + await expect(runInput("", (service) => service.query("unchanged", ""))).resolves.toBe( + "unchanged", + ); + await expect( + runInput("", (service) => service.query({ item: null }, "item.name")), + ).rejects.toThrow("Query cannot access: item.name"); + await expect( + runInput("", (service) => service.query({ item: {} }, "item.name")), + ).rejects.toThrow("Query is missing: item.name"); + }); + + it("adds output IDs while preserving existing context", async () => { + const envelope = { data: { ok: true }, errors: [] }; + await expect( + runInput("", (service) => service.attach(envelope, { previous: 1 }, "step_2")), + ).resolves.toEqual({ ...envelope, pipe: { previous: 1, step_2: { ok: true } } }); + await expect(runInput("", (service) => service.attach(envelope, {}))).resolves.toBe(envelope); + await expect(runInput("", (service) => service.attach("value", {}, "step"))).resolves.toBe( + "value", + ); + await expect(runInput("", (service) => service.attach(envelope, {}, "bad.id"))).rejects.toThrow( + "Pipeline id is invalid: bad.id", + ); + }); +}); diff --git a/src/app/cli/pipeline-context.ts b/src/app/cli/pipeline-context.ts new file mode 100644 index 0000000..acf2dde --- /dev/null +++ b/src/app/cli/pipeline-context.ts @@ -0,0 +1,137 @@ +import { readFileSync } from "node:fs"; +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigError } from "../../providers/errors/rig-error"; + +export class PipelineInputPlatformService extends Context.Service< + PipelineInputPlatformService, + { + readonly readStdin: Effect.Effect; + readonly parseJson: (text: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/PipelineInputPlatformService", { + make: Effect.gen(function* () { + const normalizeError = (cause: unknown): RigError => { + if (cause instanceof RigError) return cause; + return new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + }; + const readStdin = Effect.try({ + /* v8 ignore next -- production stdin is exercised by CLI integration */ + try: () => readFileSync(0, "utf8"), + catch: normalizeError, + }).pipe(Effect.withSpan("PipelineInputPlatformService.readStdin")); + const parseJson = Effect.fn("PipelineInputPlatformService.parseJson")(function* (text: string) { + return yield* Effect.try({ try: () => JSON.parse(text) as unknown, catch: normalizeError }); + }); + return { readStdin, parseJson } as const; + }), +}) { + static readonly layer = Layer.effect( + PipelineInputPlatformService, + PipelineInputPlatformService.make, + ); +} + +export class PipelineInputService extends Context.Service< + PipelineInputService, + { + readonly read: Effect.Effect, RigError>; + readonly query: (value: unknown, path: string) => Effect.Effect; + readonly attach: ( + envelope: unknown, + context: Record, + id?: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/application/PipelineInputService", { + make: Effect.gen(function* () { + const platform = yield* PipelineInputPlatformService; + const normalizeError = (cause: unknown): RigError => { + if (cause instanceof RigError) return cause; + return new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + }; + + const queryValue = (value: unknown, path: string): unknown => { + let current = value; + for (const part of path.split(".").filter(Boolean)) { + const cannotReadProperty = !Predicate.isObject(current) && !Array.isArray(current); + if (cannotReadProperty) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Query cannot access: ${path}`, + details: { path, missing: part }, + }); + } + current = (current as Record)[part]; + if (current === undefined) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Query is missing: ${path}`, + details: { path, missing: part }, + }); + } + } + return current; + }; + + const attachValue = ( + envelope: unknown, + context: Record, + id?: string, + ): unknown => { + if (!id) return envelope; + if (!/^[A-Za-z0-9_-]+$/.test(id)) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Pipeline id is invalid: ${id}`, + details: { id }, + }); + } + if (!Predicate.isObject(envelope)) return envelope; + return { ...envelope, pipe: { ...context, [id]: envelope.data } }; + }; + + const read = Effect.gen(function* () { + const text = (yield* platform.readStdin).trim(); + if (!text) return {}; + const envelope = yield* platform.parseJson(text); + const context = + Predicate.isObject(envelope) && Predicate.isObject(envelope.pipe) + ? { ...envelope.pipe } + : {}; + context.prev = yield* Effect.try({ + try: () => queryValue(envelope, "data"), + catch: normalizeError, + }); + return context; + }).pipe(Effect.withSpan("PipelineInputService.read")); + + const query = Effect.fn("PipelineInputService.query")(function* (value: unknown, path: string) { + return yield* Effect.try({ try: () => queryValue(value, path), catch: normalizeError }); + }); + + const attach = Effect.fn("PipelineInputService.attach")(function* ( + envelope: unknown, + context: Record, + id?: string, + ) { + return yield* Effect.try({ + try: () => attachValue(envelope, context, id), + catch: normalizeError, + }); + }); + + return { read, query, attach } as const; + }), +}) { + static readonly layer = Layer.effect(PipelineInputService, PipelineInputService.make); +} + +export const pipelineInputLayer = PipelineInputService.layer.pipe( + Layer.provide(PipelineInputPlatformService.layer), +); diff --git a/src/app/cli/runtime-bootstrap.test.ts b/src/app/cli/runtime-bootstrap.test.ts new file mode 100644 index 0000000..df63022 --- /dev/null +++ b/src/app/cli/runtime-bootstrap.test.ts @@ -0,0 +1,139 @@ +import { pathToFileURL } from "node:url"; +import { Effect, Layer } from "effect"; +import { describe, expect, it } from "vite-plus/test"; +import { + RuntimeBootstrapConfigService, + RuntimeBootstrapPlatformService, + RuntimeBootstrapService, +} from "./runtime-bootstrap"; + +const runtimeBootstrapLayerFor = (params: { + env?: NodeJS.ProcessEnv; + bunGlobal?: unknown; + spawn?: ( + command: string, + args: string[], + options: { env?: NodeJS.ProcessEnv }, + ) => { + status: number | null; + }; +}) => { + const configLayer = Layer.succeed(RuntimeBootstrapConfigService, { env: params.env ?? {} }); + const platformLayer = Layer.succeed(RuntimeBootstrapPlatformService, { + spawn: Effect.fn("RuntimeBootstrapPlatformService.spawn")(function* (command, args, options) { + return (params.spawn?.(command, args, options) ?? { status: 0 }) as never; + }), + bunGlobal: Effect.succeed(params.bunGlobal), + realpath: Effect.fn("RuntimeBootstrapPlatformService.realpath")(function* (path) { + return path; + }), + pathToFileUrl: Effect.fn("RuntimeBootstrapPlatformService.pathToFileUrl")(function* (path) { + return pathToFileURL(path); + }), + }); + return RuntimeBootstrapService.layer.pipe(Layer.provide(Layer.merge(configLayer, platformLayer))); +}; + +describe("RuntimeBootstrapService", () => { + it("runs Bun with the configured executable and bootstrap guard", async () => { + const calls: { command: string; args: string[]; env?: NodeJS.ProcessEnv }[] = []; + const entrypoint = "/workspace/dist/rig.mjs"; + const layer = runtimeBootstrapLayerFor({ + env: { RIG_BUN_PATH: "/runtime/bun" }, + bunGlobal: undefined, + spawn: (command, args, options) => { + calls.push({ command, args, env: options.env }); + return { status: calls.length === 1 ? 7 : null }; + }, + }); + const first = await Effect.runPromise( + RuntimeBootstrapService.use((service) => + Effect.all({ + bunPath: service.resolveBunPath, + shouldBootstrap: service.shouldBootstrap, + result: service.run({ + metaUrl: pathToFileURL(entrypoint).href, + argv: ["node", "rig", "list"], + }), + flag: service.autoInstallFlag, + }), + ).pipe(Effect.provide(layer)), + ); + expect(first).toEqual({ + bunPath: "/runtime/bun", + shouldBootstrap: true, + result: 7, + flag: "--install=fallback", + }); + expect(calls[0]).toMatchObject({ + command: "/runtime/bun", + args: ["--install=fallback", entrypoint, "list"], + env: { RIG_BUN_BOOTSTRAPPED: "1" }, + }); + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => + service.run({ metaUrl: pathToFileURL(entrypoint).href, argv: ["node", "rig"] }), + ).pipe(Effect.provide(layer)), + ), + ).toBe(1); + }); + + it("skips bootstrap for guarded or existing Bun runtimes", async () => { + const bootstrappedLayer = runtimeBootstrapLayerFor({ + env: { RIG_BUN_BOOTSTRAPPED: "1" }, + bunGlobal: undefined, + }); + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => + Effect.all({ + result: service.run({ metaUrl: import.meta.url, argv: [] }), + should: service.shouldBootstrap, + }), + ).pipe(Effect.provide(bootstrappedLayer)), + ), + ).toEqual({ result: undefined, should: false }); + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => service.shouldBootstrap).pipe( + Effect.provide( + runtimeBootstrapLayerFor({ + env: { RIG_DISABLE_BUN_BOOTSTRAP: "1" }, + bunGlobal: undefined, + }), + ), + ), + ), + ).toBe(false); + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => service.shouldBootstrap).pipe( + Effect.provide(runtimeBootstrapLayerFor({ bunGlobal: { version: "test" } })), + ), + ), + ).toBe(false); + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => service.resolveBunPath).pipe( + Effect.provide(runtimeBootstrapLayerFor({ bunGlobal: undefined })), + ), + ), + ).toBe("bun"); + }); + + it("matches canonical entrypoint paths", async () => { + const entrypoint = "/workspace/cli.ts"; + expect( + await Effect.runPromise( + RuntimeBootstrapService.use((service) => + Effect.all([ + service.isEntrypoint(pathToFileURL(entrypoint).href, entrypoint), + service.isEntrypoint(import.meta.url, ""), + service.isEntrypoint(import.meta.url, entrypoint), + ]), + ).pipe(Effect.provide(runtimeBootstrapLayerFor({ bunGlobal: undefined }))), + ), + ).toEqual([true, false, false]); + }); +}); diff --git a/src/app/cli/runtime-bootstrap.ts b/src/app/cli/runtime-bootstrap.ts new file mode 100644 index 0000000..346297c --- /dev/null +++ b/src/app/cli/runtime-bootstrap.ts @@ -0,0 +1,148 @@ +import { spawnSync, type SpawnSyncReturns } from "node:child_process"; +import { realpathSync } from "node:fs"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../providers/errors/rig-error"; + +type BunRuntimeSpawnOptions = Parameters[2]; + +export class RuntimeBootstrapConfigService extends Context.Service< + RuntimeBootstrapConfigService, + { readonly env: NodeJS.ProcessEnv } +>()("@rendotdev/rig/application/RuntimeBootstrapConfigService", { + make: Effect.sync(() => ({ env: process.env })), +}) { + static readonly layer = Layer.effect( + RuntimeBootstrapConfigService, + RuntimeBootstrapConfigService.make, + ); +} + +export class RuntimeBootstrapPlatformService extends Context.Service< + RuntimeBootstrapPlatformService, + { + readonly spawn: ( + command: string, + args: string[], + options: BunRuntimeSpawnOptions, + ) => Effect.Effect, RigError>; + readonly bunGlobal: Effect.Effect; + readonly realpath: (path: string) => Effect.Effect; + readonly pathToFileUrl: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/RuntimeBootstrapPlatformService", { + make: Effect.gen(function* () { + const normalizeError = (cause: unknown): RigError => { + if (cause instanceof RigError) return cause; + return new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + }; + const spawn = Effect.fn("RuntimeBootstrapPlatformService.spawn")(function* ( + command: string, + args: string[], + options: BunRuntimeSpawnOptions, + ) { + return yield* Effect.try({ + try: () => spawnSync(command, args, options), + catch: normalizeError, + }); + }); + const bunGlobal = Effect.sync( + () => (globalThis as typeof globalThis & { Bun?: unknown }).Bun, + ).pipe(Effect.withSpan("RuntimeBootstrapPlatformService.bunGlobal")); + const realpath = Effect.fn("RuntimeBootstrapPlatformService.realpath")(function* ( + path: string, + ) { + return yield* Effect.try({ try: () => realpathSync(path), catch: normalizeError }); + }); + const pathToFileUrl = Effect.fn("RuntimeBootstrapPlatformService.pathToFileUrl")(function* ( + path: string, + ) { + return pathToFileURL(path); + }); + return { spawn, bunGlobal, realpath, pathToFileUrl } as const; + }), +}) { + static readonly layer = Layer.effect( + RuntimeBootstrapPlatformService, + RuntimeBootstrapPlatformService.make, + ); +} + +export class RuntimeBootstrapService extends Context.Service< + RuntimeBootstrapService, + { + readonly run: (params: { + metaUrl: string; + argv: string[]; + }) => Effect.Effect; + readonly shouldBootstrap: Effect.Effect; + readonly resolveBunPath: Effect.Effect; + readonly autoInstallFlag: Effect.Effect; + readonly isEntrypoint: (metaUrl: string, argvPath?: string) => Effect.Effect; + } +>()("@rendotdev/rig/application/RuntimeBootstrapService", { + make: Effect.gen(function* () { + const config = yield* RuntimeBootstrapConfigService; + const platform = yield* RuntimeBootstrapPlatformService; + + const shouldBootstrap = Effect.gen(function* () { + const bunGlobal = yield* platform.bunGlobal; + return ( + bunGlobal === undefined && + config.env.RIG_BUN_BOOTSTRAPPED !== "1" && + config.env.RIG_DISABLE_BUN_BOOTSTRAP !== "1" + ); + }).pipe(Effect.withSpan("RuntimeBootstrapService.shouldBootstrap")); + + const resolveBunPath = Effect.succeed(config.env.RIG_BUN_PATH ?? "bun").pipe( + Effect.withSpan("RuntimeBootstrapService.resolveBunPath"), + ); + + const autoInstallFlag = Effect.succeed("--install=fallback").pipe( + Effect.withSpan("RuntimeBootstrapService.autoInstallFlag"), + ); + + const isEntrypoint = Effect.fn("RuntimeBootstrapService.isEntrypoint")(function* ( + metaUrl: string, + argvPath?: string, + ) { + const targetPath = argvPath ?? process.argv[1]; + if (!targetPath) return false; + const canonicalPath = yield* platform + .realpath(targetPath) + .pipe(Effect.orElseSucceed(() => targetPath)); + return metaUrl === (yield* platform.pathToFileUrl(canonicalPath)).href; + }); + + const run = Effect.fn("RuntimeBootstrapService.run")(function* (params: { + metaUrl: string; + argv: string[]; + }) { + if (!(yield* shouldBootstrap)) return undefined; + const bunPath = yield* resolveBunPath; + const flag = yield* autoInstallFlag; + const result = yield* platform.spawn( + bunPath, + [flag, fileURLToPath(params.metaUrl), ...params.argv.slice(2)], + { + stdio: "inherit", + env: { ...config.env, RIG_BUN_BOOTSTRAPPED: "1" }, + }, + ); + return result.status ?? 1; + }); + + return { run, shouldBootstrap, resolveBunPath, autoInstallFlag, isEntrypoint } as const; + }), +}) { + static readonly layer = Layer.effect(RuntimeBootstrapService, RuntimeBootstrapService.make); +} + +export const runtimeBootstrapLayer = RuntimeBootstrapService.layer.pipe( + Layer.provide( + Layer.merge(RuntimeBootstrapConfigService.layer, RuntimeBootstrapPlatformService.layer), + ), +); diff --git a/src/app/cli/terminal-theme.test.ts b/src/app/cli/terminal-theme.test.ts new file mode 100644 index 0000000..7eec0ee --- /dev/null +++ b/src/app/cli/terminal-theme.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from "vite-plus/test"; +import { TerminalColors, TerminalIcons } from "./terminal-theme"; + +describe("terminal theme", () => { + it("provides semantic color values", () => { + expect(TerminalColors).toMatchObject({ + loading: "cyan", + success: "green", + error: "red", + muted: "gray", + }); + }); + + it("provides status icons and cycles loading frames", () => { + expect(TerminalIcons.success).toBe("✔"); + expect(TerminalIcons.error).toBe("✖"); + expect(TerminalIcons.loading({ frame: 0 })).toBe("⠋"); + expect(TerminalIcons.loading({ frame: 9 })).toBe("⠏"); + expect(TerminalIcons.loading({ frame: 10 })).toBe("⠋"); + }); +}); diff --git a/src/app/cli/terminal-theme.ts b/src/app/cli/terminal-theme.ts new file mode 100644 index 0000000..bb1faf5 --- /dev/null +++ b/src/app/cli/terminal-theme.ts @@ -0,0 +1,17 @@ +import type { TextProps } from "ink"; + +export const TerminalColors = { + loading: "cyan" as NonNullable, + success: "green" as NonNullable, + error: "red" as NonNullable, + muted: "gray" as NonNullable, +} as const; + +const terminalLoadingFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; + +export const TerminalIcons = { + success: "✔", + error: "✖", + loading: (params: { frame: number }): string => + terminalLoadingFrames[params.frame % terminalLoadingFrames.length] as string, +} as const; diff --git a/src/application/cli/cli-application.ts b/src/application/cli/cli-application.ts deleted file mode 100644 index 37007b4..0000000 --- a/src/application/cli/cli-application.ts +++ /dev/null @@ -1,716 +0,0 @@ -import { existsSync, readFileSync } from "node:fs"; -import type { Command } from "commander"; -import type { RigDirectoryMigrationResult } from "../../config/migration"; -import type { PathOptions } from "../../config/paths"; -import { rigErrors } from "../../errors/RigError"; -import { rigPackageRoot } from "../../runtime/package-root"; -import { commandTargets } from "../../tools/identifiers"; -import { RunPipelineContextServiceClass } from "./pipeline-context"; - -export class CliApplicationClass { - private program!: Command; - private generatedSyncRequested = false; - - async run(argv: string[]): Promise { - const { Command } = await import("commander"); - this.program = new Command(); - this.generatedSyncRequested = false; - this.configureProgram(); - try { - if (argv.slice(2).length === 0) { - this.requestGeneratedSync(); - await this.showDefaultStatus(); - } else { - await this.program.parseAsync(argv); - } - if (this.generatedSyncRequested) await this.syncGeneratedFiles(); - } catch (error) { - this.printError(error); - } - } - - private configureProgram(): void { - this.program - .name("rig") - .description("Local typed command runtime for agents.") - .version(this.version()) - .addHelpCommand(false); - this.configureInitCommands(); - this.configureConfigCommands(); - this.configureRegistryCommands(); - this.configureListCommand(); - this.configureFindCommand(); - this.configureInspectCommand(); - this.configureCreateCommand(); - this.configureEditCommand(); - this.configureRemoveCommand(); - this.configureEnvCommand(); - this.configureRunCommand(); - this.configureCronCommands(); - this.configureTypecheckCommand(); - this.configureMigrateCommand(); - this.configureDevCommands(); - this.configureUpdateCommand(); - this.program - .command("help") - .argument("[target]", "Topic, tool name, or command id (.).") - .description("Print topic help, tool docs, or general help.") - .action(async (target?: string) => { - if (!target) { - const readmePath = rigPackageRoot.packageFile(import.meta.url, "README.md"); - /* v8 ignore else */ - if (existsSync(readmePath)) { - console.log(readFileSync(readmePath, "utf8")); - } else { - /* v8 ignore next */ - console.log(this.program.helpInformation()); - } - return; - } - /* v8 ignore start */ - if (target === "topics") { - const { helpTopicService } = await import("../../tools/help-topics"); - console.log(helpTopicService.renderTopicList()); - return; - } - const { helpTopicService } = await import("../../tools/help-topics"); - const topicContent = helpTopicService.render(target); - if (topicContent) { - console.log(topicContent); - return; - } - /* v8 ignore stop */ - const { ToolHelpServiceClass } = await import("../../tools/help"); - console.log(await new ToolHelpServiceClass(this.pathOptions()).render(target)); - }); - } - - private configureInitCommands(): void { - this.program - .command("init") - .description("Initialize config and sync tools to AGENTS.md/CLAUDE.md.") - .action(async () => { - this.requestGeneratedSync(); - await this.showDefaultStatus(); - }); - - this.program - .command("doctor") - .description("Check local Rig setup.") - .action(async () => { - this.requestGeneratedSync(); - await this.doctor(); - }); - } - - private configureConfigCommands(): void { - const configCommand = this.program.command("config").description("Manage Rig config."); - configCommand - .command("show") - .description("Print config JSON.") - .action(async () => { - const { RigConfigStoreClass } = await import("../../config/config"); - const configStore = new RigConfigStoreClass(this.pathOptions()); - await configStore.ensure(); - this.printJson(await configStore.read()); - }); - configCommand - .command("path") - .description("Print absolute config path.") - .action(async () => { - const { RigPathsClass } = await import("../../config/paths"); - console.log(new RigPathsClass(this.pathOptions()).configPath); - }); - } - - private configureRegistryCommands(): void { - const registryCommand = this.program.command("registry").description("Manage tool registries."); - registryCommand - .command("list") - .description("List registries as JSON.") - .action(async () => { - const { RegistryConfigServiceClass } = await import("../../registry/registry"); - this.printJson(await new RegistryConfigServiceClass(this.pathOptions()).list()); - }); - registryCommand - .command("create") - .argument("[path]") - .description("Add a custom registry. Defaults to the current directory.") - .action(async (pathValue?: string) => { - this.requestGeneratedSync(); - const { RegistryConfigServiceClass } = await import("../../registry/registry"); - this.printJson( - await new RegistryConfigServiceClass(this.pathOptions()).add(pathValue ?? process.cwd()), - ); - }); - registryCommand - .command("remove") - .argument("") - .description("Remove a custom registry.") - .action(async (pathValue: string) => { - this.requestGeneratedSync(); - const { RegistryConfigServiceClass } = await import("../../registry/registry"); - this.printJson(await new RegistryConfigServiceClass(this.pathOptions()).remove(pathValue)); - }); - } - - private configureListCommand(): void { - this.program - .command("list") - .alias("ls") - .description("List discovered tools and commands.") - .option("--json", "Print full JSON metadata.") - .action(async (commandOptions: { json?: boolean }) => { - const { ToolListServiceClass } = await import("../../tools/list"); - const service = new ToolListServiceClass(this.pathOptions()); - const data = await service.list(); - if (commandOptions.json) this.printJson(data); - else console.log(service.renderPlain(data)); - }); - } - - private configureFindCommand(): void { - this.program - .command("find") - .argument("", "Natural-language description of the command to find.") - .description("Find commands with local typo-tolerant fuzzy search.") - .option("--limit ", "Maximum results to return, from 1 to 50.", "5") - .option("--tool ", "Search within one tool.") - .option("--json", "Print ranked result metadata as JSON.") - .action( - async ( - query: string, - commandOptions: { limit?: string; tool?: string; json?: boolean }, - ) => { - const { ToolFindServiceClass } = await import("../../tools/find"); - const service = new ToolFindServiceClass(this.pathOptions()); - const data = await service.find({ query, options: commandOptions }); - if (commandOptions.json) this.printJson(data); - else console.log(service.renderPlain({ data })); - }, - ); - } - - private configureInspectCommand(): void { - this.program - .command("inspect") - .argument("", "Tool name or command id (..)") - .description("Print tool or command metadata as JSON.") - .action(async (target: string) => { - const { ToolInspectorClass } = await import("../../tools/inspect"); - this.printJson(await new ToolInspectorClass(this.pathOptions()).inspect(target)); - }); - } - - private configureCreateCommand(): void { - this.program - .command("create") - .argument("") - .description("Create a starter tool in the base registry.") - .action(async (name: string) => { - this.requestGeneratedSync(); - await this.createTool(name); - }); - } - - private configureEditCommand(): void { - this.program - .command("edit") - .argument("") - .description("Print the TypeScript file path for a tool.") - .action(async (name: string) => { - const { ToolFileServiceClass } = await import("../../tools/create"); - const result = await new ToolFileServiceClass(this.pathOptions()).path(name); - console.log(result.toolPath); - }); - } - - private configureRemoveCommand(): void { - this.program - .command("remove") - .argument("") - .description("Remove a local tool directory.") - .action(async (name: string) => { - this.requestGeneratedSync(); - const { ToolRemoverClass } = await import("../../tools/create"); - const result = await new ToolRemoverClass(this.pathOptions()).remove(name); - console.log(`Removed tool ${result.name}`); - console.log(`Tool directory: ${result.toolDir}`); - }); - } - - private configureEnvCommand(): void { - this.program - .command("env") - .argument("", "Tool name.") - .argument( - "[assignments...]", - "Use KEY=VALUE to set values, or remove KEY [KEY...] to remove values.", - ) - .description("Show, write, or remove tool .env values using its env schema.") - .action(async (target: string, assignments: string[]) => { - const { ToolEnvServiceClass } = await import("../../tools/env"); - this.printJson( - await new ToolEnvServiceClass(this.pathOptions()).configure(target, assignments), - ); - }); - } - - private configureRunCommand(): void { - this.program - .command("run") - .argument("", "Command id, formatted as ..") - .argument("[args...]", "Command arguments.") - .description("Run a tool command.") - .option("--input ", "JSON input string.") - .option("--input-file ", "Read JSON input from a file.") - .option("--dry-run", "Validate input and show what would run without executing.") - .option("--query ", "Print one field from the JSON envelope, such as data.output.") - .option("--as ", "Attach this command's data to a pipeline context under the given id.") - .option("--pipe", "Read a Rig pipeline context from stdin for @id.path references.") - .action( - async (commandId: string, args: string[], commandOptions: Record) => { - await this.runToolCommand(commandId, args, commandOptions); - }, - ); - } - - private configureCronCommands(): void { - const cronCommand = this.program - .command("cron") - .description("Manage scheduled Rig tool commands."); - - cronCommand - .command("list") - .description("List scheduled Rig tool commands as JSON.") - .action(async () => { - const { RigCronServiceClass } = await import("../../tools/cron"); - this.printJson(await new RigCronServiceClass(this.pathOptions(), {}).list()); - }); - - cronCommand - .command("add") - .argument("", "Unique job name, using letters, numbers, hyphens, or underscores.") - .argument("", "Command id, formatted as ..") - .argument("", "Cron expression or nickname, such as @weekly.") - .description("Schedule a Rig tool command with fixed JSON input.") - .option("--input ", "JSON input string.") - .option("--input-file ", "Read JSON input from a file.") - .action( - async ( - name: string, - commandId: string, - schedule: string, - commandOptions: { input?: string; inputFile?: string }, - ) => { - const { cronModuleUrl, RigCronServiceClass } = await import("../../tools/cron"); - const result = await new RigCronServiceClass(this.pathOptions(), {}).add({ - name, - command: commandId, - schedule, - input: commandOptions.input, - inputFile: commandOptions.inputFile, - moduleUrl: cronModuleUrl(import.meta.url), - }); - this.printJson(result); - }, - ); - - cronCommand - .command("remove") - .argument("", "Cron job name.") - .description("Remove a scheduled Rig tool command.") - .action(async (name: string) => { - const { RigCronServiceClass } = await import("../../tools/cron"); - this.printJson(await new RigCronServiceClass(this.pathOptions(), {}).remove({ name })); - }); - - cronCommand - .command("run") - .argument("", "Cron job name.") - .description("Run a scheduled Rig tool command now.") - .action(async (name: string) => { - const { RigCronServiceClass } = await import("../../tools/cron"); - const result = await new RigCronServiceClass(this.pathOptions(), {}).run({ name }); - this.printJson(result.envelope); - process.exitCode = result.exitCode; - }); - } - - private configureTypecheckCommand(): void { - this.program - .command("typecheck") - .argument("[tool]") - .description("Type-check local tool files with the injected Rig tool runtime types.") - .action(async (tool?: string) => { - const { ToolTypecheckServiceClass } = await import("../../tools/typecheck"); - const result = await new ToolTypecheckServiceClass(this.pathOptions()).typecheck(tool); - this.printJson(result); - process.exitCode = result.exitCode; - }); - } - - private configureMigrateCommand(): void { - this.program - .command("migrate") - .description("Inspect local tools for Rig tool API migrations.") - .option("--json", "Print the migration report as JSON.") - .action(async (commandOptions: { json?: boolean }) => { - const { ToolApiMigrationServiceClass } = - await import("../../tools/migration/tool-api-migration"); - const service = new ToolApiMigrationServiceClass(this.pathOptions(), {}); - const report = await service.inspect(); - if (commandOptions.json) this.printJson(report); - else console.log(service.renderCli({ report })); - process.exitCode = report.unsupported.length > 0 ? 2 : 0; - }); - } - - /* v8 ignore start */ - private configureUpdateCommand(): void { - this.program - .command("update") - .description("Update rig to the latest published version.") - .action(async () => { - const [{ CommandUiRenderer }, { RigUpdaterFactory }] = await Promise.all([ - import("./command-ui"), - import("../../runtime/updates/rig-updater"), - ]); - const currentVersion = this.version(); - const Updater = RigUpdaterFactory.create({ - packageRoot: rigPackageRoot.find(import.meta.url), - currentVersion, - }); - - await CommandUiRenderer.run({ - label: "Preparing Rig update", - successLabel: "Update finished", - execute: async function execute(report) { - report.complete({ label: `Current version: ${Updater.getCurrentVersion()}` }); - report("Checking for updates"); - const plan = await Updater.plan(); - if (plan.status === "skipped") { - report.complete({ - label: "Update unavailable", - detail: ` ${plan.reason}`, - mutedDetail: true, - }); - return { status: "skipped" as const }; - } - - if (plan.status === "ready") { - report.complete({ - label: `Update available: ${plan.currentVersion} to ${plan.latestVersion}`, - }); - report("Updating Rig"); - const update = await Updater.update({ plan }); - if (update.status !== "updated") { - throw new Error("Rig did not complete the planned update."); - } - report.complete({ - label: `Updated Rig: ${update.previousVersion} to ${update.version}`, - detail: CommandUiRenderer.formatCommandOutputGroups({ - steps: [update.step], - outputs: [update.output], - }), - mutedDetail: true, - }); - } else { - report.complete({ label: `Already up to date: ${plan.version}` }); - } - - report("Synchronizing runtime and agent instructions"); - const sync = await Updater.sync({ plan }); - report.complete({ - label: "Synchronized runtime and agent instructions", - detail: CommandUiRenderer.formatCommandOutputGroups({ - steps: [sync.step], - outputs: [sync.output], - }), - mutedDetail: true, - }); - return { status: "ready" as const }; - }, - renderSuccess: function renderSuccess(result) { - return result.status === "skipped" - ? `Rig ${currentVersion} was left unchanged.` - : "Rig is ready to use."; - }, - }); - }); - } - /* v8 ignore stop */ - - private configureDevCommands(): void { - const devCommand = this.program.command("dev").description("Local development helpers."); - devCommand - .command("link") - .description("Link this checkout as the local rig command for development.") - .option("--bin-dir ", "Directory where the rig shim should be written.") - .option("--force", "Overwrite an existing non-Rig shim.") - .action(async (commandOptions: { binDir?: string; force?: boolean }) => { - const { DevLinkServiceClass } = await import("../../dev/dev-link"); - const service = new DevLinkServiceClass(this.pathOptions()); - const status = await service.link({ - binDir: commandOptions.binDir, - force: Boolean(commandOptions.force), - }); - console.log(service.renderLinkResult(status)); - }); - - devCommand - .command("unlink") - .description("Remove the local rig development shim.") - .option("--bin-dir ", "Directory where the rig shim was written.") - .option("--force", "Remove even if the file is not a Rig dev shim.") - .action(async (commandOptions: { binDir?: string; force?: boolean }) => { - const { DevLinkServiceClass } = await import("../../dev/dev-link"); - const service = new DevLinkServiceClass(this.pathOptions()); - const status = await service.unlink({ - binDir: commandOptions.binDir, - force: Boolean(commandOptions.force), - }); - console.log(service.renderUnlinkResult(status)); - }); - - devCommand - .command("status") - .description("Show local rig development shim status as JSON.") - .option("--bin-dir ", "Directory where the rig shim should be checked.") - .action(async (commandOptions: { binDir?: string }) => { - const { DevLinkServiceClass } = await import("../../dev/dev-link"); - this.printJson(await new DevLinkServiceClass(this.pathOptions()).status(commandOptions)); - }); - } - - private async runToolCommand( - commandId: string, - args: string[], - commandOptions: Record, - ): Promise { - const { ToolRunnerClass } = await import("../../tools/run"); - const commandTarget = this.commandTarget(commandId); - const pipeline = new RunPipelineContextServiceClass(); - /* v8 ignore next */ - const pipeContext = commandOptions.pipe ? pipeline.readFromStdin() : {}; - const result = await new ToolRunnerClass(this.pathOptions()).run( - commandTarget.tool, - commandTarget.command, - { - ...this.pathOptions(), - args, - input: commandOptions.input as string | undefined, - inputFile: commandOptions.inputFile as string | undefined, - dryRun: Boolean(commandOptions.dryRun), - pipeContext, - }, - ); - const envelope = pipeline.withOutputId( - result.envelope, - pipeContext, - commandOptions.as as string | undefined, - ); - const query = commandOptions.query as string | undefined; - if (query) this.printQueryResult(pipeline.query(envelope, query)); - else this.printJson(envelope); - process.exitCode = result.exitCode; - } - - private async showDefaultStatus(): Promise { - const [{ RigConfigStoreClass }, { RigPathsClass }, { ToolDiscoveryServiceClass }] = - await Promise.all([ - import("../../config/config"), - import("../../config/paths"), - import("../../registry/discover"), - ]); - const options = this.pathOptions(); - const paths = new RigPathsClass(options); - const configStore = new RigConfigStoreClass(options); - const config = await configStore.ensure(); - const tools = await new ToolDiscoveryServiceClass(options).discover(); - const registries = configStore.registryEntries(config); - const currentVersion = this.version(); - - const { RigLoggerFactoryClass } = await import("../../runtime/logger"); - new RigLoggerFactoryClass(options) - .app("cli") - .info({ version: currentVersion, tools: tools.length }, "Default status rendered."); - if (this.printMigrationNotice(configStore.migrationResult())) { - await configStore.acknowledgeMigrationPrompt(); - } - console.log("Rig is ready.\n"); - console.log(`Version: ${currentVersion}`); - console.log(`Config: ${paths.configPath}`); - console.log(`Base registry: ${registries[0]?.path}`); - const customRegistries = registries.filter((r) => r.kind === "custom"); - /* v8 ignore next 5 */ - if (customRegistries.length > 0) { - console.log("Custom registries:"); - for (const reg of customRegistries) { - console.log(` ${reg.path}`); - } - } - console.log(`Tools found: ${tools.length}`); - console.log("\nNext steps:"); - console.log(" rig list"); - console.log('\nRun "rig doctor" if you want to verify your setup.'); - await this.printToolApiMigrationNotice(); - await this.printUpdateNotice(currentVersion); - } - - private async createTool(name: string): Promise { - const { ToolCreatorClass } = await import("../../tools/create"); - const result = await new ToolCreatorClass(this.pathOptions()).create(name); - console.log(`Created tool ${result.name}`); - console.log(`\nTool directory: ${result.toolDir}`); - console.log(`Tool file: ${result.toolPath}`); - console.log("\nFiles:"); - for (const file of result.files) console.log(` ${file}`); - console.log(`\nEdit: ${result.toolPath}`); - console.log("\nTry:"); - console.log(` rig help ${result.name}`); - console.log(` rig help ${result.id}`); - console.log(` rig run ${result.id} test`); - } - - private async doctor(): Promise { - const [{ RigConfigStoreClass }, { RigPathsClass }, { ToolDiscoveryServiceClass }] = - await Promise.all([ - import("../../config/config"), - import("../../config/paths"), - import("../../registry/discover"), - ]); - const options = this.pathOptions(); - const paths = new RigPathsClass(options); - const configStore = new RigConfigStoreClass(options); - const config = await configStore.ensure(); - if (this.printMigrationNotice(configStore.migrationResult())) { - await configStore.acknowledgeMigrationPrompt(); - } - const registries = configStore.registryEntries(config); - const tools = await new ToolDiscoveryServiceClass(options).discover(); - - const { RigLoggerFactoryClass } = await import("../../runtime/logger"); - new RigLoggerFactoryClass(options) - .app("doctor") - .info({ registries: registries.length, tools: tools.length }, "Doctor check completed."); - console.log("Rig doctor\n"); - console.log(`Config: OK ${paths.configPath}`); - console.log(`Runtime SDK: OK ${paths.runtimeSdkPath}`); - console.log(`Registries: ${registries.length}`); - for (const registry of registries) { - console.log(` ${registry.kind}: ${registry.path}`); - } - console.log(`Tools: ${tools.length}`); - console.log("\nStatus: OK"); - await this.printToolApiMigrationNotice(); - await this.printUpdateNotice(this.version()); - } - - private printMigrationNotice(migration: RigDirectoryMigrationResult | undefined): boolean { - if (!migration) return false; - - if (migration.status === "migrated") { - console.log("Rig moved its home folder:"); - console.log(` From: ${migration.legacyDir}`); - console.log(` To: ${migration.currentDir}`); - if (migration.configUpdated) console.log(" Updated base registry: ~/rig/tools"); - console.log(""); - return false; - } - - console.log("Rig home folder migration needs your attention:"); - console.log(` Old folder: ${migration.legacyDir}`); - console.log(` New folder: ${migration.currentDir}`); - console.log(` Reason: ${migration.reason}`); - console.log("Move the files you want to keep into the new folder, then remove the old folder."); - console.log("This migration prompt is versioned; Rig will not show it again after this run."); - console.log(""); - return true; - } - - private async printUpdateNotice(currentVersion: string): Promise { - await this.ignoreSyncErrors(async () => { - const { NpmUpdateCheckServiceClass } = await import("../../runtime/update-check"); - const notice = await new NpmUpdateCheckServiceClass(this.pathOptions()).check(currentVersion); - /* v8 ignore next */ - if (notice) console.log(`\n${notice.message}`); - }); - } - - private async printToolApiMigrationNotice(): Promise { - const { ToolApiMigrationServiceClass } = - await import("../../tools/migration/tool-api-migration"); - const service = new ToolApiMigrationServiceClass(this.pathOptions(), {}); - const report = await service.inspect(); - if (!report.ready) console.log(`\n${service.renderCli({ report })}`); - } - - private requestGeneratedSync(): void { - this.generatedSyncRequested = true; - } - - private async syncGeneratedFiles(): Promise { - await this.ignoreSyncErrors(async () => { - const { ToolRuntimeInstructionSyncServiceClass } = - await import("../../tools/runtime-instruction"); - await new ToolRuntimeInstructionSyncServiceClass(this.pathOptions()).sync(); - }); - - if (process.env.RIG_AGENT_SYNC === "0") return; - - await this.ignoreSyncErrors(async () => { - const { AgentInstructionSyncServiceClass } = await import("../../agents/sync"); - await new AgentInstructionSyncServiceClass(this.pathOptions()).sync(); - }); - } - - private async ignoreSyncErrors(sync: () => Promise): Promise { - try { - await sync(); - } catch { - // Generated file sync should never block the requested Rig command. - } - } - - private commandTarget(commandId: string): { tool: string; command: string } { - return commandTargets.parse(commandId); - } - - private version(): string { - try { - const packageJsonPath = rigPackageRoot.packageFile(import.meta.url, "package.json"); - const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")) as { - version?: unknown; - }; - return typeof packageJson.version === "string" ? packageJson.version : "0.0.0"; - } catch { - return "0.0.0"; - } - } - - private pathOptions(): PathOptions { - return process.env.RIG_HOME ? { homeDir: process.env.RIG_HOME } : {}; - } - - private printQueryResult(value: unknown): void { - if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { - console.log(String(value)); - return; - } - this.printJson(value); - } - - private printJson(value: unknown): void { - console.log(JSON.stringify(value, null, 2)); - } - - private printError(error: unknown): never { - const rigError = rigErrors.from(error); - console.error(`${rigError.code}: ${rigError.message}`); - if (rigError.details !== undefined) { - console.error(JSON.stringify(rigError.details, null, 2)); - } - process.exit(1); - } -} diff --git a/src/application/cli/command-ui.test.tsx b/src/application/cli/command-ui.test.tsx deleted file mode 100644 index e22af66..0000000 --- a/src/application/cli/command-ui.test.tsx +++ /dev/null @@ -1,264 +0,0 @@ -import { cleanup, render } from "ink-testing-library"; -import { stripVTControlCharacters } from "node:util"; -import type { ReactElement } from "react"; -import { afterEach, describe, expect, it } from "vite-plus/test"; -import { CommandUi, CommandUiRendererClass } from "./command-ui"; - -afterEach(cleanup); - -class CommandUiTestRendererClass { - public current?: ReactElement; - public readonly states: ReactElement[] = []; - public unmounted = false; - public flushed = false; - - public render(tree: ReactElement) { - this.capture(tree); - return { - rerender: (nextTree: ReactElement) => this.capture(nextTree), - unmount: () => { - this.unmounted = true; - }, - waitUntilRenderFlush: async () => { - this.flushed = true; - }, - }; - } - - public lastFrame(): string | undefined { - const frame = this.current ? render(this.current).lastFrame() : undefined; - return frame ? stripVTControlCharacters(frame) : undefined; - } - - private capture(tree: ReactElement): void { - this.current = tree; - this.states.push(tree); - } -} - -describe("CommandUi", () => { - it("renders loading, success, and error states", () => { - const view = render(); - expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe("⠋ Updating Rig"); - - view.rerender(); - expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe("✔ Updated Rig\nRig is ready."); - - view.rerender(); - expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe( - "✖ Updating Rig\nRegistry failed.", - ); - }); - - it("renders completed items and hides an empty current state", () => { - const view = render( - , - ); - - expect(stripVTControlCharacters(view.lastFrame() ?? "")).toBe( - "✔ Checked version\n✔ Updated Rig\n npm install\n✔ Synchronized\nDone", - ); - }); -}); - -describe("CommandUiRendererClass", () => { - it("prints one stable non-interactive result with completed details", async () => { - const writes: string[] = []; - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: { - isTTY: false, - write(value) { - writes.push(String(value)); - return true; - }, - }, - render() { - throw new Error("Non-interactive output must not mount Ink."); - }, - }, - ); - - const result = await Renderer.run({ - label: "Preparing Rig update", - successLabel: "Update finished", - async execute(report) { - report("Checking for updates"); - report.complete({ label: "Current version: 0.0.41" }); - report.complete({ - label: "Updated Rig", - detail: " npm install\n changed 1 package", - mutedDetail: true, - }); - return "ready"; - }, - renderSuccess(value) { - return value === "ready" ? "Rig is ready." : ""; - }, - }); - - expect(result).toBe("ready"); - expect(writes).toEqual([ - [ - "✔ Current version: 0.0.41", - "✔ Updated Rig", - " npm install", - " changed 1 package", - "✔ Update finished", - "Rig is ready.", - "", - ].join("\n"), - ]); - }); - - it("uses the initial label and omits an empty success detail", async () => { - const writes: string[] = []; - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: { - isTTY: false, - write(value) { - writes.push(String(value)); - return true; - }, - }, - render() { - throw new Error("Non-interactive output must not mount Ink."); - }, - }, - ); - - await Renderer.run({ - label: "Checking Rig", - async execute() { - return undefined; - }, - renderSuccess() { - return ""; - }, - }); - - expect(writes).toEqual(["✔ Checking Rig\n"]); - }); - - it("renders interactive progress and success", async () => { - const TestRenderer = new CommandUiTestRendererClass(); - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: { isTTY: true, write: () => true }, - render: TestRenderer.render.bind(TestRenderer), - }, - ); - - const result = await Renderer.run({ - label: "Preparing Rig update", - successLabel: "Update finished", - async execute(report) { - report("Checking for updates"); - report.complete({ label: "Checked for updates" }); - return 42; - }, - renderSuccess(value) { - return `Result: ${value}`; - }, - }); - - expect(result).toBe(42); - expect(TestRenderer.states).toHaveLength(4); - expect(TestRenderer.lastFrame()).toBe("✔ Checked for updates\n✔ Update finished\nResult: 42"); - expect(TestRenderer.flushed).toBe(true); - expect(TestRenderer.unmounted).toBe(true); - }); - - it("renders Error failures against the active label", async () => { - const TestRenderer = new CommandUiTestRendererClass(); - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: { isTTY: true, write: () => true }, - render: TestRenderer.render.bind(TestRenderer), - }, - ); - - await expect( - Renderer.run({ - label: "Preparing Rig update", - async execute(report) { - report("Updating Rig"); - throw new Error("Registry unavailable"); - }, - renderSuccess() { - return "unreachable"; - }, - }), - ).rejects.toThrow("Registry unavailable"); - - expect(TestRenderer.lastFrame()).toBe("✖ Updating Rig\nRegistry unavailable"); - expect(TestRenderer.unmounted).toBe(true); - }); - - it("renders non-Error failures against the initial label after completion", async () => { - const TestRenderer = new CommandUiTestRendererClass(); - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: { isTTY: true, write: () => true }, - render: TestRenderer.render.bind(TestRenderer), - }, - ); - - await expect( - Renderer.run({ - label: "Preparing Rig update", - async execute(report) { - report.complete({ label: "Checked version" }); - throw "offline"; - }, - renderSuccess() { - return "unreachable"; - }, - }), - ).rejects.toBe("offline"); - - expect(TestRenderer.lastFrame()).toBe("✔ Checked version\n✖ Preparing Rig update\noffline"); - }); - - it("formats subprocess output and omits empty groups", () => { - const Renderer = new CommandUiRendererClass( - {}, - { - stdout: process.stdout, - render() { - throw new Error("Unexpected render."); - }, - }, - ); - - expect( - Renderer.formatCommandOutputGroups({ - steps: [ - { command: "/runtime/bin/npm", args: ["install", "@rendotdev/rig"] }, - { command: "/runtime/bin/rig", args: ["init"] }, - ], - outputs: ["changed 1 package\nready\n", ""], - }), - ).toBe(" npm install @rendotdev/rig\n changed 1 package\n ready"); - expect( - Renderer.formatCommandOutputGroups({ - steps: [{ command: "rig", args: ["init"] }], - outputs: [], - }), - ).toBeUndefined(); - }); -}); diff --git a/src/application/cli/command-ui.tsx b/src/application/cli/command-ui.tsx deleted file mode 100644 index c973c1d..0000000 --- a/src/application/cli/command-ui.tsx +++ /dev/null @@ -1,265 +0,0 @@ -import { basename } from "node:path"; -import { Text, render, useAnimation, type Instance } from "ink"; -import type { ReactElement } from "react"; -import { defineService, defineUIComponent } from "../../define"; -import type { RigUpdateStep } from "../../runtime/updates/rig-updater"; -import { TerminalColors, TerminalIcons } from "./terminal-theme"; - -type CommandUiState = "loading" | "success" | "error"; -type CommandUiCompletedItem = { label: string; detail?: string; mutedDetail?: boolean }; - -export type CommandUiReporter = ((label: string) => void) & { - complete: (params: CommandUiCompletedItem) => void; -}; - -type CommandUiProps = { - state: CommandUiState; - label: string; - detail?: string; - completed?: CommandUiCompletedItem[]; -}; - -export const CommandUiComponent = defineUIComponent({ - params: {}, - deps: {}, - component(props: CommandUiProps) { - const { frame } = useAnimation({ interval: 80, isActive: props.state === "loading" }); - const symbol = - props.state === "loading" - ? TerminalIcons.loading({ frame }) - : props.state === "success" - ? TerminalIcons.success - : TerminalIcons.error; - const color = - props.state === "success" - ? TerminalColors.success - : props.state === "error" - ? TerminalColors.error - : TerminalColors.loading; - const hasCompleted = Boolean(props.completed?.length); - const current = props.label - ? `${symbol} ${props.label}${props.detail ? `\n${props.detail}` : ""}` - : ""; - - return ( - - {props.completed?.map(function renderCompleted(item, index) { - return ( - - {index > 0 ? "\n" : ""} - - {TerminalIcons.success} {item.label} - - {item.detail ? ( - - {`\n${item.detail}`} - - ) : null} - - ); - })} - {hasCompleted && current ? "\n" : ""} - {current ? {current} : null} - - ); - }, -}); - -export const CommandUi = CommandUiComponent; - -function createReporter(params: { - onUpdate: (label: string) => void; - onComplete: (item: CommandUiCompletedItem) => void; -}): CommandUiReporter { - function report(label: string) { - params.onUpdate(label); - } - const reporter = report as CommandUiReporter; - reporter.complete = function complete(item) { - params.onComplete(item); - }; - return reporter; -} - -function ignoreUpdate(_label: string): void {} - -function formatCompleted(params: { completed: CommandUiCompletedItem[] }): string { - return params.completed - .map(function formatItem(item) { - return `${TerminalIcons.success} ${item.label}${item.detail ? `\n${item.detail}` : ""}`; - }) - .join("\n"); -} - -function formatUpdateCommandOutputGroups(params: { - steps: RigUpdateStep[]; - outputs: string[]; -}): string | undefined { - const groups = params.steps.flatMap(function formatStep(step, index) { - const output = params.outputs[index]?.trim(); - if (!output) return []; - const command = [basename(step.command), ...step.args].join(" "); - return [ - ` ${command}`, - ...output.split("\n").map(function indent(line) { - return ` ${line}`; - }), - ]; - }); - return groups.length > 0 ? groups.join("\n") : undefined; -} - -type CommandUiRendererDeps = { - stdout: Pick; - render: ( - tree: ReactElement, - options: { stdout: NodeJS.WriteStream; patchConsole: boolean }, - ) => Pick; -}; - -const CommandUiRendererProductionDeps: CommandUiRendererDeps = { - stdout: process.stdout, - render, -}; - -export class CommandUiRendererService extends defineService({ - params: {}, - deps: CommandUiRendererProductionDeps, -}) { - public async run(params: { - label: string; - successLabel?: string; - execute: (report: CommandUiReporter) => Promise; - renderSuccess: (result: Result) => string; - }): Promise { - const successLabel = params.successLabel ?? params.label; - const completed: CommandUiCompletedItem[] = []; - if (!this.deps.stdout.isTTY) { - const reporter = createReporter({ - onUpdate: ignoreUpdate, - onComplete: function captureCompleted(item) { - completed.push(item); - }, - }); - const result = await params.execute(reporter); - const detail = params.renderSuccess(result); - const output = [ - formatCompleted({ completed }), - `${TerminalIcons.success} ${successLabel}`, - detail, - ] - .filter(Boolean) - .join("\n"); - this.deps.stdout.write(`${output}\n`); - return result; - } - - const instance = this.deps.render(, { - stdout: this.deps.stdout as NodeJS.WriteStream, - patchConsole: false, - }); - let currentLabel = params.label; - function report(label: string) { - currentLabel = label; - instance.rerender(); - } - const reporter = createReporter({ - onUpdate: report, - onComplete: function complete(item) { - completed.push(item); - currentLabel = ""; - instance.rerender(); - }, - }); - - try { - const result = await params.execute(reporter); - instance.rerender( - , - ); - await instance.waitUntilRenderFlush(); - instance.unmount(); - return result; - } catch (error) { - instance.rerender( - , - ); - await instance.waitUntilRenderFlush(); - instance.unmount(); - throw error; - } - } - - public formatCommandOutputGroups(params: { steps: RigUpdateStep[]; outputs: string[] }) { - return formatUpdateCommandOutputGroups(params); - } -} - -export const CommandUiRenderer = new CommandUiRendererService(); - -export type CommandUiRendererClass = { - run: typeof CommandUiRenderer.run; - formatCommandOutputGroups: typeof CommandUiRenderer.formatCommandOutputGroups; -}; - -type CommandUiRendererConstructor = { - new (params: {}, deps: CommandUiRendererDeps): CommandUiRendererClass; - readonly prototype: CommandUiRendererClass; -}; - -type CommandUiRendererAdapter = CommandUiRendererClass & { - readonly resource: CommandUiRendererService; -}; - -const CommandUiRendererClassAdapter = function constructCommandUiRenderer( - this: CommandUiRendererAdapter, - _params: {}, - deps: CommandUiRendererDeps, -): void { - Object.defineProperty(this, "resource", { - value: new CommandUiRendererService({ params: {}, deps }), - }); -}; -Object.defineProperty(CommandUiRendererClassAdapter, "name", { - value: "CommandUiRendererClass", -}); -Object.defineProperties(CommandUiRendererClassAdapter.prototype, { - run: { - configurable: true, - value: function run( - this: CommandUiRendererAdapter, - params: { - label: string; - successLabel?: string; - execute: (report: CommandUiReporter) => Promise; - renderSuccess: (result: Result) => string; - }, - ) { - return this.resource.run(params); - }, - writable: true, - }, - formatCommandOutputGroups: { - configurable: true, - value: function formatCommandOutputGroups( - this: CommandUiRendererAdapter, - params: { steps: RigUpdateStep[]; outputs: string[] }, - ) { - return this.resource.formatCommandOutputGroups(params); - }, - writable: true, - }, -}); - -export const CommandUiRendererClass = - CommandUiRendererClassAdapter as unknown as CommandUiRendererConstructor; diff --git a/src/application/cli/composition-root.test.ts b/src/application/cli/composition-root.test.ts deleted file mode 100644 index d356d9c..0000000 --- a/src/application/cli/composition-root.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { describe, expect, it, vi } from "vite-plus/test"; -import { CliApplicationClass } from "./cli-application"; -import { CliCompositionRoot, CliCompositionRootClass } from "./composition-root"; -import { BunRuntimeBootstrapClass, CliEntrypointClass } from "./runtime-bootstrap"; - -describe("CLI composition root", () => { - it("runs the application when Bun bootstrap is unnecessary", async () => { - const application = { run: vi.fn<(argv: string[]) => Promise>(async () => undefined) }; - const runtimeBootstrap = { - run: vi.fn<(params: { metaUrl: string; argv: string[] }) => number | undefined>( - () => undefined, - ), - }; - const root = new CliCompositionRootClass( - { metaUrl: "file:///rig.ts", argv: ["bun", "rig", "list"] }, - { application: application as never, runtimeBootstrap: runtimeBootstrap as never }, - ); - - await root.run(); - - expect(runtimeBootstrap.run).toHaveBeenCalledWith({ - metaUrl: "file:///rig.ts", - argv: ["bun", "rig", "list"], - }); - expect(application.run).toHaveBeenCalledWith(["bun", "rig", "list"]); - }); - - it("exits with the bootstrap result before running the application", async () => { - const application = { run: vi.fn<(argv: string[]) => Promise>(async () => undefined) }; - const runtimeBootstrap = { - run: vi.fn<(params: { metaUrl: string; argv: string[] }) => number | undefined>(() => 7), - }; - const exit = vi.fn<(code: number) => never>(() => { - throw new Error("exit"); - }); - const root = new CliCompositionRootClass( - { metaUrl: "file:///rig.ts", argv: ["node", "rig"] }, - { application: application as never, runtimeBootstrap: runtimeBootstrap as never, exit }, - ); - - await expect(root.run()).rejects.toThrow("exit"); - expect(exit).toHaveBeenCalledWith(7); - expect(application.run).not.toHaveBeenCalled(); - }); - - it("runs the production dependency graph through replaceable boundaries", async () => { - const applicationRun = vi - .spyOn(CliApplicationClass.prototype, "run") - .mockResolvedValue(undefined); - vi.spyOn(BunRuntimeBootstrapClass.prototype, "run").mockReturnValue(undefined); - - await CliCompositionRoot.run({}); - - expect(applicationRun).toHaveBeenCalledWith([]); - expect( - new CliCompositionRootClass({ metaUrl: import.meta.url, argv: process.argv }, {}), - ).toBeDefined(); - expect(new BunRuntimeBootstrapClass({}, {})).toBeDefined(); - }); - - it("preserves the constructible entrypoint matcher", () => { - const entrypoint = new CliEntrypointClass( - {}, - { - realpath(path) { - return `/resolved${path}`; - }, - pathToFileUrl(path) { - return new URL(`file://${path}`); - }, - }, - ); - - expect(entrypoint.matches({ metaUrl: "file:///resolved/rig.ts", argvPath: "/rig.ts" })).toBe( - true, - ); - }); -}); diff --git a/src/application/cli/composition-root.ts b/src/application/cli/composition-root.ts deleted file mode 100644 index 7a46995..0000000 --- a/src/application/cli/composition-root.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { defineService } from "../../define"; -import { CliApplicationClass } from "./cli-application"; -import { BunRuntimeBootstrapClass } from "./runtime-bootstrap"; - -export type CliCompositionRootParams = { - metaUrl: string; - argv: string[]; -}; - -export type CliCompositionRootDeps = { - application?: Pick; - runtimeBootstrap?: Pick; - exit?: (code: number) => never; -}; - -type CliCompositionRootServiceDeps = { - runApplication: (params: { argv: string[] }) => Promise; - runRuntimeBootstrap: (params: { metaUrl: string; argv: string[] }) => number | undefined; - exit: (code: number) => never; -}; - -const CliCompositionRootProductionDeps: CliCompositionRootServiceDeps = { - runApplication: async function runApplication(params: { argv: string[] }) { - await new CliApplicationClass().run(params.argv); - }, - runRuntimeBootstrap: function runRuntimeBootstrap(params: { metaUrl: string; argv: string[] }) { - return new BunRuntimeBootstrapClass({}, {}).run(params); - }, - exit: process.exit.bind(process), -}; - -export class CliCompositionRootService extends defineService({ - params: { metaUrl: "", argv: [] } as CliCompositionRootParams, - deps: CliCompositionRootProductionDeps, -}) { - public async run(_params: {}): Promise { - const bootstrapped = this.deps.runRuntimeBootstrap({ - metaUrl: this.params.metaUrl, - argv: this.params.argv, - }); - if (bootstrapped !== undefined) this.deps.exit(bootstrapped); - await this.deps.runApplication({ argv: this.params.argv }); - } -} - -export const CliCompositionRoot = new CliCompositionRootService(); - -export type CliCompositionRootClass = { - run(): Promise; -}; - -type CliCompositionRootConstructor = { - new (params: CliCompositionRootParams, deps: CliCompositionRootDeps): CliCompositionRootClass; - readonly prototype: CliCompositionRootClass; -}; - -type CliCompositionRootAdapter = CliCompositionRootClass & { - readonly resource: CliCompositionRootService; -}; - -const CliCompositionRootClassAdapter = function constructCliCompositionRoot( - this: CliCompositionRootAdapter, - params: CliCompositionRootParams, - deps: CliCompositionRootDeps, -): void { - const application = deps.application; - const runtimeBootstrap = deps.runtimeBootstrap; - Object.defineProperty(this, "resource", { - value: new CliCompositionRootService({ - params, - deps: { - runApplication: application - ? async function runApplication(runParams: { argv: string[] }) { - await application.run(runParams.argv); - } - : CliCompositionRootProductionDeps.runApplication, - runRuntimeBootstrap: runtimeBootstrap - ? function runRuntimeBootstrap(runParams: { metaUrl: string; argv: string[] }) { - return runtimeBootstrap.run(runParams); - } - : CliCompositionRootProductionDeps.runRuntimeBootstrap, - exit: deps.exit ?? CliCompositionRootProductionDeps.exit, - }, - }), - }); -}; -Object.defineProperty(CliCompositionRootClassAdapter, "name", { - value: "CliCompositionRootClass", -}); -Object.defineProperty(CliCompositionRootClassAdapter.prototype, "run", { - configurable: true, - value: function run(this: CliCompositionRootAdapter) { - return this.resource.run({}); - }, - writable: true, -}); - -export const CliCompositionRootClass = - CliCompositionRootClassAdapter as unknown as CliCompositionRootConstructor; diff --git a/src/application/cli/pipeline-context.test.ts b/src/application/cli/pipeline-context.test.ts deleted file mode 100644 index fa17ed4..0000000 --- a/src/application/cli/pipeline-context.test.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; -import { - RunPipelineContextService, - RunPipelineContext, - RunPipelineContextServiceClass, -} from "./pipeline-context"; - -describe("pipeline context", () => { - it("reads pipeline context and previous data from stdin", () => { - const Pipeline = new RunPipelineContextService({ - params: {}, - deps: { - readStdin() { - return JSON.stringify({ pipe: { first: 1 }, data: { value: 2 } }); - }, - parseJson(text) { - return JSON.parse(text) as unknown; - }, - }, - }); - - expect(Pipeline.readFromStdin({})).toEqual({ first: 1, prev: { value: 2 } }); - }); - - it("handles envelopes without inherited pipeline context", () => { - const Pipeline = new RunPipelineContextService({ - params: {}, - deps: { - readStdin() { - return JSON.stringify({ data: null }); - }, - parseJson(text) { - return JSON.parse(text) as unknown; - }, - }, - }); - const NonRecordPipeline = new RunPipelineContextService({ - params: {}, - deps: { - readStdin() { - return JSON.stringify({ pipe: "invalid", data: 2 }); - }, - parseJson(text) { - return JSON.parse(text) as unknown; - }, - }, - }); - const NonEnvelopePipeline = new RunPipelineContextService({ - params: {}, - deps: { - readStdin() { - return "null"; - }, - parseJson(text) { - return JSON.parse(text) as unknown; - }, - }, - }); - - expect(Pipeline.readFromStdin({})).toEqual({ prev: null }); - expect(NonRecordPipeline.readFromStdin({})).toEqual({ prev: 2 }); - expect(() => NonEnvelopePipeline.readFromStdin({})).toThrow("Query cannot access: data"); - }); - - it("returns an empty context for empty stdin", () => { - const Pipeline = new RunPipelineContextService({ - params: {}, - deps: { - readStdin() { - return " "; - }, - parseJson() { - throw new Error("must not parse"); - }, - }, - }); - - expect(Pipeline.readFromStdin({})).toEqual({}); - }); - - it("queries object and array paths and reports inaccessible values", () => { - expect( - RunPipelineContext.query({ - value: { items: [{ name: "rig" }] }, - path: "items.0.name", - }), - ).toBe("rig"); - expect(RunPipelineContext.query({ value: "unchanged", path: "" })).toBe("unchanged"); - expect(() => RunPipelineContext.query({ value: { item: null }, path: "item.name" })).toThrow( - "Query cannot access: item.name", - ); - expect(() => RunPipelineContext.query({ value: { item: {} }, path: "item.name" })).toThrow( - "Query is missing: item.name", - ); - }); - - it("adds output IDs while preserving existing context", () => { - const envelope = { data: { ok: true }, errors: [] }; - - expect( - RunPipelineContext.withOutputId({ - envelope, - context: { previous: 1 }, - id: "step_2", - }), - ).toEqual({ - ...envelope, - pipe: { previous: 1, step_2: { ok: true } }, - }); - expect(RunPipelineContext.withOutputId({ envelope, context: {} })).toBe(envelope); - expect(RunPipelineContext.withOutputId({ envelope: "value", context: {}, id: "step" })).toBe( - "value", - ); - expect(() => RunPipelineContext.withOutputId({ envelope, context: {}, id: "bad.id" })).toThrow( - "Pipeline id is invalid: bad.id", - ); - }); - - it("preserves positional compatibility methods", () => { - const adapter = new RunPipelineContextServiceClass(); - const envelope = { data: 2 }; - - expect(adapter).toBeInstanceOf(RunPipelineContextServiceClass); - expect(adapter.query({ value: 3 }, "value")).toBe(3); - expect(adapter.withOutputId(envelope, {}, "result")).toEqual({ - data: 2, - pipe: { result: 2 }, - }); - }); -}); diff --git a/src/application/cli/pipeline-context.ts b/src/application/cli/pipeline-context.ts deleted file mode 100644 index 5a2b202..0000000 --- a/src/application/cli/pipeline-context.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { readFileSync } from "node:fs"; -import { defineService } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; - -function isPipelineRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null; -} - -function pipelineContext(envelope: unknown): Record { - if (!isPipelineRecord(envelope)) return {}; - return isPipelineRecord(envelope.pipe) ? { ...envelope.pipe } : {}; -} - -function validatePipelineId(id: string): void { - if (/^[A-Za-z0-9_-]+$/.test(id)) return; - throw new RigErrorClass("INPUT_ERROR", `Pipeline id is invalid: ${id}`, { id }); -} - -const RunPipelineContextServiceDeps: { - readStdin: () => string; - parseJson: (text: string) => unknown; -} = { - /* v8 ignore next 3 -- production stdin is exercised by CLI integration */ - readStdin: function readStdin() { - return readFileSync(0, "utf8"); - }, - /* v8 ignore next 3 -- production parser is exercised through CLI integration */ - parseJson: function parseJson(text: string) { - return JSON.parse(text) as unknown; - }, -}; - -export class RunPipelineContextService extends defineService({ - params: {}, - deps: RunPipelineContextServiceDeps, -}) { - public query(params: { value: unknown; path: string }): unknown { - let current = params.value; - for (const part of params.path.split(".").filter(Boolean)) { - if (!isPipelineRecord(current) && !Array.isArray(current)) { - throw new RigErrorClass("INPUT_ERROR", `Query cannot access: ${params.path}`, { - path: params.path, - missing: part, - }); - } - current = (current as Record)[part]; - if (current === undefined) { - throw new RigErrorClass("INPUT_ERROR", `Query is missing: ${params.path}`, { - path: params.path, - missing: part, - }); - } - } - return current; - } - - public readFromStdin(_params: {}): Record { - const text = this.deps.readStdin().trim(); - if (!text) return {}; - const envelope = this.deps.parseJson(text); - const context = pipelineContext(envelope); - const data = this.query({ value: envelope, path: "data" }); - context.prev = data; - return context; - } - - public withOutputId(params: { - envelope: unknown; - context: Record; - id?: string; - }): unknown { - if (!params.id) return params.envelope; - validatePipelineId(params.id); - if (!isPipelineRecord(params.envelope)) return params.envelope; - const data = params.envelope.data; - return { ...params.envelope, pipe: { ...params.context, [params.id]: data } }; - } -} - -export const RunPipelineContext = new RunPipelineContextService(); - -export type RunPipelineContextServiceClass = { - readFromStdin(): Record; - withOutputId(envelope: unknown, context: Record, id?: string): unknown; - query(value: unknown, path: string): unknown; -}; - -type RunPipelineContextServiceConstructor = { - new (): RunPipelineContextServiceClass; - readonly prototype: RunPipelineContextServiceClass; -}; - -const RunPipelineContextServiceClassAdapter = function constructRunPipelineContext(): void {}; -Object.defineProperty(RunPipelineContextServiceClassAdapter, "name", { - value: "RunPipelineContextServiceClass", -}); -Object.defineProperties(RunPipelineContextServiceClassAdapter.prototype, { - readFromStdin: { - configurable: true, - /* v8 ignore next 3 -- compatibility path delegates to production stdin */ - value: function readFromStdin(): Record { - return RunPipelineContext.readFromStdin({}); - }, - writable: true, - }, - withOutputId: { - configurable: true, - value: function withOutputId( - envelope: unknown, - context: Record, - id?: string, - ): unknown { - return RunPipelineContext.withOutputId({ envelope, context, id }); - }, - writable: true, - }, - query: { - configurable: true, - value: function query(value: unknown, path: string): unknown { - return RunPipelineContext.query({ value, path }); - }, - writable: true, - }, -}); - -export const RunPipelineContextServiceClass = - RunPipelineContextServiceClassAdapter as unknown as RunPipelineContextServiceConstructor; diff --git a/src/application/cli/runtime-bootstrap.ts b/src/application/cli/runtime-bootstrap.ts deleted file mode 100644 index 7ef74e6..0000000 --- a/src/application/cli/runtime-bootstrap.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { spawnSync, type SpawnSyncReturns } from "node:child_process"; -import { realpathSync } from "node:fs"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { defineService } from "../../define"; - -type BunRuntimeSpawn = ( - command: string, - args: string[], - options: Parameters[2], -) => SpawnSyncReturns; - -type BunRuntimeGlobalProvider = () => unknown; - -export type BunRuntimeBootstrapParams = { - /** @deprecated Rig now uses the Bun runtime available on PATH. */ - packageRoot?: string; -}; - -export type BunRuntimeBootstrapDeps = { - spawn?: BunRuntimeSpawn; - env?: NodeJS.ProcessEnv; - bunGlobal?: BunRuntimeGlobalProvider; -}; - -type BunRuntimeBootstrapServiceDeps = { - spawn: BunRuntimeSpawn; - env: NodeJS.ProcessEnv; - bunGlobal: BunRuntimeGlobalProvider; -}; - -function runtimeAutoInstallFlag(_params: {}): string { - return "--install=fallback"; -} - -const BunRuntimeBootstrapProductionDeps: BunRuntimeBootstrapServiceDeps = { - spawn: spawnSync, - env: process.env, - bunGlobal: function bunGlobal() { - return (globalThis as typeof globalThis & { Bun?: unknown }).Bun; - }, -}; - -export class BunRuntimeBootstrapService extends defineService({ - params: {} as BunRuntimeBootstrapParams, - deps: BunRuntimeBootstrapProductionDeps, -}) { - public shouldBootstrap(_params: {}): boolean { - return ( - this.deps.bunGlobal() === undefined && - this.deps.env.RIG_BUN_BOOTSTRAPPED !== "1" && - this.deps.env.RIG_DISABLE_BUN_BOOTSTRAP !== "1" - ); - } - - public resolveBunPath(_params: {}): string { - return this.deps.env.RIG_BUN_PATH ?? "bun"; - } - - public run(params: { metaUrl: string; argv: string[] }): number | undefined { - if (!this.shouldBootstrap({})) return undefined; - const result = this.deps.spawn( - this.resolveBunPath({}), - [runtimeAutoInstallFlag({}), fileURLToPath(params.metaUrl), ...params.argv.slice(2)], - { - stdio: "inherit", - env: { ...this.deps.env, RIG_BUN_BOOTSTRAPPED: "1" }, - }, - ); - return result.status ?? 1; - } - - public autoInstallFlag(_params: {}): string { - return runtimeAutoInstallFlag({}); - } -} - -export const BunRuntimeBootstrapServiceDefault = new BunRuntimeBootstrapService(); - -export type BunRuntimeBootstrapClass = { - run(params: { metaUrl: string; argv: string[] }): number | undefined; - shouldBootstrap(): boolean; - resolveBunPath(): string; - autoInstallFlag(): string; -}; - -type BunRuntimeBootstrapConstructor = { - new (params: BunRuntimeBootstrapParams, deps: BunRuntimeBootstrapDeps): BunRuntimeBootstrapClass; - readonly prototype: BunRuntimeBootstrapClass; -}; - -type BunRuntimeBootstrapAdapter = BunRuntimeBootstrapClass & { - readonly resource: BunRuntimeBootstrapService; -}; - -const BunRuntimeBootstrapClassAdapter = function constructBunRuntimeBootstrap( - this: BunRuntimeBootstrapAdapter, - params: BunRuntimeBootstrapParams, - deps: BunRuntimeBootstrapDeps, -): void { - Object.defineProperty(this, "resource", { - value: new BunRuntimeBootstrapService({ - params, - deps: { - spawn: deps.spawn ?? BunRuntimeBootstrapProductionDeps.spawn, - env: deps.env ?? BunRuntimeBootstrapProductionDeps.env, - bunGlobal: deps.bunGlobal ?? BunRuntimeBootstrapProductionDeps.bunGlobal, - }, - }), - }); -}; -Object.defineProperty(BunRuntimeBootstrapClassAdapter, "name", { - value: "BunRuntimeBootstrapClass", -}); -Object.defineProperties(BunRuntimeBootstrapClassAdapter.prototype, { - run: { - configurable: true, - value: function run( - this: BunRuntimeBootstrapAdapter, - params: { metaUrl: string; argv: string[] }, - ) { - return this.resource.run(params); - }, - writable: true, - }, - shouldBootstrap: { - configurable: true, - value: function shouldBootstrap(this: BunRuntimeBootstrapAdapter) { - return this.resource.shouldBootstrap({}); - }, - writable: true, - }, - resolveBunPath: { - configurable: true, - value: function resolveBunPath(this: BunRuntimeBootstrapAdapter) { - return this.resource.resolveBunPath({}); - }, - writable: true, - }, - autoInstallFlag: { - configurable: true, - value: function autoInstallFlag(this: BunRuntimeBootstrapAdapter) { - return this.resource.autoInstallFlag({}); - }, - writable: true, - }, -}); - -export const BunRuntimeBootstrapClass = - BunRuntimeBootstrapClassAdapter as unknown as BunRuntimeBootstrapConstructor; -export const BunRuntimeBootstrap = BunRuntimeBootstrapClass; - -type CliEntrypointDeps = { - realpath: (path: string) => string; - pathToFileUrl: (path: string) => URL; -}; - -const CliEntrypointProductionDeps: CliEntrypointDeps = { - realpath: function realpath(path: string) { - return realpathSync(path); - }, - pathToFileUrl: function pathToFileUrl(path: string) { - return pathToFileURL(path); - }, -}; - -export class CliEntrypointService extends defineService({ - params: {}, - deps: CliEntrypointProductionDeps, -}) { - public matches(params: { metaUrl: string; argvPath?: string }): boolean { - if (!params.argvPath) return false; - try { - const resolved = this.deps.realpath(params.argvPath); - return params.metaUrl === this.deps.pathToFileUrl(resolved).href; - } catch { - return params.metaUrl === this.deps.pathToFileUrl(params.argvPath).href; - } - } -} - -export const CliEntrypointServiceDefault = new CliEntrypointService(); - -export type CliEntrypointClass = { - matches(params: { metaUrl: string; argvPath?: string }): boolean; -}; - -type CliEntrypointConstructor = { - new (params: Record, deps: CliEntrypointDeps): CliEntrypointClass; - readonly prototype: CliEntrypointClass; -}; - -type CliEntrypointAdapter = CliEntrypointClass & { readonly resource: CliEntrypointService }; - -const CliEntrypointClassAdapter = function constructCliEntrypoint( - this: CliEntrypointAdapter, - _params: Record, - deps: CliEntrypointDeps, -): void { - Object.defineProperty(this, "resource", { - value: new CliEntrypointService({ params: {}, deps }), - }); -}; -Object.defineProperty(CliEntrypointClassAdapter, "name", { value: "CliEntrypointClass" }); -Object.defineProperty(CliEntrypointClassAdapter.prototype, "matches", { - configurable: true, - value: function matches( - this: CliEntrypointAdapter, - params: { metaUrl: string; argvPath?: string }, - ) { - return this.resource.matches(params); - }, - writable: true, -}); - -export const CliEntrypointClass = CliEntrypointClassAdapter as unknown as CliEntrypointConstructor; -export const CliEntrypoint = CliEntrypointServiceDefault; - -export function isCliEntrypoint(metaUrl: string, argvPath = process.argv[1]): boolean { - return CliEntrypoint.matches({ metaUrl, argvPath }); -} diff --git a/src/application/cli/terminal-theme.test.ts b/src/application/cli/terminal-theme.test.ts deleted file mode 100644 index 59e0915..0000000 --- a/src/application/cli/terminal-theme.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; -import { - TerminalColorsClass, - TerminalColorsSingleton, - TerminalIconsClass, - TerminalIconsSingleton, - TerminalColors, - TerminalIcons, -} from "./terminal-theme"; - -describe("terminal theme", () => { - it("builds semantic color values and preserves legacy construction", () => { - const BuiltColors = TerminalColorsSingleton; - const Colors = new TerminalColorsClass({}, {}); - - expect(BuiltColors).toMatchObject({ - loading: "cyan", - success: "green", - error: "red", - muted: "gray", - }); - expect(Colors).toMatchObject(BuiltColors); - expect(Colors).toBeInstanceOf(TerminalColorsClass); - expect(TerminalColors).toBe(TerminalColorsSingleton); - }); - - it("builds status icons and cycles loading frames", () => { - const BuiltIcons = TerminalIconsSingleton; - const Icons = new TerminalIconsClass({}, {}); - - expect(Icons.success).toBe("✔"); - expect(Icons.error).toBe("✖"); - expect(Icons.loading({ frame: 0 })).toBe("⠋"); - expect(Icons.loading({ frame: 10 })).toBe("⠋"); - expect(BuiltIcons.loading({ frame: 9 })).toBe("⠏"); - expect(TerminalIconsSingleton.loading({ frame: 1 })).toBe("⠙"); - expect(Icons).toBeInstanceOf(TerminalIconsClass); - expect(TerminalIcons).toBe(TerminalIconsSingleton); - }); -}); diff --git a/src/application/cli/terminal-theme.ts b/src/application/cli/terminal-theme.ts deleted file mode 100644 index 2f871f0..0000000 --- a/src/application/cli/terminal-theme.ts +++ /dev/null @@ -1,56 +0,0 @@ -import type { TextProps } from "ink"; -import { defineSingleton } from "../../define"; - -type TerminalColorsValue = { - readonly loading: NonNullable; - readonly success: NonNullable; - readonly error: NonNullable; - readonly muted: NonNullable; -}; - -type TerminalIconsValue = { - readonly success: string; - readonly error: string; - loading(params: { frame: number }): string; -}; - -type LegacyConstructor = { - new (params: {}, deps: {}): Value; - readonly prototype: Value; -}; - -function TerminalColorsClassAdapter(this: TerminalColorsValue) { - Object.assign(this, TerminalColorsSingleton); -} - -function TerminalIconsClassAdapter(this: TerminalIconsValue) { - Object.assign(this, TerminalIconsSingleton); -} - -export const TerminalColorsSingleton = defineSingleton({ - params: {}, - deps: {}, - loading: "cyan" as NonNullable, - success: "green" as NonNullable, - error: "red" as NonNullable, - muted: "gray" as NonNullable, -}); - -export const TerminalIconsSingleton = defineSingleton({ - params: { - loadingFrames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"], - }, - deps: {}, - success: "✔", - error: "✖", - loading(params: { frame: number }): string { - return this.params.loadingFrames[params.frame % this.params.loadingFrames.length] as string; - }, -}); - -export const TerminalColorsClass = - TerminalColorsClassAdapter as unknown as LegacyConstructor; -export const TerminalIconsClass = - TerminalIconsClassAdapter as unknown as LegacyConstructor; -export const TerminalColors = TerminalColorsSingleton; -export const TerminalIcons = TerminalIconsSingleton; diff --git a/src/architecture.test.ts b/src/architecture.test.ts deleted file mode 100644 index c4a0c6e..0000000 --- a/src/architecture.test.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; -import { readFile, readdir } from "node:fs/promises"; - -class ArchitectureSourceSetClass { - async implementationFiles(): Promise { - const files = await Promise.all( - ["src", "scripts"].map(async (root) => - (await readdir(root, { recursive: true })) - .filter( - (path) => - (path.endsWith(".ts") || path.endsWith(".tsx")) && - !path.endsWith(".test.ts") && - !path.endsWith(".test.tsx") && - !path.endsWith(".d.ts"), - ) - .map((path) => `${root}/${path}`), - ), - ); - return files.flat().toSorted(); - } - - async violations( - pattern: RegExp, - validate: (match: RegExpMatchArray) => boolean, - ): Promise { - const files = await this.implementationFiles(); - const violations = await Promise.all( - files.map(async (path) => { - const source = await readFile(path, "utf8"); - return [...source.matchAll(pattern)] - .filter((match) => !validate(match)) - .map((match) => `${path}:${this.line(source, match.index)}`); - }), - ); - return violations.flat(); - } - - private line(source: string, index: number | undefined): number { - return source.slice(0, index ?? 0).split("\n").length; - } -} - -describe("architecture", () => { - const sources = new ArchitectureSourceSetClass(); - - it("uses architectural suffixes for implementation class declarations", async () => { - const violations = await sources.violations(/\bclass\s+([A-Za-z_$][\w$]*)/g, (match) => - /(?:Class|Repo|Repository|Service|Runtime|Provider|Route)$/.test(match[1]!), - ); - expect(violations).toEqual([]); - }); - - it("keeps behavior on instances instead of static methods", async () => { - const violations = await sources.violations(/\bstatic\s+[A-Za-z_$][\w$]*\s*[<(]/g, () => false); - expect(violations).toEqual([]); - }); - - it("uses definitions for migrated application behavior", async () => { - const migratedDefinitions = [ - { - path: "src/application/cli/composition-root.ts", - names: ["CliCompositionRootService"], - }, - { - path: "src/application/cli/runtime-bootstrap.ts", - names: ["BunRuntimeBootstrapService", "CliEntrypointService"], - }, - { - path: "src/application/cli/command-ui.tsx", - names: ["CommandUiComponent", "CommandUiRendererService"], - }, - { - path: "src/cron/application/rig-cron.ts", - names: ["RigCronService", "RigCronWorkerService", "CronStateTransactionService"], - }, - { - path: "src/runtime/updates/rig-updater.ts", - names: ["RigUpdateCommandRunnerService", "RigUpdaterService", "RigUpdaterFactoryService"], - }, - { - path: "src/runtime/updates/npm-update-check.ts", - names: ["NpmUpdateCheckService", "VersionComparatorSingleton"], - }, - ]; - - const sourcesByEntry = await Promise.all( - migratedDefinitions.map(async (entry) => ({ - entry, - source: await readFile(entry.path, "utf8"), - })), - ); - - for (const { entry, source } of sourcesByEntry) { - expect(source).not.toContain("DomainClass"); - for (const name of entry.names) { - expect(source).toContain(name); - expect(source).not.toContain(`${name}Builder`); - } - } - }); - - it("keeps persistence independent from CLI application modules", async () => { - const persistenceFiles = (await sources.implementationFiles()).filter((entry) => - entry.startsWith("src/persistence/"), - ); - const violations = ( - await Promise.all( - persistenceFiles.map(async (path) => ({ path, source: await readFile(path, "utf8") })), - ) - ) - .filter(({ source }) => /from\s+["'][^"']*(?:application\/cli|\/cli)[^"']*["']/.test(source)) - .map(({ path }) => path); - expect(violations).toEqual([]); - }); -}); diff --git a/src/bin.ts b/src/bin.ts deleted file mode 100644 index 840b2da..0000000 --- a/src/bin.ts +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bun -import { CliCompositionRootClass } from "./application/cli/composition-root"; -import { isCliEntrypoint } from "./application/cli/runtime-bootstrap"; - -/* v8 ignore next 3 */ -if (isCliEntrypoint(import.meta.url)) { - await new CliCompositionRootClass({ metaUrl: import.meta.url, argv: process.argv }, {}).run(); -} diff --git a/src/cli.ts b/src/cli.ts deleted file mode 100644 index 395c689..0000000 --- a/src/cli.ts +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bun -import { CliCompositionRootClass } from "./application/cli/composition-root"; -import { isCliEntrypoint } from "./application/cli/runtime-bootstrap"; - -export { - CliApplicationClass, - CliApplicationClass as CliApplication, -} from "./application/cli/cli-application"; -export { - BunRuntimeBootstrapClass, - BunRuntimeBootstrapClass as BunRuntimeBootstrap, - isCliEntrypoint, -} from "./application/cli/runtime-bootstrap"; -export { RigCronWorkerClass, RigCronWorkerClass as RigCronWorker } from "./tools/cron"; - -/* v8 ignore next 3 */ -if (isCliEntrypoint(import.meta.url)) { - await new CliCompositionRootClass({ metaUrl: import.meta.url, argv: process.argv }, {}).run(); -} diff --git a/src/collections/application/tool-collection.ts b/src/collections/application/tool-collection.ts deleted file mode 100644 index 747ffd4..0000000 --- a/src/collections/application/tool-collection.ts +++ /dev/null @@ -1,1135 +0,0 @@ -import type { Database } from "bun:sqlite"; -import { existsSync, readdirSync, statSync } from "node:fs"; -import { mkdir, readFile, rm, unlink, writeFile } from "node:fs/promises"; -import { dirname, join, resolve } from "node:path"; -import type { z } from "zod"; -import { BoundedFileLockClass } from "../../config/file-lock"; -import { RigErrorClass } from "../../errors/RigError"; -import { BunSqliteModuleLoaderClass } from "../../persistence/sqlite/tool-database"; -import { CollectionNameClass } from "../../tools/identifiers"; -import type { LoadedTool } from "../../tools/types"; -import { collectionFieldPathCompiler } from "../domain/field-path"; - -// ─── Public types ──────────────────────────────────────────────────────────── - -export type CollectionDefinition = z.ZodObject> = { - schema?: T; - generateId?: (data: z.input) => string; -}; - -export type CollectionEntry> = { - id: string; - data: T; - body: string; - createdAt: string; - updatedAt: string; -}; - -export type SearchResult> = CollectionEntry & { - snippet: string; - rank: number; -}; - -export type ListOptions = { - where?: Record; - sort?: string; - limit?: number; - offset?: number; -}; - -export type CollectionHandle> = { - readonly name: string; - readonly path: string; - - create(entry: { id?: string; data: T; body?: string }): Promise>; - getEntry(id: string): Promise | null>; - update(id: string, patch: { data?: Partial; body?: string }): Promise>; - upsert(entry: { id: string; data: T; body?: string }): Promise<{ id: string; created: boolean }>; - remove(id: string): Promise; - - list(opts?: ListOptions): Promise<{ entries: CollectionEntry[]; total: number }>; - search(query: string, opts?: { limit?: number }): Promise<{ entries: SearchResult[] }>; - count(where?: Record): Promise; - - getCollection(filter?: (entry: CollectionEntry) => boolean): Promise[]>; - clear(): Promise; -}; - -// ─── Frontmatter parser/serializer ────────────────────────────────────────── - -const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/; - -export class FrontmatterCodecClass { - parse(content: string): { data: Record; body: string } { - const match = content.match(FRONTMATTER_RE); - if (!match) return { data: {}, body: content }; - const yaml = match[1]!; - const body = match[2]!.replace(/^\n/, ""); // strip leading blank line after --- - return { data: this.parseYaml(yaml), body }; - } - - serialize(data: Record, body: string): string { - const yaml = this.serializeYaml(data); - const normalizedBody = body.endsWith("\n") ? body : `${body}\n`; - return `---\n${yaml}---\n\n${normalizedBody}`; - } - - private parseYaml(yaml: string): Record { - const result: Record = {}; - let currentKey: string | null = null; - // Block context: could be an array or a nested object, decided by first indented line - let blockBuffer: unknown[] | Record | null = null; - let currentArrayObj: Record | null = null; - - const lines = yaml.split(/\r?\n/); - for (const line of lines) { - // Array item line: " - value" or " - key: value" - if (blockBuffer !== null && /^\s+-\s/.test(line)) { - // Ensure block is an array - if (!Array.isArray(blockBuffer)) blockBuffer = []; - // Flush previous object item if any - if (currentArrayObj !== null) { - (blockBuffer as unknown[]).push(currentArrayObj); - currentArrayObj = null; - } - const value = line.replace(/^\s+-\s*/, ""); - const objKv = value.match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); - if (objKv) { - currentArrayObj = { [objKv[1]!]: this.parseScalar(objKv[2]!.trim()) }; - } else { - /* v8 ignore next */ - (blockBuffer as unknown[]).push(this.parseScalar(value)); - } - continue; - } - - // Continuation of object item in array: " key: value" (deeper indent, 4+) - if (currentArrayObj !== null && /^\s{4,}\S/.test(line)) { - const kvMatch = line.trim().match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); - if (kvMatch) { - currentArrayObj[kvMatch[1]!] = this.parseScalar(kvMatch[2]!.trim()); - continue; - } - } - - // Nested object property: " key: value" (2-space indent, no dash) - /* v8 ignore next */ - if (blockBuffer !== null && /^\s{2}[A-Za-z_]/.test(line) && !/^\s+-/.test(line)) { - // Ensure block is an object - if (Array.isArray(blockBuffer) && blockBuffer.length === 0) { - blockBuffer = {}; - } - if (!Array.isArray(blockBuffer)) { - const kvMatch = line.trim().match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); - if (kvMatch) { - (blockBuffer as Record)[kvMatch[1]!] = this.parseScalar( - kvMatch[2]!.trim(), - ); - continue; - } - } - } - - // Flush current array object item - if (currentArrayObj !== null) { - /* v8 ignore else -- array items always initialize an array block */ - if (Array.isArray(blockBuffer)) blockBuffer.push(currentArrayObj); - currentArrayObj = null; - } - - // Flush block if we hit a top-level key - if (blockBuffer !== null && currentKey !== null && /^[A-Za-z_]/.test(line)) { - result[currentKey] = blockBuffer; - blockBuffer = null; - } - - // Empty line or comment - if (!line.trim() || line.trim().startsWith("#")) continue; - - // Top-level key: value - const kvMatch = line.match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); - if (!kvMatch) continue; - - currentKey = kvMatch[1]!; - const rawValue = kvMatch[2]!.trim(); - - if (rawValue === "[]") { - result[currentKey] = []; - continue; - } - - if (rawValue === "") { - // Start of a block (array or nested object, decided by first child line) - blockBuffer = []; - continue; - } - - // Inline array: [a, b, c] - if (rawValue.startsWith("[") && rawValue.endsWith("]")) { - const inner = rawValue.slice(1, -1); - result[currentKey] = inner.split(",").map((s) => this.parseScalar(s.trim())); - continue; - } - - result[currentKey] = this.parseScalar(rawValue); - } - - // Flush trailing - /* v8 ignore next 3 */ - if (currentArrayObj !== null && Array.isArray(blockBuffer)) { - blockBuffer.push(currentArrayObj); - } - if (blockBuffer !== null && currentKey !== null) { - result[currentKey] = blockBuffer; - } - - return result; - } - - private parseScalar(value: string): unknown { - if (value === "true") return true; - if (value === "false") return false; - if (value === "null" || value === "~" || value === "") return null; - // Quoted string - if ( - (value.startsWith('"') && value.endsWith('"')) || - (value.startsWith("'") && value.endsWith("'")) - ) { - return value.slice(1, -1); - } - // Number - const num = Number(value); - if (!Number.isNaN(num) && value !== "") return num; - return value; - } - - private serializeYaml(data: Record, indent = ""): string { - const lines: string[] = []; - for (const [key, value] of Object.entries(data)) { - /* v8 ignore next */ - if (value === undefined) continue; - if (Array.isArray(value)) { - if (value.length === 0) { - lines.push(`${indent}${key}: []`); - } else if (this.isSimpleArray(value)) { - lines.push(`${indent}${key}: [${value.map((v) => this.serializeScalar(v)).join(", ")}]`); - } else { - lines.push(`${indent}${key}:`); - for (const item of value) { - /* v8 ignore else -- non-record values are emitted by the simple-array branch */ - if (this.isRecord(item)) { - const objYaml = this.serializeYaml(item, `${indent} `); - const objLines = objYaml.split("\n").filter(Boolean); - lines.push(`${indent} - ${objLines[0]!.trim()}`); - for (const objLine of objLines.slice(1)) { - lines.push(`${indent} ${objLine.trim()}`); - } - } else { - /* v8 ignore next */ - lines.push(`${indent} - ${this.serializeScalar(item)}`); - } - } - } - } else if (this.isRecord(value)) { - lines.push(`${indent}${key}:`); - lines.push(this.serializeYaml(value, `${indent} `)); - } else { - lines.push(`${indent}${key}: ${this.serializeScalar(value)}`); - } - } - return lines.join("\n") + "\n"; - } - - private serializeScalar(value: unknown): string { - if (value === null || value === undefined) return "null"; - if (typeof value === "boolean") return String(value); - if (typeof value === "number") return String(value); - const str = String(value); - // Quote if it contains special chars - if (/[:{},&*#?|> typeof v !== "object" || v === null); - } - - private isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); - } -} - -// ─── Index interface ──────────────────────────────────────────────────────── - -export type DocRow = { - id: string; - data_json: string; - body: string; - created_at: string; - updated_at: string; - file_mtime: number; -}; - -export type CollectionFileMetadata = { - mtimeMs: number; - ctimeMs: number; - size: number; -}; - -export type CollectionFileRecord = CollectionFileMetadata & { - id: string; - status: "indexed" | "invalid"; -}; - -export interface CollectionIndexInterface { - withExclusiveInitialization(params: { operation: () => Promise }): Promise; - open(): Promise; - upsertDoc(entry: CollectionEntry>, fileMtime: number): void; - deleteDoc(id: string): void; - upsertFile(record: CollectionFileRecord): void; - deleteFile(id: string): void; - getFile(id: string): CollectionFileRecord | null; - allFileIds(): string[]; - getDoc(id: string): DocRow | null; - listDocs(opts: ListOptions): { rows: DocRow[]; total: number }; - searchDocs(query: string, limit: number): DocRow[]; - countDocs(where?: Record): number; - allIds(): string[]; - clearAll(): void; - close(): void; -} - -class CollectionEntryIdClass { - readonly filePath: string; - - constructor( - readonly value: string, - collectionPath: string, - ) { - if ( - typeof value !== "string" || - value.length === 0 || - value === "." || - value === ".." || - value.includes("/") || - value.includes("\\") - ) { - throw new RigErrorClass("INPUT_ERROR", `Invalid collection entry id: ${value}`, { - expected: "a non-empty filename without path separators or dot segments", - }); - } - - const root = resolve(collectionPath); - const filePath = resolve(root, `${value}.md`); - /* v8 ignore next 3 -- defense in depth after separator and dot-segment validation */ - if (dirname(filePath) !== root) { - throw new RigErrorClass( - "INPUT_ERROR", - `Collection entry id escapes its collection: ${value}`, - ); - } - this.filePath = filePath; - } -} - -class CollectionFileFingerprintClass { - read(path: string): CollectionFileMetadata { - const stat = statSync(path); - return { mtimeMs: stat.mtimeMs, ctimeMs: stat.ctimeMs, size: stat.size }; - } - - matches(record: CollectionFileRecord | null, metadata: CollectionFileMetadata): boolean { - return ( - record !== null && - record.mtimeMs === metadata.mtimeMs && - record.ctimeMs === metadata.ctimeMs && - record.size === metadata.size - ); - } -} - -const collectionFileFingerprint = new CollectionFileFingerprintClass(); - -// ─── SQLite index (production) ────────────────────────────────────────────── - -/* v8 ignore start */ -type CollectionDatabaseConstructor = new ( - filename: string, - options?: { create?: boolean; strict?: boolean }, -) => Database; - -class CollectionIndexCorruptionErrorClass extends Error {} - -class SqliteCollectionIndexClass implements CollectionIndexInterface { - private db: Database | null = null; - private readonly dbPath: string; - private readonly collectionPath: string; - private readonly sqliteLoader = new BunSqliteModuleLoaderClass(); - private readonly initializationLock: BoundedFileLockClass; - - constructor(collectionPath: string) { - this.collectionPath = collectionPath; - this.dbPath = join(collectionPath, ".index.sqlite"); - this.initializationLock = new BoundedFileLockClass(this.dbPath); - } - - async withExclusiveInitialization(params: { operation: () => Promise }): Promise { - return this.initializationLock.run(params.operation); - } - - async open(): Promise { - if (this.db) return; - if (!this.sqliteLoader.available()) { - throw new RigErrorClass("TOOL_INVALID", "Collections require the Bun SQLite runtime."); - } - const Database = await this.sqliteLoader.database(); - try { - this.initialize(Database); - } catch (error) { - this.close(); - if (!this.isRecoverable(error)) throw error; - await this.recover(Database); - } - } - - private async recover(Database: CollectionDatabaseConstructor): Promise { - try { - this.initialize(Database); - return; - } catch (error) { - this.close(); - if (!this.isRecoverable(error)) throw error; - } - - await this.removeDerivedFiles(); - this.initialize(Database); - } - - private initialize(Database: CollectionDatabaseConstructor): void { - this.db = new Database(this.dbPath, { create: true, strict: true }); - try { - this.db.run("PRAGMA busy_timeout = 5000;"); - this.db.run("PRAGMA journal_mode = WAL;"); - this.migrate(); - this.validate(); - } catch (error) { - this.close(); - throw error; - } - } - - private validate(): void { - try { - this.db!.query( - "SELECT id, data_json, body, created_at, updated_at, file_mtime FROM docs LIMIT 0", - ).all(); - this.db!.query( - "SELECT id, mtime_ms, ctime_ms, size, status FROM collection_files LIMIT 0", - ).all(); - this.db!.query("SELECT rowid, id, data_json, body FROM docs_fts LIMIT 0").all(); - this.validateIntegrityIfDue(); - } catch (error) { - if (error instanceof CollectionIndexCorruptionErrorClass) throw error; - throw new CollectionIndexCorruptionErrorClass( - `Collection index schema is unusable: ${error instanceof Error ? error.message : String(error)}`, - { cause: error }, - ); - } - } - - private validateIntegrityIfDue(): void { - const checkIntervalMs = 24 * 60 * 60 * 1000; - const checked = this.db!.query( - "SELECT checked_at FROM collection_health WHERE key = 'integrity'", - ).get() as { checked_at: number } | null; - if (checked && Date.now() - checked.checked_at < checkIntervalMs) return; - - const integrityRows = this.db!.query("PRAGMA quick_check;").all() as Array< - Record - >; - const integrityResults = integrityRows.flatMap((row) => Object.values(row)); - if (integrityResults.length !== 1 || integrityResults[0] !== "ok") { - throw new CollectionIndexCorruptionErrorClass( - `Collection index integrity check failed: ${integrityResults.join(", ")}`, - ); - } - this.db!.query( - `INSERT INTO collection_health (key, checked_at) - VALUES ('integrity', $checkedAt) - ON CONFLICT(key) DO UPDATE SET checked_at = excluded.checked_at`, - ).run({ checkedAt: Date.now() }); - } - - private isRecoverable(error: unknown): boolean { - if (error instanceof CollectionIndexCorruptionErrorClass) return true; - if (typeof error !== "object" || error === null || !("code" in error)) return false; - const code = String(error.code); - return code === "SQLITE_NOTADB" || code.startsWith("SQLITE_CORRUPT"); - } - - private async removeDerivedFiles(): Promise { - await Promise.all( - [this.dbPath, `${this.dbPath}-shm`, `${this.dbPath}-wal`].map((path) => - rm(path, { force: true }), - ), - ); - } - - private migrate(): void { - this.db!.run(` - CREATE TABLE IF NOT EXISTS docs ( - id TEXT PRIMARY KEY, - data_json TEXT NOT NULL, - body TEXT NOT NULL DEFAULT '', - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - file_mtime REAL NOT NULL DEFAULT 0 - ); - `); - this.db!.run(` - CREATE TABLE IF NOT EXISTS collection_files ( - id TEXT PRIMARY KEY, - mtime_ms REAL NOT NULL, - ctime_ms REAL NOT NULL, - size INTEGER NOT NULL, - status TEXT NOT NULL CHECK(status IN ('indexed', 'invalid')) - ); - `); - this.db!.run(` - CREATE TABLE IF NOT EXISTS collection_health ( - key TEXT PRIMARY KEY, - checked_at INTEGER NOT NULL - ); - `); - this.db!.run(` - CREATE VIRTUAL TABLE IF NOT EXISTS docs_fts USING fts5( - id, data_json, body, - content=docs, content_rowid=rowid - ); - `); - // Triggers for FTS sync - this.db!.run(` - CREATE TRIGGER IF NOT EXISTS docs_ai AFTER INSERT ON docs BEGIN - INSERT INTO docs_fts(rowid, id, data_json, body) - VALUES (new.rowid, new.id, new.data_json, new.body); - END; - `); - this.db!.run(` - CREATE TRIGGER IF NOT EXISTS docs_ad AFTER DELETE ON docs BEGIN - INSERT INTO docs_fts(docs_fts, rowid, id, data_json, body) - VALUES ('delete', old.rowid, old.id, old.data_json, old.body); - END; - `); - this.db!.run(` - CREATE TRIGGER IF NOT EXISTS docs_au AFTER UPDATE ON docs BEGIN - INSERT INTO docs_fts(docs_fts, rowid, id, data_json, body) - VALUES ('delete', old.rowid, old.id, old.data_json, old.body); - INSERT INTO docs_fts(rowid, id, data_json, body) - VALUES (new.rowid, new.id, new.data_json, new.body); - END; - `); - } - - upsertDoc(entry: CollectionEntry>, fileMtime: number): void { - this.db!.query(` - INSERT INTO docs (id, data_json, body, created_at, updated_at, file_mtime) - VALUES ($id, $dataJson, $body, $createdAt, $updatedAt, $fileMtime) - ON CONFLICT(id) DO UPDATE SET - data_json = excluded.data_json, - body = excluded.body, - updated_at = excluded.updated_at, - file_mtime = excluded.file_mtime - `).run({ - id: entry.id, - dataJson: JSON.stringify(entry.data), - body: entry.body, - createdAt: entry.createdAt, - updatedAt: entry.updatedAt, - fileMtime, - }); - } - - deleteDoc(id: string): void { - this.db!.query("DELETE FROM docs WHERE id = $id").run({ id }); - } - - upsertFile(record: CollectionFileRecord): void { - this.db!.query(` - INSERT INTO collection_files (id, mtime_ms, ctime_ms, size, status) - VALUES ($id, $mtimeMs, $ctimeMs, $size, $status) - ON CONFLICT(id) DO UPDATE SET - mtime_ms = excluded.mtime_ms, - ctime_ms = excluded.ctime_ms, - size = excluded.size, - status = excluded.status - `).run(record); - } - - deleteFile(id: string): void { - this.db!.query("DELETE FROM collection_files WHERE id = $id").run({ id }); - } - - getFile(id: string): CollectionFileRecord | null { - const row = this.db!.query(` - SELECT id, mtime_ms, ctime_ms, size, status - FROM collection_files - WHERE id = $id - `).get({ id }) as { - id: string; - mtime_ms: number; - ctime_ms: number; - size: number; - status: "indexed" | "invalid"; - } | null; - if (!row) return null; - return { - id: row.id, - mtimeMs: row.mtime_ms, - ctimeMs: row.ctime_ms, - size: row.size, - status: row.status, - }; - } - - allFileIds(): string[] { - const rows = this.db!.query("SELECT id FROM collection_files").all() as { id: string }[]; - return rows.map((row) => row.id); - } - - getDoc(id: string): DocRow | null { - return this.db!.query("SELECT * FROM docs WHERE id = $id").get({ id }) as DocRow | null; - } - - listDocs(opts: ListOptions): { rows: DocRow[]; total: number } { - let whereClause = ""; - const params: Record = {}; - - if (opts.where && Object.keys(opts.where).length > 0) { - const conditions: string[] = []; - for (const [index, [key, value]] of Object.entries(opts.where).entries()) { - const field = collectionFieldPathCompiler.compile(key); - const paramName = `where_${index}`; - conditions.push(`json_extract(data_json, '${field.sqliteJsonPath}') = $${paramName}`); - params[paramName] = typeof value === "object" ? JSON.stringify(value) : value; - } - whereClause = `WHERE ${conditions.join(" AND ")}`; - } - - let orderClause = "ORDER BY updated_at DESC"; - if (opts.sort) { - const desc = opts.sort.startsWith("-"); - const field = desc ? opts.sort.slice(1) : opts.sort; - const compiledField = collectionFieldPathCompiler.compile(field); - orderClause = `ORDER BY json_extract(data_json, '${compiledField.sqliteJsonPath}') ${desc ? "DESC" : "ASC"}`; - } - - const limit = opts.limit ?? 50; - const offset = opts.offset ?? 0; - - const countRow = this.db!.query(`SELECT COUNT(*) as cnt FROM docs ${whereClause}`).get( - params as any, - ) as { cnt: number }; - - const rows = this.db!.query( - `SELECT * FROM docs ${whereClause} ${orderClause} LIMIT $limit OFFSET $offset`, - ).all({ ...params, limit, offset } as any) as DocRow[]; - - return { rows, total: countRow.cnt }; - } - - searchDocs(query: string, limit: number): DocRow[] { - // Use FTS5 MATCH for full-text search, join back to docs for full row - const rows = this.db!.query(` - SELECT docs.*, docs_fts.rank - FROM docs_fts - JOIN docs ON docs.id = docs_fts.id - WHERE docs_fts MATCH $query - ORDER BY docs_fts.rank - LIMIT $limit - `).all({ query, limit }) as (DocRow & { rank: number })[]; - return rows; - } - - countDocs(where?: Record): number { - if (!where || Object.keys(where).length === 0) { - const row = this.db!.query("SELECT COUNT(*) as cnt FROM docs").get() as { cnt: number }; - return row.cnt; - } - const conditions: string[] = []; - const params: Record = {}; - for (const [index, [key, value]] of Object.entries(where).entries()) { - const field = collectionFieldPathCompiler.compile(key); - const paramName = `where_${index}`; - conditions.push(`json_extract(data_json, '${field.sqliteJsonPath}') = $${paramName}`); - params[paramName] = typeof value === "object" ? JSON.stringify(value) : value; - } - const row = this.db!.query( - `SELECT COUNT(*) as cnt FROM docs WHERE ${conditions.join(" AND ")}`, - ).get(params as any) as { cnt: number }; - return row.cnt; - } - - allIds(): string[] { - const rows = this.db!.query("SELECT id FROM docs").all() as { id: string }[]; - return rows.map((r) => r.id); - } - - clearAll(): void { - this.db!.run("DELETE FROM docs"); - this.db!.run("DELETE FROM collection_files"); - } - - close(): void { - this.db?.close(false); - this.db = null; - } -} -/* v8 ignore stop */ - -// ─── Collection handle implementation ─────────────────────────────────────── - -export class CollectionHandleImplClass< - T extends Record, -> implements CollectionHandle { - readonly name: string; - readonly path: string; - private readonly schema: z.ZodObject | undefined; - private readonly generateId: ((data: T) => string) | undefined; - private readonly index: CollectionIndexInterface; - private readonly codec = new FrontmatterCodecClass(); - private reconciled = false; - - constructor( - name: string, - collectionPath: string, - definition: CollectionDefinition, - index?: CollectionIndexInterface, - ) { - this.name = name; - this.path = collectionPath; - this.schema = definition.schema; - this.generateId = definition.generateId as ((data: T) => string) | undefined; - /* v8 ignore next */ - this.index = index ?? new SqliteCollectionIndexClass(collectionPath); - } - - async init(): Promise { - await mkdir(this.path, { recursive: true }); - await this.index.withExclusiveInitialization({ - operation: async () => { - await this.index.open(); - await this.reconcile(); - }, - }); - } - - async create(entry: { id?: string; data: T; body?: string }): Promise> { - const data = this.validate(entry.data); - const id = entry.id ?? this.deriveId(data); - if (id === undefined) { - throw new RigErrorClass( - "INPUT_ERROR", - "Collection entry needs an id (provide one or define generateId).", - ); - } - - const entryId = new CollectionEntryIdClass(id, this.path); - const filePath = entryId.filePath; - if (existsSync(filePath)) { - throw new RigErrorClass("INPUT_ERROR", `Collection entry already exists: ${id}`, { id }); - } - - const now = new Date().toISOString(); - const doc: CollectionEntry = { - id, - data, - body: entry.body ?? "", - createdAt: now, - updatedAt: now, - }; - - await this.writeToDisk(doc); - return doc; - } - - async getEntry(id: string): Promise | null> { - const entryId = new CollectionEntryIdClass(id, this.path); - const filePath = entryId.filePath; - if (!existsSync(filePath)) return null; - return this.readFromDisk(entryId.value); - } - - async update( - id: string, - patch: { data?: Partial; body?: string }, - ): Promise> { - const existing = await this.getEntry(id); - if (!existing) { - throw new RigErrorClass("INPUT_ERROR", `Collection entry not found: ${id}`, { id }); - } - - const mergedData = patch.data - ? this.validate({ ...existing.data, ...patch.data } as T) - : existing.data; - - const doc: CollectionEntry = { - id, - data: mergedData, - body: patch.body !== undefined ? patch.body : existing.body, - createdAt: existing.createdAt, - updatedAt: new Date().toISOString(), - }; - - await this.writeToDisk(doc); - return doc; - } - - async upsert(entry: { - id: string; - data: T; - body?: string; - }): Promise<{ id: string; created: boolean }> { - const entryId = new CollectionEntryIdClass(entry.id, this.path); - const exists = existsSync(entryId.filePath); - if (exists) { - await this.update(entry.id, { data: entry.data, body: entry.body }); - return { id: entry.id, created: false }; - } - await this.create(entry); - return { id: entry.id, created: true }; - } - - async remove(id: string): Promise { - const entryId = new CollectionEntryIdClass(id, this.path); - const filePath = entryId.filePath; - if (!existsSync(filePath)) return false; - await unlink(filePath); - this.index.deleteDoc(id); - this.index.deleteFile(id); - return true; - } - - async list(opts: ListOptions = {}): Promise<{ entries: CollectionEntry[]; total: number }> { - const { rows, total } = this.index.listDocs(opts); - return { - entries: rows.map((row) => this.rowToEntry(row)), - total, - }; - } - - async search(query: string, opts?: { limit?: number }): Promise<{ entries: SearchResult[] }> { - const limit = opts?.limit ?? 10; - const rows = this.index.searchDocs(query, limit) as (DocRow & { rank: number })[]; - return { - entries: rows.map((row) => ({ - ...this.rowToEntry(row), - snippet: this.extractSnippet(row.body, query), - rank: row.rank, - })), - }; - } - - async count(where?: Record): Promise { - return this.index.countDocs(where); - } - - async getCollection( - filter?: (entry: CollectionEntry) => boolean, - ): Promise[]> { - const { entries } = await this.list({ limit: 100_000 }); - return filter ? entries.filter(filter) : entries; - } - - async clear(): Promise { - const ids = [...new Set([...this.index.allIds(), ...this.index.allFileIds()])]; - await Promise.all( - ids.map(async (id) => { - const filePath = this.filePath(id); - /* v8 ignore next */ - if (existsSync(filePath)) await unlink(filePath); - }), - ); - this.index.clearAll(); - } - - close(): void { - this.index.close(); - } - - // ─── Private ───────────────────────────────────────────────────────── - - private validate(data: unknown): T { - if (!this.schema) return data as T; - const result = this.schema.safeParse(data); - if (!result.success) { - throw new RigErrorClass("VALIDATION_ERROR", "Collection entry data is invalid.", { - errors: result.error.flatten(), - }); - } - return result.data as T; - } - - private deriveId(data: T): string | undefined { - if (this.generateId) return this.generateId(data); - // Fallback: try common fields - const candidate = - (data as Record).id ?? - (data as Record).slug ?? - (data as Record).title; - if (typeof candidate === "string") return this.slugify(candidate); - return undefined; - } - - private slugify(value: string): string { - return value - .toLowerCase() - .replace(/[^a-z0-9]+/g, "-") - .replace(/^-|-$/g, ""); - } - - private filePath(id: string): string { - return new CollectionEntryIdClass(id, this.path).filePath; - } - - private async writeToDisk(doc: CollectionEntry): Promise { - const content = this.codec.serialize(doc.data as Record, doc.body); - const filePath = this.filePath(doc.id); - await writeFile(filePath, content, "utf8"); - const metadata = collectionFileFingerprint.read(filePath); - this.index.upsertDoc(doc as CollectionEntry>, metadata.mtimeMs); - this.index.upsertFile({ id: doc.id, status: "indexed", ...metadata }); - } - - private async readFromDisk(id: string): Promise> { - const filePath = this.filePath(id); - const content = await readFile(filePath, "utf8"); - const { data, body } = this.codec.parse(content); - const stat = statSync(filePath); - - // Check index for timestamps - const row = this.index.getDoc(id); - /* v8 ignore next 3 */ - const createdAt = row?.created_at ?? stat.birthtime.toISOString(); - const updatedAt = row?.updated_at ?? stat.mtime.toISOString(); - - return { - id, - data: (this.schema ? this.validate(data) : data) as T, - body, - createdAt, - updatedAt, - }; - } - - /* v8 ignore start */ - private async reconcile(): Promise { - if (this.reconciled) return; - this.reconciled = true; - - // Stat every Markdown file, then parse only additions and metadata changes. - const files = readdirSync(this.path).filter((f) => f.endsWith(".md")); - const knownIds = new Set([...this.index.allIds(), ...this.index.allFileIds()]); - const fileIds = new Set(); - - await Promise.all( - files.map(async (file) => { - const id = file.slice(0, -3); // remove .md - fileIds.add(id); - const filePath = join(this.path, file); - const metadata = collectionFileFingerprint.read(filePath); - const tracked = this.index.getFile(id); - if (collectionFileFingerprint.matches(tracked, metadata)) return; - - // File is newer or missing from index: re-index - try { - const content = await readFile(filePath, "utf8"); - const { data, body } = this.codec.parse(content); - const stableMetadata = collectionFileFingerprint.read(filePath); - const indexed = this.index.getDoc(id); - const entry: CollectionEntry> = { - id, - data: this.schema ? this.validate(data) : data, - body, - createdAt: indexed?.created_at ?? new Date(stableMetadata.ctimeMs).toISOString(), - updatedAt: new Date(stableMetadata.mtimeMs).toISOString(), - }; - this.index.upsertDoc(entry, stableMetadata.mtimeMs); - this.index.upsertFile({ id, status: "indexed", ...stableMetadata }); - } catch { - this.index.deleteDoc(id); - this.index.upsertFile({ id, status: "invalid", ...metadata }); - } - }), - ); - - // Remove index entries and fingerprints for deleted files. - for (const id of knownIds) { - if (!fileIds.has(id)) { - this.index.deleteDoc(id); - this.index.deleteFile(id); - } - } - } - /* v8 ignore stop */ - - private rowToEntry(row: DocRow): CollectionEntry { - return { - id: row.id, - data: JSON.parse(row.data_json) as T, - body: row.body, - createdAt: row.created_at, - updatedAt: row.updated_at, - }; - } - - private extractSnippet(body: string, query: string): string { - const words = query.toLowerCase().split(/\s+/); - const lines = body.split("\n"); - for (const line of lines) { - const lower = line.toLowerCase(); - if (words.some((w) => lower.includes(w))) { - return line.slice(0, 200); - } - } - return body.slice(0, 150); - } -} - -// ─── Service: creates handles for a loaded tool ───────────────────────────── - -export class ToolCollectionServiceClass { - constructor(private readonly handleFactory = collectionHandleFactory) {} - - async setup(tool: LoadedTool): Promise> | undefined> { - const definitions = (tool.definition as ToolDefinitionWithCollections).collections; - if (!definitions || Object.keys(definitions).length === 0) return undefined; - - const toolDir = dirname(tool.path); - return Object.fromEntries( - Object.entries(definitions).map(([name, def]) => { - const collectionName = new CollectionNameClass(name); - const collectionPath = join(toolDir, collectionName.value); - const handle = this.handleFactory.create(collectionName.value, collectionPath, def ?? {}); - return [name, new LazyCollectionHandleClass(handle)]; - }), - ); - } - - close(handles: Record> | undefined): void { - if (!handles) return; - for (const handle of Object.values(handles)) { - (handle as LazyCollectionHandleClass).close(); - } - } -} - -export type ManagedCollectionHandle = CollectionHandle & { - init(): Promise; - close(): void; -}; - -export interface CollectionHandleFactory { - create(name: string, path: string, definition: CollectionDefinition): ManagedCollectionHandle; -} - -export class LazyCollectionHandleClass> implements CollectionHandle { - public readonly name: string; - public readonly path: string; - private initialization?: Promise; - private initializedHandle?: ManagedCollectionHandle; - - public constructor(private readonly handle: ManagedCollectionHandle) { - this.name = handle.name; - this.path = handle.path; - } - - public async create(entry: { id?: string; data: T; body?: string }): Promise> { - return (await this.resource()).create(entry) as Promise>; - } - - public async getEntry(id: string): Promise | null> { - return (await this.resource()).getEntry(id) as Promise | null>; - } - - public async update( - id: string, - patch: { data?: Partial; body?: string }, - ): Promise> { - return (await this.resource()).update(id, patch) as Promise>; - } - - public async upsert(entry: { - id: string; - data: T; - body?: string; - }): Promise<{ id: string; created: boolean }> { - return (await this.resource()).upsert(entry); - } - - public async remove(id: string): Promise { - return (await this.resource()).remove(id); - } - - public async list(opts?: ListOptions): Promise<{ entries: CollectionEntry[]; total: number }> { - return (await this.resource()).list(opts) as Promise<{ - entries: CollectionEntry[]; - total: number; - }>; - } - - public async search( - query: string, - opts?: { limit?: number }, - ): Promise<{ entries: SearchResult[] }> { - return (await this.resource()).search(query, opts) as Promise<{ entries: SearchResult[] }>; - } - - public async count(where?: Record): Promise { - return (await this.resource()).count(where); - } - - public async getCollection( - filter?: (entry: CollectionEntry) => boolean, - ): Promise[]> { - return (await this.resource()).getCollection(filter) as Promise[]>; - } - - public async clear(): Promise { - return (await this.resource()).clear(); - } - - public close(): void { - this.initializedHandle?.close(); - } - - private resource(): Promise { - this.initialization ??= this.initialize(); - return this.initialization; - } - - private async initialize(): Promise { - try { - await this.handle.init(); - this.initializedHandle = this.handle; - return this.handle; - } catch (error) { - this.handle.close(); - throw error; - } - } -} - -export class CollectionHandleFactoryClass implements CollectionHandleFactory { - create(name: string, path: string, definition: CollectionDefinition): ManagedCollectionHandle { - return new CollectionHandleImplClass(name, path, definition); - } -} - -export const collectionHandleFactory = new CollectionHandleFactoryClass(); - -// Internal type for accessing collections on the definition -type ToolDefinitionWithCollections = { - collections?: Record; -}; diff --git a/src/collections/domain/field-path.ts b/src/collections/domain/field-path.ts deleted file mode 100644 index 91b8ad8..0000000 --- a/src/collections/domain/field-path.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; - -export type CompiledCollectionFieldPath = { - readonly segments: readonly string[]; - readonly sqliteJsonPath: string; - read(value: unknown): unknown; -}; - -function invalidFieldPath(value: unknown): InstanceType { - return new RigErrorClass("INPUT_ERROR", `Invalid collection field path: ${String(value)}`, { - expected: "dot-separated field names beginning with a letter or underscore", - }); -} - -export const CollectionFieldPathSingleton = defineSingleton({ - params: {}, - deps: {}, - compile(params: { value: string }): CompiledCollectionFieldPath { - if (typeof params.value !== "string" || params.value.length === 0) { - throw invalidFieldPath(params.value); - } - - const segments = params.value.split("."); - if (segments.some((segment) => !/^[A-Za-z_][A-Za-z0-9_-]*$/.test(segment))) { - throw invalidFieldPath(params.value); - } - - return { - segments, - sqliteJsonPath: `$${segments.map((segment) => `.${segment}`).join("")}`, - read(input: unknown) { - let current = input; - for (const segment of segments) { - if (typeof current !== "object" || current === null || Array.isArray(current)) { - return undefined; - } - current = (current as Record)[segment]; - } - return current; - }, - }; - }, -}); - -type CollectionFieldPathCompilerConstructor = { - new (): CollectionFieldPathCompilerClass; - readonly prototype: CollectionFieldPathCompilerClass; -}; - -export type CollectionFieldPathCompilerClass = { - compile(value: string): CompiledCollectionFieldPath; -}; - -const CollectionFieldPathCompilerClassAdapter = - function constructCollectionFieldPathCompiler(): void {}; -Object.defineProperty(CollectionFieldPathCompilerClassAdapter, "name", { - value: "CollectionFieldPathCompilerClass", -}); -Object.defineProperty(CollectionFieldPathCompilerClassAdapter.prototype, "compile", { - configurable: true, - value: function compile(value: string): CompiledCollectionFieldPath { - return CollectionFieldPathSingleton.compile({ value }); - }, - writable: true, -}); - -export const CollectionFieldPathCompilerClass = - CollectionFieldPathCompilerClassAdapter as unknown as CollectionFieldPathCompilerConstructor; - -export const collectionFieldPathCompiler = new CollectionFieldPathCompilerClass(); diff --git a/src/collections/persistence/memory-index.ts b/src/collections/persistence/memory-index.ts deleted file mode 100644 index 14ec67e..0000000 --- a/src/collections/persistence/memory-index.ts +++ /dev/null @@ -1,296 +0,0 @@ -import { defineService } from "../../define"; -import type { - CollectionEntry, - CollectionFileRecord, - CollectionIndexInterface, - DocRow, - ListOptions, -} from "../application/tool-collection"; -import { collectionFieldPathCompiler } from "../domain/field-path"; - -async function runMemoryExclusiveOperation(params: { operation: () => Promise }): Promise { - return await params.operation(); -} - -async function openMemoryIndex(_params: {}): Promise {} - -function closeMemoryIndex(_params: {}): void {} - -export class MemoryCollectionIndexService extends defineService({ - params: {}, - deps: {}, -}) { - private readonly docs = new Map(); - private readonly files = new Map(); - - public upsertDoc(params: { - entry: CollectionEntry>; - fileMtime: number; - }): void { - this.docs.set(params.entry.id, { - id: params.entry.id, - data_json: JSON.stringify(params.entry.data), - body: params.entry.body, - created_at: params.entry.createdAt, - updated_at: params.entry.updatedAt, - file_mtime: params.fileMtime, - }); - } - - public deleteDoc(params: { id: string }): void { - this.docs.delete(params.id); - } - - public upsertFile(params: { record: CollectionFileRecord }): void { - this.files.set(params.record.id, params.record); - } - - public deleteFile(params: { id: string }): void { - this.files.delete(params.id); - } - - public getFile(params: { id: string }): CollectionFileRecord | null { - return this.files.get(params.id) ?? null; - } - - public allFileIds(_params: {}): string[] { - return [...this.files.keys()]; - } - - public getDoc(params: { id: string }): DocRow | null { - return this.docs.get(params.id) ?? null; - } - - public listDocs(params: { options: ListOptions }): { rows: DocRow[]; total: number } { - const options = params.options; - let rows = [...this.docs.values()]; - - if (options.where && Object.keys(options.where).length > 0) { - rows = rows.filter(function matchesWhere(row) { - const data: unknown = JSON.parse(row.data_json); - return Object.entries(options.where!).every(function matchesEntry([key, value]) { - const actual = collectionFieldPathCompiler.compile(key).read(data); - if (typeof value === "object" && value !== null) { - return JSON.stringify(actual) === JSON.stringify(value); - } - return actual === value; - }); - }); - } - - const total = rows.length; - - if (options.sort) { - const descending = options.sort.startsWith("-"); - const field = descending ? options.sort.slice(1) : options.sort; - const compiledField = collectionFieldPathCompiler.compile(field); - rows.sort(function compareRows(left, right) { - const leftData: unknown = JSON.parse(left.data_json); - const rightData: unknown = JSON.parse(right.data_json); - const leftValue = compiledField.read(leftData) ?? ""; - const rightValue = compiledField.read(rightData) ?? ""; - const comparison = leftValue < rightValue ? -1 : leftValue > rightValue ? 1 : 0; - return descending ? -comparison : comparison; - }); - } else { - rows.sort(function compareUpdatedAt(left, right) { - return right.updated_at > left.updated_at ? 1 : -1; - }); - } - - const offset = options.offset ?? 0; - const limit = options.limit ?? 50; - rows = rows.slice(offset, offset + limit); - return { rows, total }; - } - - public searchDocs(params: { query: string; limit: number }): DocRow[] { - const words = params.query.toLowerCase().split(/\s+/); - const scored: { row: DocRow; rank: number }[] = []; - - for (const row of this.docs.values()) { - const text = `${row.id} ${row.data_json} ${row.body}`.toLowerCase(); - const matches = words.filter(function matchesWord(word) { - return text.includes(word); - }).length; - if (matches > 0) { - scored.push({ - row: { ...row, rank: -matches } as DocRow & { rank: number }, - rank: -matches, - }); - } - } - - scored.sort(function compareRank(left, right) { - return left.rank - right.rank; - }); - return scored.slice(0, params.limit).map(function scoredRow(entry) { - return entry.row; - }); - } - - public countDocs(params: { where?: Record }): number { - if (!params.where || Object.keys(params.where).length === 0) return this.docs.size; - return this.listDocs({ options: { where: params.where } }).total; - } - - public allIds(_params: {}): string[] { - return [...this.docs.keys()]; - } - - public clearAll(_params: {}): void { - this.docs.clear(); - this.files.clear(); - } - - public withExclusiveInitialization(params: { operation: () => Promise }): Promise { - return runMemoryExclusiveOperation(params); - } - - public open(params: {}): Promise { - return openMemoryIndex(params); - } - - public close(params: {}): void { - closeMemoryIndex(params); - } -} - -export type MemoryCollectionIndexClass = CollectionIndexInterface; - -type MemoryCollectionIndexConstructor = { - new (): MemoryCollectionIndexClass; - readonly prototype: MemoryCollectionIndexClass; -}; - -type MemoryCollectionIndexAdapter = MemoryCollectionIndexClass & { - readonly resource: MemoryCollectionIndexService; -}; - -const MemoryCollectionIndexClassAdapter = function constructMemoryCollectionIndex( - this: MemoryCollectionIndexAdapter, -): void { - Object.defineProperty(this, "resource", { - value: new MemoryCollectionIndexService(), - }); -}; -Object.defineProperty(MemoryCollectionIndexClassAdapter, "name", { - value: "MemoryCollectionIndexClass", -}); -Object.defineProperties(MemoryCollectionIndexClassAdapter.prototype, { - withExclusiveInitialization: { - configurable: true, - value: function withExclusiveInitialization( - this: MemoryCollectionIndexAdapter, - params: { operation: () => Promise }, - ) { - return this.resource.withExclusiveInitialization(params); - }, - writable: true, - }, - open: { - configurable: true, - value: function open(this: MemoryCollectionIndexAdapter) { - return this.resource.open({}); - }, - writable: true, - }, - upsertDoc: { - configurable: true, - value: function upsertDoc( - this: MemoryCollectionIndexAdapter, - entry: CollectionEntry>, - fileMtime: number, - ) { - return this.resource.upsertDoc({ entry, fileMtime }); - }, - writable: true, - }, - deleteDoc: { - configurable: true, - value: function deleteDoc(this: MemoryCollectionIndexAdapter, id: string) { - return this.resource.deleteDoc({ id }); - }, - writable: true, - }, - upsertFile: { - configurable: true, - value: function upsertFile(this: MemoryCollectionIndexAdapter, record: CollectionFileRecord) { - return this.resource.upsertFile({ record }); - }, - writable: true, - }, - deleteFile: { - configurable: true, - value: function deleteFile(this: MemoryCollectionIndexAdapter, id: string) { - return this.resource.deleteFile({ id }); - }, - writable: true, - }, - getFile: { - configurable: true, - value: function getFile(this: MemoryCollectionIndexAdapter, id: string) { - return this.resource.getFile({ id }); - }, - writable: true, - }, - allFileIds: { - configurable: true, - value: function allFileIds(this: MemoryCollectionIndexAdapter) { - return this.resource.allFileIds({}); - }, - writable: true, - }, - getDoc: { - configurable: true, - value: function getDoc(this: MemoryCollectionIndexAdapter, id: string) { - return this.resource.getDoc({ id }); - }, - writable: true, - }, - listDocs: { - configurable: true, - value: function listDocs(this: MemoryCollectionIndexAdapter, options: ListOptions) { - return this.resource.listDocs({ options }); - }, - writable: true, - }, - searchDocs: { - configurable: true, - value: function searchDocs(this: MemoryCollectionIndexAdapter, query: string, limit: number) { - return this.resource.searchDocs({ query, limit }); - }, - writable: true, - }, - countDocs: { - configurable: true, - value: function countDocs(this: MemoryCollectionIndexAdapter, where?: Record) { - return this.resource.countDocs({ where }); - }, - writable: true, - }, - allIds: { - configurable: true, - value: function allIds(this: MemoryCollectionIndexAdapter) { - return this.resource.allIds({}); - }, - writable: true, - }, - clearAll: { - configurable: true, - value: function clearAll(this: MemoryCollectionIndexAdapter) { - return this.resource.clearAll({}); - }, - writable: true, - }, - close: { - configurable: true, - value: function close(this: MemoryCollectionIndexAdapter) { - return this.resource.close({}); - }, - writable: true, - }, -}); - -export const MemoryCollectionIndexClass = - MemoryCollectionIndexClassAdapter as unknown as MemoryCollectionIndexConstructor; diff --git a/src/config/application/config-store.ts b/src/config/application/config-store.ts deleted file mode 100644 index 6b575ac..0000000 --- a/src/config/application/config-store.ts +++ /dev/null @@ -1,293 +0,0 @@ -import { existsSync } from "node:fs"; -import { mkdir, readFile } from "node:fs/promises"; -import { defineService } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import { RuntimeSupportClass } from "../../runtime/support"; -import { - rigConfigDefaults, - RigConfigSchema, - type RigConfig, -} from "../../domains/settings/index.ts"; -import { AtomicFileWriterClass, BoundedFileLockClass, type FileLockOptions } from "../file-lock"; -import { - RigDirectoryMigrationServiceClass, - RigMigrationPromptStoreClass, - type RigDirectoryMigrationResult, -} from "../migration"; -import { RigPathsClass, type PathOptions } from "../paths"; - -export type ConfigOptions = PathOptions & { - configLock?: FileLockOptions; -}; - -export type RigConfigMutator = (config: RigConfig) => RigConfig | Promise; - -export type RegistryEntry = { - kind: "base" | "custom"; - path: string; -}; - -type RigConfigPaths = Pick< - RigPathsClass, - "configPath" | "defaultBaseRegistryDir" | "resolve" | "rigDir" ->; -type RigConfigRuntimeSupport = Pick; -type RigConfigLock = Pick; -type RigConfigWriter = Pick; - -type RigConfigStoreServiceDeps = { - createPaths: (options: ConfigOptions) => RigConfigPaths; - createRuntimeSupport: (options: ConfigOptions) => RigConfigRuntimeSupport; - createLock: (params: { path: string; options?: FileLockOptions }) => RigConfigLock; - createWriter: () => RigConfigWriter; - migrateDirectory: (paths: RigPathsClass) => Promise; - markMigrationPrompted: (params: { paths: RigPathsClass; promptId: string }) => Promise; - mkdir: typeof mkdir; - exists: typeof existsSync; - readFile: typeof readFile; -}; - -const RigConfigStoreServiceProductionDeps: RigConfigStoreServiceDeps = { - createPaths(options) { - return new RigPathsClass(options); - }, - createRuntimeSupport(options) { - return new RuntimeSupportClass(options); - }, - createLock(params) { - return new BoundedFileLockClass(params.path, params.options); - }, - createWriter() { - return new AtomicFileWriterClass(); - }, - async migrateDirectory(paths) { - return await new RigDirectoryMigrationServiceClass(paths).migrateIfNeeded(); - }, - async markMigrationPrompted(params) { - await new RigMigrationPromptStoreClass(params.paths).markPrompted(params.promptId); - }, - mkdir, - exists: existsSync, - readFile, -}; - -export class RigConfigStoreService extends defineService({ - params: {} as ConfigOptions, - deps: RigConfigStoreServiceProductionDeps, -}) { - private readonly paths = this.deps.createPaths(this.params); - private readonly runtimeSupport = this.deps.createRuntimeSupport(this.params); - private readonly lock = this.deps.createLock({ - path: this.paths.configPath, - options: this.params.configLock, - }); - private readonly writer = this.deps.createWriter(); - private migration: RigDirectoryMigrationResult | undefined; - - public migrationResult(_params: {}): RigDirectoryMigrationResult | undefined { - return this.migration; - } - - public async acknowledgeMigrationPrompt(_params: {}): Promise { - if (this.migration?.status !== "manual") return; - await this.deps.markMigrationPrompted({ - paths: this.paths as RigPathsClass, - promptId: this.migration.promptId, - }); - } - - public async read(_params: {}): Promise { - let raw: string; - try { - raw = await this.deps.readFile(this.paths.configPath, "utf8"); - } catch (error) { - throw new RigErrorClass( - "CONFIG_INVALID", - `Could not read config at ${this.paths.configPath}.`, - { error }, - ); - } - - let parsed: unknown; - try { - parsed = JSON.parse(raw); - } catch (error) { - throw new RigErrorClass( - "CONFIG_INVALID", - `Config is not valid JSON at ${this.paths.configPath}.`, - { error }, - ); - } - - const result = RigConfigSchema.safeParse(parsed); - if (!result.success) { - throw new RigErrorClass("CONFIG_INVALID", "Rig config is invalid.", result.error.flatten()); - } - return result.data; - } - - private async writeUnlocked(params: { config: RigConfig }): Promise { - const result = RigConfigSchema.safeParse(params.config); - if (!result.success) { - throw new RigErrorClass("CONFIG_INVALID", "Rig config is invalid.", result.error.flatten()); - } - - await this.writer.write(this.paths.configPath, `${JSON.stringify(result.data, null, 2)}\n`); - return result.data; - } - - public resolvedBaseRegistry(params: { config: RigConfig }): string { - return this.paths.resolve(params.config.baseRegistryDir || this.paths.defaultBaseRegistryDir); - } - - public resolvedCustomRegistries(params: { config: RigConfig }): string[] { - return params.config.customRegistries.map((pathValue) => this.paths.resolve(pathValue)); - } - - public registryEntries(params: { config: RigConfig }): RegistryEntry[] { - return [ - { kind: "base", path: this.resolvedBaseRegistry(params) }, - ...this.resolvedCustomRegistries(params).map(function customRegistry(path) { - return { kind: "custom" as const, path }; - }), - ]; - } - - public async ensure(_params: {}): Promise { - this.migration = await this.deps.migrateDirectory(this.paths as RigPathsClass); - await this.deps.mkdir(this.paths.rigDir, { recursive: true }); - if (!this.deps.exists(this.paths.configPath)) { - await this.lock.run(async () => { - if (!this.deps.exists(this.paths.configPath)) { - await this.writeUnlocked({ config: rigConfigDefaults.create() }); - } - }); - } - - const rigConfig = await this.read({}); - const registries = this.registryEntries({ config: rigConfig }); - await this.deps.mkdir(this.resolvedBaseRegistry({ config: rigConfig }), { recursive: true }); - await this.runtimeSupport.ensure( - registries.map(function registryPath(registry) { - return registry.path; - }), - ); - return rigConfig; - } - - public async write(params: { config: RigConfig }): Promise { - await this.lock.run(() => this.writeUnlocked(params)); - } - - public async update(params: { mutator: RigConfigMutator }): Promise { - return await this.lock.run(async () => { - const current = this.deps.exists(this.paths.configPath) - ? await this.read({}) - : rigConfigDefaults.create(); - const next = await params.mutator(current); - return await this.writeUnlocked({ config: next }); - }); - } -} - -export type RigConfigStoreClass = { - migrationResult(): RigDirectoryMigrationResult | undefined; - acknowledgeMigrationPrompt(): Promise; - ensure(): Promise; - read(): Promise; - write(config: RigConfig): Promise; - update(mutator: RigConfigMutator): Promise; - resolvedBaseRegistry(config: RigConfig): string; - resolvedCustomRegistries(config: RigConfig): string[]; - registryEntries(config: RigConfig): RegistryEntry[]; -}; - -type RigConfigStoreConstructor = { - new (options?: ConfigOptions): RigConfigStoreClass; - readonly prototype: RigConfigStoreClass; -}; - -type RigConfigStoreAdapter = RigConfigStoreClass & { - readonly resource: RigConfigStoreService; -}; - -const RigConfigStoreClassAdapter = function constructRigConfigStore( - this: RigConfigStoreAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new RigConfigStoreService({ - params: options, - deps: RigConfigStoreServiceProductionDeps, - }), - }); -}; -Object.defineProperty(RigConfigStoreClassAdapter, "name", { value: "RigConfigStoreClass" }); -Object.defineProperties(RigConfigStoreClassAdapter.prototype, { - migrationResult: { - configurable: true, - value: function migrationResult(this: RigConfigStoreAdapter) { - return this.resource.migrationResult({}); - }, - writable: true, - }, - acknowledgeMigrationPrompt: { - configurable: true, - value: function acknowledgeMigrationPrompt(this: RigConfigStoreAdapter) { - return this.resource.acknowledgeMigrationPrompt({}); - }, - writable: true, - }, - ensure: { - configurable: true, - value: function ensure(this: RigConfigStoreAdapter) { - return this.resource.ensure({}); - }, - writable: true, - }, - read: { - configurable: true, - value: function read(this: RigConfigStoreAdapter) { - return this.resource.read({}); - }, - writable: true, - }, - write: { - configurable: true, - value: function write(this: RigConfigStoreAdapter, config: RigConfig) { - return this.resource.write({ config }); - }, - writable: true, - }, - update: { - configurable: true, - value: function update(this: RigConfigStoreAdapter, mutator: RigConfigMutator) { - return this.resource.update({ mutator }); - }, - writable: true, - }, - resolvedBaseRegistry: { - configurable: true, - value: function resolvedBaseRegistry(this: RigConfigStoreAdapter, config: RigConfig) { - return this.resource.resolvedBaseRegistry({ config }); - }, - writable: true, - }, - resolvedCustomRegistries: { - configurable: true, - value: function resolvedCustomRegistries(this: RigConfigStoreAdapter, config: RigConfig) { - return this.resource.resolvedCustomRegistries({ config }); - }, - writable: true, - }, - registryEntries: { - configurable: true, - value: function registryEntries(this: RigConfigStoreAdapter, config: RigConfig) { - return this.resource.registryEntries({ config }); - }, - writable: true, - }, -}); - -export const RigConfigStoreClass = - RigConfigStoreClassAdapter as unknown as RigConfigStoreConstructor; diff --git a/src/config/config.test.ts b/src/config/config.test.ts deleted file mode 100644 index 56b9682..0000000 --- a/src/config/config.test.ts +++ /dev/null @@ -1,412 +0,0 @@ -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { spawn } from "node:child_process"; -import { existsSync } from "node:fs"; -import { mkdir, mkdtemp, readFile, readdir, rm, utimes, writeFile } from "node:fs/promises"; -import { hostname, tmpdir } from "node:os"; -import { join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { RigConfigStoreClass } from "./config"; -import { - RigDirectoryMigrationServiceClass, - RigHomeDirectoryMigrationPromptId, - RigMigrationPromptStoreClass, -} from "./migration"; -import { RigPathsClass } from "./paths"; - -class TestHomeStore { - private readonly homes: string[] = []; - - async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-test-home-")); - this.homes.push(home); - return home; - } - - async cleanup(): Promise { - await Promise.all( - this.homes.splice(0).map((home) => rm(home, { recursive: true, force: true })), - ); - } -} - -const homes = new TestHomeStore(); - -afterEach(async () => { - await homes.cleanup(); -}); - -describe("config", () => { - test("expands tilde paths", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - expect(paths.expandTilde("~/rig/tools")).toBe(join(home, "rig/tools")); - expect(paths.legacyRigDir).toBe(join(home, ".rig")); - expect(paths.legacyDefaultBaseRegistryDir).toBe("~/.rig/tools"); - expect(paths.migrationPromptStatePath).toBe(join(home, "rig", "migration-prompts.json")); - }); - - test("creates first-run config and base registry in a temp home", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const store = new RigConfigStoreClass({ homeDir: home }); - const config = await store.ensure(); - expect(config.version).toBe(1); - expect(store.migrationResult()).toBeUndefined(); - expect(existsSync(paths.configPath)).toBe(true); - expect(existsSync(join(home, "rig/tools"))).toBe(true); - expect(existsSync(join(home, "rig/tools/tsconfig.json"))).toBe(true); - expect(existsSync(join(home, "rig/runtime/sdk.ts"))).toBe(true); - expect(existsSync(join(home, "rig/runtime/globals.d.ts"))).toBe(true); - }); - - test("migrates legacy .rig state to the visible rig folder", async () => { - const home = await homes.create(); - const legacyDir = join(home, ".rig"); - await mkdir(join(legacyDir, "tools", "legacy"), { recursive: true }); - await writeFile( - join(legacyDir, "rig.json"), - `${JSON.stringify({ version: 1, baseRegistryDir: "~/.rig/tools", customRegistries: [] })}\n`, - "utf8", - ); - await writeFile(join(legacyDir, "tools", "legacy", "index.rig.ts"), "export default {};\n"); - - const store = new RigConfigStoreClass({ homeDir: home }); - const config = await store.ensure(); - - expect(store.migrationResult()).toMatchObject({ - promptId: RigHomeDirectoryMigrationPromptId, - status: "migrated", - legacyDir, - currentDir: join(home, "rig"), - configUpdated: true, - }); - expect(config.baseRegistryDir).toBe("~/rig/tools"); - expect(existsSync(legacyDir)).toBe(false); - expect(existsSync(join(home, "rig", "tools", "legacy", "index.rig.ts"))).toBe(true); - expect(await readFile(join(home, "rig", "rig.json"), "utf8")).toContain("~/rig/tools"); - }); - - test("replaces a generated default rig folder during legacy migration", async () => { - const home = await homes.create(); - const legacyDir = join(home, ".rig"); - await mkdir(join(legacyDir, "tools", "legacy"), { recursive: true }); - await writeFile( - join(legacyDir, "rig.json"), - `${JSON.stringify({ version: 1, baseRegistryDir: "~/custom-tools", customRegistries: [] })}\n`, - "utf8", - ); - await mkdir(join(home, "rig", "tools"), { recursive: true }); - await writeFile( - join(home, "rig", "rig.json"), - `${JSON.stringify({ - version: 1, - baseRegistryDir: "~/rig/tools", - customRegistries: [], - cronJobs: [], - })}\n`, - "utf8", - ); - await writeFile(join(home, "rig", "tools", "tsconfig.json"), "// Generated by Rig.\n"); - await mkdir(join(home, "rig", "cron")); - await writeFile(join(home, "rig", "update-check.json"), "{}\n"); - await writeFile(join(home, "rig", "migration-prompts.json"), "{}\n"); - - const store = new RigConfigStoreClass({ homeDir: home }); - const config = await store.ensure(); - - expect(store.migrationResult()).toMatchObject({ status: "migrated", configUpdated: false }); - expect(config.baseRegistryDir).toBe("~/custom-tools"); - expect(existsSync(join(home, "rig", "tools", "legacy"))).toBe(true); - }); - - test("leaves legacy state in place when the new rig folder has user data", async () => { - const home = await homes.create(); - const legacyDir = join(home, ".rig"); - await mkdir(join(legacyDir, "tools", "legacy"), { recursive: true }); - await writeFile( - join(legacyDir, "rig.json"), - `${JSON.stringify({ version: 1, baseRegistryDir: "~/.rig/tools", customRegistries: [] })}\n`, - "utf8", - ); - await mkdir(join(home, "rig", "tools", "current"), { recursive: true }); - await writeFile( - join(home, "rig", "rig.json"), - `${JSON.stringify({ - version: 1, - baseRegistryDir: "~/rig/tools", - customRegistries: [], - cronJobs: [], - })}\n`, - "utf8", - ); - - const store = new RigConfigStoreClass({ homeDir: home }); - const config = await store.ensure(); - - expect(store.migrationResult()).toMatchObject({ - promptId: RigHomeDirectoryMigrationPromptId, - status: "manual", - legacyDir, - currentDir: join(home, "rig"), - configUpdated: false, - }); - expect(config.baseRegistryDir).toBe("~/rig/tools"); - expect(existsSync(legacyDir)).toBe(true); - expect(existsSync(join(home, "rig", "tools", "current"))).toBe(true); - - await store.acknowledgeMigrationPrompt(); - const promptStatePath = new RigPathsClass({ homeDir: home }).migrationPromptStatePath; - const promptState = await readFile(promptStatePath, "utf8"); - expect(promptState).toContain(RigHomeDirectoryMigrationPromptId); - await store.acknowledgeMigrationPrompt(); - expect(await readFile(promptStatePath, "utf8")).toBe(promptState); - - const nextStore = new RigConfigStoreClass({ homeDir: home }); - await expect(nextStore.ensure()).resolves.toMatchObject({ baseRegistryDir: "~/rig/tools" }); - expect(nextStore.migrationResult()).toBeUndefined(); - await nextStore.acknowledgeMigrationPrompt(); - }); - - test("handles legacy migration edge cases conservatively", async () => { - const emptyLegacyHome = await homes.create(); - await mkdir(join(emptyLegacyHome, ".rig")); - await expect( - new RigDirectoryMigrationServiceClass( - new RigPathsClass({ homeDir: emptyLegacyHome }), - ).migrateIfNeeded(), - ).resolves.toBeUndefined(); - - const toolsOnlyHome = await homes.create(); - await mkdir(join(toolsOnlyHome, ".rig", "tools", "legacy"), { recursive: true }); - await expect( - new RigDirectoryMigrationServiceClass( - new RigPathsClass({ homeDir: toolsOnlyHome }), - ).migrateIfNeeded(), - ).resolves.toMatchObject({ status: "migrated", configUpdated: false }); - expect(existsSync(join(toolsOnlyHome, "rig", "tools", "legacy"))).toBe(true); - - const invalidCurrentHome = await homes.create(); - await mkdir(join(invalidCurrentHome, ".rig", "tools"), { recursive: true }); - await mkdir(join(invalidCurrentHome, "rig")); - await writeFile(join(invalidCurrentHome, "rig", "rig.json"), "{ nope", "utf8"); - await expect( - new RigDirectoryMigrationServiceClass( - new RigPathsClass({ homeDir: invalidCurrentHome }), - ).migrateIfNeeded(), - ).resolves.toMatchObject({ status: "manual" }); - - const cronConflictHome = await homes.create(); - await mkdir(join(cronConflictHome, ".rig", "tools"), { recursive: true }); - await mkdir(join(cronConflictHome, "rig", "cron"), { recursive: true }); - await writeFile( - join(cronConflictHome, "rig", "rig.json"), - `${JSON.stringify({ - version: 1, - baseRegistryDir: "~/rig/tools", - customRegistries: [], - cronJobs: [], - })}\n`, - "utf8", - ); - await writeFile(join(cronConflictHome, "rig", "cron", "job.ts"), "// job\n", "utf8"); - await expect( - new RigDirectoryMigrationServiceClass( - new RigPathsClass({ homeDir: cronConflictHome }), - ).migrateIfNeeded(), - ).resolves.toMatchObject({ status: "manual" }); - - const extraFileHome = await homes.create(); - await mkdir(join(extraFileHome, ".rig", "tools"), { recursive: true }); - await mkdir(join(extraFileHome, "rig")); - await writeFile( - join(extraFileHome, "rig", "rig.json"), - `${JSON.stringify({ - version: 1, - baseRegistryDir: "~/rig/tools", - customRegistries: [], - cronJobs: [], - })}\n`, - "utf8", - ); - await writeFile(join(extraFileHome, "rig", "notes.txt"), "mine\n", "utf8"); - await expect( - new RigDirectoryMigrationServiceClass( - new RigPathsClass({ homeDir: extraFileHome }), - ).migrateIfNeeded(), - ).resolves.toMatchObject({ status: "manual" }); - - const invalidPromptStateHome = await homes.create(); - const invalidPromptStatePaths = new RigPathsClass({ homeDir: invalidPromptStateHome }); - await mkdir(invalidPromptStatePaths.rigDir, { recursive: true }); - await writeFile(invalidPromptStatePaths.migrationPromptStatePath, "[]\n", "utf8"); - const promptStore = new RigMigrationPromptStoreClass(invalidPromptStatePaths); - await expect(promptStore.hasPrompted("custom-prompt")).resolves.toBe(false); - await promptStore.markPrompted("custom-prompt"); - await expect(promptStore.hasPrompted("custom-prompt")).resolves.toBe(true); - }); - - test("serializes same-process updates without lost config changes", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - await new RigConfigStoreClass({ homeDir: home }).ensure(); - - await Promise.all( - Array.from({ length: 20 }, (_, index) => - new RigConfigStoreClass({ homeDir: home }).update(async (config) => { - await new Promise((resolve) => setTimeout(resolve, 1)); - return { - ...config, - customRegistries: [...config.customRegistries, `registry-${index}`], - }; - }), - ), - ); - - const config = await new RigConfigStoreClass({ homeDir: home }).read(); - expect(config.customRegistries.toSorted()).toEqual( - Array.from({ length: 20 }, (_, index) => `registry-${index}`).toSorted(), - ); - expect((await readdir(paths.rigDir)).filter((entry) => entry.includes(".tmp-"))).toEqual([]); - expect(existsSync(`${paths.configPath}.lock`)).toBe(false); - }); - - test("serializes disposable-process updates without temp collisions or lost changes", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - await new RigConfigStoreClass({ homeDir: home }).ensure(); - const configModule = pathToFileURL(join(process.cwd(), "src", "config", "config.ts")).href; - const script = ` -import { RigConfigStoreClass } from ${JSON.stringify(configModule)}; -const store = new RigConfigStoreClass({ homeDir: process.env.RIG_CONCURRENCY_HOME }); -await store.update(async (config) => { - await Bun.sleep(5); - return { ...config, customRegistries: [...config.customRegistries, process.env.RIG_CONCURRENCY_ID] }; -}); -`; - - const processes = Array.from({ length: 8 }, (_, index) => - spawn("bun", ["--eval", script], { - cwd: process.cwd(), - env: { - ...process.env, - RIG_CONCURRENCY_HOME: home, - RIG_CONCURRENCY_ID: `process-${index}`, - }, - }), - ); - const results = await Promise.all( - processes.map( - (child) => - new Promise<{ exitCode: number; stderr: string }>((resolve) => { - let stderr = ""; - child.stderr.setEncoding("utf8"); - child.stderr.on("data", (chunk: string) => { - stderr += chunk; - }); - child.on("close", (code) => resolve({ exitCode: code ?? 1, stderr })); - }), - ), - ); - - expect(results).toEqual(results.map(() => ({ exitCode: 0, stderr: "" }))); - const config = await new RigConfigStoreClass({ homeDir: home }).read(); - expect(config.customRegistries.toSorted()).toEqual( - Array.from({ length: 8 }, (_, index) => `process-${index}`).toSorted(), - ); - expect((await readdir(paths.rigDir)).filter((entry) => entry.includes(".tmp-"))).toEqual([]); - expect(existsSync(`${paths.configPath}.lock`)).toBe(false); - }); - - test("times out on a live lock and recovers a stale dead-owner lock", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - await new RigConfigStoreClass({ homeDir: home }).ensure(); - const lockPath = `${paths.configPath}.lock`; - const ownerPath = join(lockPath, "owner.json"); - await mkdir(lockPath); - await writeFile( - ownerPath, - `${JSON.stringify({ - token: "live", - pid: process.pid, - hostname: hostname(), - acquiredAt: new Date(0).toISOString(), - })}\n`, - "utf8", - ); - await utimes(lockPath, new Date(0), new Date(0)); - - const boundedStore = new RigConfigStoreClass({ - homeDir: home, - configLock: { timeoutMs: 30, retryMs: 5, staleMs: 1 }, - }); - await expect(boundedStore.update((config) => config)).rejects.toThrow( - "Timed out waiting for lock", - ); - - await writeFile( - ownerPath, - `${JSON.stringify({ - token: "dead", - pid: 2_147_483_647, - hostname: hostname(), - acquiredAt: new Date(0).toISOString(), - })}\n`, - "utf8", - ); - await utimes(lockPath, new Date(0), new Date(0)); - await expect( - boundedStore.update((config) => ({ - ...config, - customRegistries: [...config.customRegistries, "recovered"], - })), - ).resolves.toMatchObject({ customRegistries: ["recovered"] }); - expect(existsSync(lockPath)).toBe(false); - }); - - test("releases the lock when a mutator fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const store = new RigConfigStoreClass({ homeDir: home }); - const original = await store.ensure(); - - await expect( - store.update(() => { - throw new Error("mutator failed"); - }), - ).rejects.toThrow("mutator failed"); - expect(await store.read()).toEqual(original); - expect(existsSync(`${paths.configPath}.lock`)).toBe(false); - await expect( - store.update((config) => ({ ...config, customRegistries: ["after-failure"] })), - ).resolves.toMatchObject({ customRegistries: ["after-failure"] }); - }); - - test("initializes config through update when ensure was not called", async () => { - const home = await homes.create(); - const store = new RigConfigStoreClass({ homeDir: home }); - - await expect( - store.update((config) => ({ ...config, customRegistries: ["initialized-by-update"] })), - ).resolves.toMatchObject({ - version: 1, - baseRegistryDir: "~/rig/tools", - customRegistries: ["initialized-by-update"], - }); - }); - - test("serializes concurrent migration prompt updates", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const store = new RigMigrationPromptStoreClass(paths); - const promptIds = Array.from({ length: 12 }, (_, index) => `prompt-${index}`); - - await Promise.all(promptIds.map((promptId) => store.markPrompted(promptId))); - - await expect( - Promise.all(promptIds.map((promptId) => store.hasPrompted(promptId))), - ).resolves.toEqual(promptIds.map(() => true)); - expect(existsSync(`${paths.migrationPromptStatePath}.lock`)).toBe(false); - expect((await readdir(paths.rigDir)).filter((entry) => entry.includes(".tmp-"))).toEqual([]); - }); -}); diff --git a/src/config/config.ts b/src/config/config.ts deleted file mode 100644 index 8a61b8f..0000000 --- a/src/config/config.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./application/config-store"; diff --git a/src/config/file-lock.ts b/src/config/file-lock.ts deleted file mode 100644 index 74b2d67..0000000 --- a/src/config/file-lock.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../domains/settings/index.ts"; diff --git a/src/config/migration.ts b/src/config/migration.ts deleted file mode 100644 index 74b2d67..0000000 --- a/src/config/migration.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../domains/settings/index.ts"; diff --git a/src/config/paths.ts b/src/config/paths.ts deleted file mode 100644 index 74b2d67..0000000 --- a/src/config/paths.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../domains/settings/index.ts"; diff --git a/src/config/schema.ts b/src/config/schema.ts deleted file mode 100644 index 74b2d67..0000000 --- a/src/config/schema.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../domains/settings/index.ts"; diff --git a/src/cron/application/rig-cron.ts b/src/cron/application/rig-cron.ts deleted file mode 100644 index 7fe7ad5..0000000 --- a/src/cron/application/rig-cron.ts +++ /dev/null @@ -1,768 +0,0 @@ -import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; -import { dirname } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigConfigStoreClass } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import type { RigCronJob } from "../../config/schema"; -import { RigErrorClass } from "../../errors/RigError"; -import type { SuccessEnvelope } from "../../runtime/envelope"; -import { commandTargets } from "../../tools/identifiers"; -import { ToolRunnerClass } from "../../tools/run"; - -export type CronAddOptions = { - name: string; - command: string; - schedule: string; - input?: string; - inputFile?: string; - moduleUrl: string; -}; - -export type CronRunResult = { - job: RigCronJob; - envelope: unknown; - exitCode: number; -}; - -type CronControllerLike = { - cron?: string; - scheduledTime?: number; - type?: string; -}; - -type BunCronApi = { - (path: string, schedule: string, title: string): Promise; - parse(expression: string): Date | null; - remove(title: string): Promise; -}; - -export type CronRegistrar = { - register(params: { path: string; schedule: string; title: string }): Promise; - remove(params: { title: string }): Promise; - validate(params: { schedule: string }): void; -}; - -export type RigCronServiceParams = ConfigOptions; - -export type RigCronServiceDeps = { - registrar?: CronRegistrar; -}; - -export type RigCronWorkerParams = ConfigOptions; - -export type RigCronWorkerDeps = { - service?: RigCronServiceClass; -}; - -function getBunCron(_params: {}): BunCronApi { - const cron = (globalThis as typeof globalThis & { Bun?: { cron?: unknown } }).Bun?.cron; - /* v8 ignore next 3 */ - if (typeof cron !== "function") { - throw new RigErrorClass("CRON_ERROR", "Bun cron is unavailable. Run rig with Bun."); - } - return cron as BunCronApi; -} - -export class BunCronRegistrarService extends defineService({ - params: {}, - deps: { getCron: getBunCron }, -}) { - public register(params: { path: string; schedule: string; title: string }): Promise { - return this.deps.getCron({})(params.path, params.schedule, params.title); - } - - public remove(params: { title: string }): Promise { - return this.deps.getCron({}).remove(params.title); - } - - public validate(params: { schedule: string }): void { - const next = this.deps.getCron({}).parse(params.schedule); - if (!next) { - throw new RigErrorClass("CRON_ERROR", `Cron schedule has no future runs: ${params.schedule}`); - } - } -} - -export const BunCronRegistrar = new BunCronRegistrarService(); - -function parseCronJobName(params: { value: string }): { value: string } { - if (!/^[A-Za-z0-9_-]+$/.test(params.value)) { - throw new RigErrorClass( - "INPUT_ERROR", - "Cron job names may only contain letters, numbers, hyphens, and underscores.", - { name: params.value }, - ); - } - return { value: params.value }; -} - -export const CronJobNameSingleton = defineSingleton({ - params: {}, - deps: {}, - parse: parseCronJobName, -}); - -async function readJsonFile(params: { path: string }): Promise { - /* v8 ignore next 3 */ - return typeof Bun !== "undefined" - ? Bun.file(params.path).json() - : JSON.parse(await readFile(params.path, "utf8")); -} - -export class CronInputReaderService extends defineService({ - params: {}, - deps: { readJsonFile }, -}) { - public async read(params: { input?: string; inputFile?: string }): Promise { - if (params.input && params.inputFile) { - throw new RigErrorClass("INPUT_ERROR", "Use --input or --input-file, not both."); - } - if (params.inputFile) return await this.deps.readJsonFile({ path: params.inputFile }); - return params.input === undefined ? undefined : JSON.parse(params.input); - } -} - -export const CronInputReader = new CronInputReaderService(); - -function parseCronCommandTarget(params: { id: string }): { - id: string; - tool: string; - command: string; -} { - const target = commandTargets.parse(params.id); - return { id: params.id, tool: target.tool, command: target.command }; -} - -export const CronCommandTargetSingleton = defineSingleton({ - params: {}, - deps: {}, - parse: parseCronCommandTarget, -}); - -export class CronWorkerScriptService extends defineService({ - params: {}, - deps: { fileUrlToPath: fileURLToPath }, -}) { - public render(params: { name: string; homeDir?: string; moduleUrl: string }): string { - const entrypoint = this.deps.fileUrlToPath(params.moduleUrl); - /* v8 ignore next */ - const env = params.homeDir ? { RIG_HOME: params.homeDir } : {}; - return `export default { - async scheduled() { - const proc = Bun.spawn([ - process.execPath, - "--install=fallback", - ${JSON.stringify(entrypoint)}, - "cron", - "run", - ${JSON.stringify(params.name)}, - ], { - stdout: "pipe", - stderr: "pipe", - env: { ...process.env, ...${JSON.stringify(env)} }, - }); - - const [stdout, stderr, exitCode] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]); - if (stdout) console.log(stdout.trimEnd()); - if (stderr) console.error(stderr.trimEnd()); - if (exitCode !== 0) throw new Error("Rig cron job failed: " + ${JSON.stringify(params.name)}); - }, -}; -`; - } -} - -export const CronWorkerScript = new CronWorkerScriptService(); - -type CronWorkerSnapshot = { - workerSource?: string; -}; - -type CronConfigStore = Pick; - -type CronStateTransactionDeps = { - configStore: CronConfigStore; - registrar: CronRegistrar; - readFile: typeof readFile; - writeFile: typeof writeFile; - mkdir: typeof mkdir; - rm: typeof rm; - dirname: typeof dirname; -}; - -const CronStateTransactionProductionDeps: CronStateTransactionDeps = { - configStore: new RigConfigStoreClass({}), - registrar: BunCronRegistrar, - readFile, - writeFile, - mkdir, - rm, - dirname, -}; - -function sameJob(params: { left: RigCronJob; right: RigCronJob }): boolean { - return JSON.stringify(params.left) === JSON.stringify(params.right); -} - -function errorMessage(params: { error: unknown }): string { - return params.error instanceof Error ? params.error.message : String(params.error); -} - -function isMissingFile(params: { error: unknown }): boolean { - return ( - typeof params.error === "object" && - params.error !== null && - "code" in params.error && - (params.error as { code?: unknown }).code === "ENOENT" - ); -} - -async function captureRollbackError(params: { - rollbackErrors: unknown[]; - rollback: () => Promise; -}): Promise { - try { - await params.rollback(); - } catch (error) { - params.rollbackErrors.push(error); - } -} - -export class CronStateTransactionService extends defineService({ - params: {}, - deps: CronStateTransactionProductionDeps, -}) { - private async writeWorker(params: { workerPath: string; source: string }): Promise { - await this.deps.mkdir(this.deps.dirname(params.workerPath), { recursive: true }); - await this.deps.writeFile(params.workerPath, params.source, "utf8"); - } - - private async readWorker(params: { workerPath: string }): Promise { - try { - return await this.deps.readFile(params.workerPath, "utf8"); - } catch (error) { - /* v8 ignore else -- non-ENOENT read failures are propagated unchanged */ - if (isMissingFile({ error })) return undefined; - /* v8 ignore next */ - throw error; - } - } - - public async snapshot(params: { workerPath: string }): Promise { - return { workerSource: await this.readWorker(params) }; - } - - public async restoreReplacedJob(params: { - job: RigCronJob; - previousJob?: RigCronJob; - }): Promise { - await this.deps.configStore.update(function restoreConfig(config) { - const current = config.cronJobs.find((candidate) => candidate.name === params.job.name); - if (!current || !sameJob({ left: current, right: params.job })) return config; - const withoutReplacement = config.cronJobs.filter( - (candidate) => candidate.name !== params.job.name, - ); - return { - ...config, - cronJobs: params.previousJob - ? [...withoutReplacement, params.previousJob] - : withoutReplacement, - }; - }); - } - - public async restoreRemovedJob(params: { previousJob: RigCronJob }): Promise { - await this.deps.configStore.update(function restoreConfig(config) { - if (config.cronJobs.some((job) => job.name === params.previousJob.name)) return config; - return { ...config, cronJobs: [...config.cronJobs, params.previousJob] }; - }); - } - - private async restoreWorker(params: { - snapshot: CronWorkerSnapshot; - workerPath: string; - }): Promise { - if (params.snapshot.workerSource === undefined) { - await this.deps.rm(params.workerPath, { force: true }); - return; - } - await this.writeWorker({ - workerPath: params.workerPath, - source: params.snapshot.workerSource, - }); - } - - private throwAfterRollback(params: { error: unknown; rollbackErrors: unknown[] }): never { - if (params.rollbackErrors.length === 0) throw params.error; - throw new RigErrorClass("CRON_ERROR", "Cron state rollback was incomplete.", { - cause: errorMessage({ error: params.error }), - rollbackErrors: params.rollbackErrors.map(function formatRollbackError(rollbackError) { - return errorMessage({ error: rollbackError }); - }), - }); - } - - public async replace(params: { - snapshot: CronWorkerSnapshot; - workerPath: string; - workerSource: string; - job: RigCronJob; - }): Promise { - let previousJob: RigCronJob | undefined; - let workerChanged = false; - let configChanged = false; - let registrationAttempted = false; - try { - await this.writeWorker({ workerPath: params.workerPath, source: params.workerSource }); - workerChanged = true; - await this.deps.configStore.update(function replaceJob(config) { - previousJob = config.cronJobs.find((existing) => existing.name === params.job.name); - return { - ...config, - cronJobs: [ - ...config.cronJobs.filter((existing) => existing.name !== params.job.name), - params.job, - ], - }; - }); - configChanged = true; - registrationAttempted = true; - await this.deps.registrar.register({ - path: params.workerPath, - schedule: params.job.schedule, - title: params.job.name, - }); - } catch (error) { - const rollbackErrors: unknown[] = []; - if (configChanged) { - await captureRollbackError({ - rollbackErrors, - rollback: () => this.restoreReplacedJob({ job: params.job, previousJob }), - }); - } - if (workerChanged) { - await captureRollbackError({ - rollbackErrors, - rollback: () => - this.restoreWorker({ - snapshot: params.snapshot, - workerPath: params.workerPath, - }), - }); - } - if (registrationAttempted) { - const registrationRollback = previousJob - ? () => - this.deps.registrar.register({ - path: params.workerPath, - schedule: previousJob!.schedule, - title: previousJob!.name, - }) - : () => this.deps.registrar.remove({ title: params.job.name }); - await captureRollbackError({ rollbackErrors, rollback: registrationRollback }); - } - this.throwAfterRollback({ error, rollbackErrors }); - } - } - - public async remove(params: { - snapshot: CronWorkerSnapshot; - workerPath: string; - name: string; - }): Promise { - let previousJob: RigCronJob | undefined; - let configChanged = false; - await this.deps.registrar.remove({ title: params.name }); - try { - await this.deps.configStore.update(function removeJob(config) { - previousJob = config.cronJobs.find((job) => job.name === params.name); - return { - ...config, - cronJobs: config.cronJobs.filter((job) => job.name !== params.name), - }; - }); - configChanged = true; - await this.deps.rm(params.workerPath, { force: true }); - } catch (error) { - const rollbackErrors: unknown[] = []; - if (configChanged && previousJob) { - await captureRollbackError({ - rollbackErrors, - rollback: () => this.restoreRemovedJob({ previousJob: previousJob! }), - }); - } - await captureRollbackError({ - rollbackErrors, - rollback: () => - this.restoreWorker({ - snapshot: params.snapshot, - workerPath: params.workerPath, - }), - }); - if (previousJob) { - await captureRollbackError({ - rollbackErrors, - rollback: () => - this.deps.registrar.register({ - path: params.workerPath, - schedule: previousJob!.schedule, - title: previousJob!.name, - }), - }); - } - this.throwAfterRollback({ error, rollbackErrors }); - } - return previousJob !== undefined; - } -} - -type CronPaths = Pick; -type CronServiceStore = Pick; -type CronTransaction = CronStateTransactionService; -type LegacyCronTransaction = Omit & { - restoreReplacedJob(job: RigCronJob, previousJob?: RigCronJob): Promise; - restoreRemovedJob(previousJob: RigCronJob): Promise; -}; - -type RigCronServiceDefinitionDeps = { - registrar: CronRegistrar; - createPaths: (params: RigCronServiceParams) => CronPaths; - createConfigStore: (params: RigCronServiceParams) => CronServiceStore; - readInput: CronInputReaderService["read"]; - renderWorker: CronWorkerScriptService["render"]; - createTransaction: (params: { - configStore: CronServiceStore; - registrar: CronRegistrar; - }) => CronTransaction; - runTool: (params: { - config: RigCronServiceParams; - tool: string; - command: string; - options: RigCronServiceParams & { input?: string; dryRun?: boolean }; - }) => Promise<{ envelope: unknown; exitCode: number }>; -}; - -const RigCronServiceProductionDeps: RigCronServiceDefinitionDeps = { - registrar: BunCronRegistrar, - createPaths(params) { - return new RigPathsClass(params); - }, - createConfigStore(params) { - return new RigConfigStoreClass(params); - }, - readInput(params) { - return CronInputReader.read(params); - }, - renderWorker(params) { - return CronWorkerScript.render(params); - }, - createTransaction(params) { - return new CronStateTransactionService({ - params: {}, - deps: { - ...CronStateTransactionProductionDeps, - configStore: params.configStore, - registrar: params.registrar, - }, - }); - }, - async runTool(params) { - return await new ToolRunnerClass(params.config).run( - params.tool, - params.command, - params.options, - ); - }, -}; - -export class RigCronService extends defineService({ - params: {} as RigCronServiceParams, - deps: RigCronServiceProductionDeps, -}) { - private readonly paths = this.deps.createPaths(this.params); - private readonly configStore = this.deps.createConfigStore(this.params); - private readonly transaction = this.deps.createTransaction({ - configStore: this.configStore, - registrar: this.deps.registrar, - }); - - public async list(_params: {}): Promise<{ cronJobs: RigCronJob[] }> { - const rigConfig = await this.configStore.ensure(); - return { - cronJobs: [...rigConfig.cronJobs].toSorted((left, right) => - left.name.localeCompare(right.name), - ), - }; - } - - private async validateCommand(params: { - target: { id: string; tool: string; command: string }; - input: unknown | undefined; - }): Promise { - const result = await this.deps.runTool({ - config: this.params, - tool: params.target.tool, - command: params.target.command, - options: { - ...this.params, - input: params.input === undefined ? undefined : JSON.stringify(params.input), - dryRun: true, - }, - }); - if (result.exitCode !== 0) { - throw new RigErrorClass("CRON_ERROR", `Cron command validation failed: ${params.target.id}`, { - envelope: result.envelope, - }); - } - } - - public async add(params: CronAddOptions): Promise<{ job: RigCronJob; workerPath: string }> { - const name = CronJobNameSingleton.parse({ value: params.name }); - const target = CronCommandTargetSingleton.parse({ id: params.command }); - const input = await this.deps.readInput(params); - - this.deps.registrar.validate({ schedule: params.schedule }); - await this.validateCommand({ target, input }); - - await this.configStore.ensure(); - const job: RigCronJob = { - name: name.value, - command: target.id, - schedule: params.schedule, - ...(input === undefined ? {} : { input }), - }; - const workerPath = this.paths.cronWorkerPath(name.value); - const workerSource = this.deps.renderWorker({ - name: name.value, - homeDir: this.params.homeDir, - moduleUrl: params.moduleUrl, - }); - const snapshot = await this.transaction.snapshot({ workerPath }); - await this.transaction.replace({ snapshot, workerPath, workerSource, job }); - - return { job, workerPath }; - } - - public async remove(params: { - name: string; - }): Promise<{ name: string; removed: boolean; workerPath: string }> { - const name = CronJobNameSingleton.parse({ value: params.name }); - await this.configStore.ensure(); - const workerPath = this.paths.cronWorkerPath(name.value); - const snapshot = await this.transaction.snapshot({ workerPath }); - const removed = await this.transaction.remove({ snapshot, workerPath, name: name.value }); - return { name: name.value, removed, workerPath }; - } - - public async run(params: { name: string }): Promise { - const name = CronJobNameSingleton.parse({ value: params.name }); - const rigConfig = await this.configStore.ensure(); - const job = rigConfig.cronJobs.find((candidate) => candidate.name === name.value); - if (!job) { - throw new RigErrorClass("CRON_ERROR", `Cron job not found: ${name.value}`, { name }); - } - - const target = CronCommandTargetSingleton.parse({ id: job.command }); - const result = await this.deps.runTool({ - config: this.params, - tool: target.tool, - command: target.command, - options: { - ...this.params, - input: job.input === undefined ? undefined : JSON.stringify(job.input), - }, - }); - return { job, envelope: result.envelope, exitCode: result.exitCode }; - } -} - -export type RigCronServiceClass = { - list(): Promise<{ cronJobs: RigCronJob[] }>; - add(params: CronAddOptions): Promise<{ job: RigCronJob; workerPath: string }>; - remove(params: { name: string }): Promise<{ name: string; removed: boolean; workerPath: string }>; - run(params: { name: string }): Promise; -}; - -type RigCronServiceConstructor = { - new (params: RigCronServiceParams, deps: RigCronServiceDeps): RigCronServiceClass; - readonly prototype: RigCronServiceClass; -}; - -type RigCronServiceAdapter = RigCronServiceClass & { - readonly resource: RigCronService; - readonly transaction: LegacyCronTransaction; -}; - -const RigCronServiceClassAdapter = function constructRigCronService( - this: RigCronServiceAdapter, - serviceParams: RigCronServiceParams, - deps: RigCronServiceDeps, -): void { - const registrar = deps.registrar ?? RigCronServiceProductionDeps.registrar; - const resource = new RigCronService({ - params: serviceParams, - deps: { ...RigCronServiceProductionDeps, registrar }, - }); - const transactionResource = RigCronServiceProductionDeps.createTransaction({ - configStore: RigCronServiceProductionDeps.createConfigStore(serviceParams), - registrar, - }); - const transaction: LegacyCronTransaction = { - snapshot(params) { - return transactionResource.snapshot(params); - }, - replace(params) { - return transactionResource.replace(params); - }, - remove(params) { - return transactionResource.remove(params); - }, - restoreReplacedJob(job, previousJob) { - return transactionResource.restoreReplacedJob({ job, previousJob }); - }, - restoreRemovedJob(previousJob) { - return transactionResource.restoreRemovedJob({ previousJob }); - }, - }; - Object.defineProperties(this, { - resource: { value: resource }, - transaction: { value: transaction }, - }); -}; -Object.defineProperty(RigCronServiceClassAdapter, "name", { value: "RigCronServiceClass" }); -Object.defineProperties(RigCronServiceClassAdapter.prototype, { - list: { - configurable: true, - value: function list(this: RigCronServiceAdapter) { - return this.resource.list({}); - }, - writable: true, - }, - add: { - configurable: true, - value: function add(this: RigCronServiceAdapter, params: CronAddOptions) { - return this.resource.add(params); - }, - writable: true, - }, - remove: { - configurable: true, - value: function remove(this: RigCronServiceAdapter, params: { name: string }) { - return this.resource.remove(params); - }, - writable: true, - }, - run: { - configurable: true, - value: function run(this: RigCronServiceAdapter, params: { name: string }) { - return this.resource.run(params); - }, - writable: true, - }, -}); - -export const RigCronServiceClass = - RigCronServiceClassAdapter as unknown as RigCronServiceConstructor; - -type RigCronWorkerServiceDeps = { - runCron: (params: { config: RigCronWorkerParams; name: string }) => Promise; - log: (message: string) => void; - setExitCode: (code: number) => void; -}; - -const RigCronWorkerProductionDeps: RigCronWorkerServiceDeps = { - async runCron(params) { - return await new RigCronServiceClass(params.config, {}).run({ name: params.name }); - }, - log(message) { - console.log(message); - }, - setExitCode(code) { - process.exitCode = code; - }, -}; - -export class RigCronWorkerService extends defineService({ - params: {} as RigCronWorkerParams, - deps: RigCronWorkerProductionDeps, -}) { - public async scheduled(params: { name: string; controller?: CronControllerLike }): Promise { - const result = await this.deps.runCron({ config: this.params, name: params.name }); - const envelope = result.envelope as Partial; - this.deps.log( - JSON.stringify( - { - cron: { - name: result.job.name, - command: result.job.command, - schedule: params.controller?.cron ?? result.job.schedule, - scheduledTime: params.controller?.scheduledTime, - type: params.controller?.type, - }, - result: envelope, - }, - null, - 2, - ), - ); - this.deps.setExitCode(result.exitCode); - } -} - -export type RigCronWorkerClass = { - scheduled(params: { name: string; controller?: CronControllerLike }): Promise; -}; - -type RigCronWorkerConstructor = { - new (params: RigCronWorkerParams, deps: RigCronWorkerDeps): RigCronWorkerClass; - readonly prototype: RigCronWorkerClass; -}; - -type RigCronWorkerAdapter = RigCronWorkerClass & { - readonly resource: RigCronWorkerService; -}; - -const RigCronWorkerClassAdapter = function constructRigCronWorker( - this: RigCronWorkerAdapter, - params: RigCronWorkerParams, - deps: RigCronWorkerDeps, -): void { - const service = deps.service; - Object.defineProperty(this, "resource", { - value: new RigCronWorkerService({ - params, - deps: service - ? { - ...RigCronWorkerProductionDeps, - runCron(runParams) { - return service.run({ name: runParams.name }); - }, - } - : RigCronWorkerProductionDeps, - }), - }); -}; -Object.defineProperty(RigCronWorkerClassAdapter, "name", { value: "RigCronWorkerClass" }); -Object.defineProperty(RigCronWorkerClassAdapter.prototype, "scheduled", { - configurable: true, - value: function scheduled( - this: RigCronWorkerAdapter, - params: { name: string; controller?: CronControllerLike }, - ) { - return this.resource.scheduled(params); - }, - writable: true, -}); - -export const RigCronWorkerClass = RigCronWorkerClassAdapter as unknown as RigCronWorkerConstructor; -export const RigCronWorker = RigCronWorkerClass; - -export function cronModuleUrl(metaUrl: string): string { - return pathToFileURL(fileURLToPath(metaUrl)).href; -} diff --git a/src/define.test.ts b/src/define.test.ts deleted file mode 100644 index a29dc87..0000000 --- a/src/define.test.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { describe, expect, it, vi } from "vite-plus/test"; -import { - defineApp, - defineConfig, - defineProvider, - defineRepo, - defineRuntime, - defineService, - defineSingleton, - defineType, - defineUIComponent, - defineUIHook, - defineUtil, -} from "./define.ts"; - -describe("definitions", () => { - it("defines type, config, and utility values without changing their shape", () => { - const parser = defineType({ parse: (value: string) => value.trim() }); - const config = defineConfig({ port: 3333 } as const); - const util = defineUtil({ double: (value: number) => value * 2 }); - - expect(parser.parse(" value ")).toBe("value"); - expect(config.port).toBe(3333); - expect(util.double(3)).toBe(6); - }); - - it("defines classes with defaults and replacement construction props", () => { - class Formatter extends defineService({ - params: { prefix: "production" }, - deps: { uppercase: (value: string) => value.toUpperCase() }, - }) { - public format(props: { value: string }): string { - return `${this.params.prefix}:${this.deps.uppercase(props.value)}`; - } - } - - expect(new Formatter().format({ value: "value" })).toBe("production:VALUE"); - expect( - new Formatter({ - params: { prefix: "test" }, - deps: { uppercase: (value) => value }, - }).format({ value: "value" }), - ).toBe("test:value"); - }); - - it("defines singletons whose methods use params and deps through this", () => { - const Singleton = defineSingleton({ - params: { prefix: "production" }, - deps: { uppercase: (value: string) => value.toUpperCase() }, - format(props: { value: string }) { - return `${this.params.prefix}:${this.deps.uppercase(props.value)}`; - }, - }); - - expect(Singleton.format({ value: "value" })).toBe("production:VALUE"); - }); - - it("defines hooks whose implementation uses params and deps through this", () => { - const useValue = defineUIHook({ - params: { prefix: "production" }, - deps: { uppercase: (value: string) => value.toUpperCase() }, - hook(props: { value: string }) { - return `${this.params.prefix}:${this.deps.uppercase(props.value)}`; - }, - }); - - expect(useValue({ value: "value" })).toBe("production:VALUE"); - }); - - it("defines components whose implementation receives props", () => { - const Message = defineUIComponent({ - params: { prefix: "production" }, - deps: { uppercase: (value: string) => value.toUpperCase() }, - component(props: { message: string }) { - return `${this.params.prefix}:${this.deps.uppercase(props.message)}`; - }, - }); - - expect(Message({ message: "value" })).toBe("production:VALUE"); - }); - - it("runs entrypoints with params and deps on this", async () => { - const execute = vi.fn(async function execute(value: string) { - return value.toUpperCase(); - }); - - const result = await defineApp({ - params: { value: "lgtm" }, - deps: { execute }, - async run() { - return await this.deps.execute(this.params.value); - }, - }); - - expect(result).toBe("LGTM"); - expect(execute).toHaveBeenCalledWith("lgtm"); - }); - - it("defines constructable repository, runtime, and provider layers", () => { - class Repository extends defineRepo({ params: { value: "repo" }, deps: {} }) { - public read(): string { - return this.params.value; - } - } - class Runtime extends defineRuntime({ params: { value: "runtime" }, deps: {} }) { - public run(): string { - return this.params.value; - } - } - class Provider extends defineProvider({ params: { value: "provider" }, deps: {} }) { - public provide(): string { - return this.params.value; - } - } - - expect(new Repository().read()).toBe("repo"); - expect(new Runtime().run()).toBe("runtime"); - expect(new Provider().provide()).toBe("provider"); - }); -}); diff --git a/src/define.ts b/src/define.ts deleted file mode 100644 index a4c8e17..0000000 --- a/src/define.ts +++ /dev/null @@ -1,123 +0,0 @@ -import type { ReactNode } from "react"; - -export type DefinitionProps = Readonly<{ - params: Readonly; - deps: Readonly; -}>; - -type DefinitionContext = Readonly<{ - params: Readonly; - deps: Readonly; -}>; - -type PublicDependency = Value extends (...params: never[]) => unknown - ? Value - : Value extends object - ? { readonly [Key in keyof Value]: Value[Key] } - : Value; - -type PublicDependencies = { - readonly [Key in keyof Deps]: PublicDependency; -}; - -function defineValue(definition: Definition): Definition { - return definition; -} - -export function defineType(definition: Definition): Definition { - return defineValue(definition); -} - -export function defineConfig(definition: Definition): Definition { - return defineValue(definition); -} - -export function defineUtil(definition: Definition): Definition { - return defineValue(definition); -} - -function defineConstructable( - defaults: DefinitionProps, -) { - type Props = DefinitionProps>; - const publicDefaults = defaults as Props; - - return class DefinedClass { - protected readonly params: Readonly; - protected readonly deps: Readonly>; - - public constructor(props: Props = publicDefaults) { - this.params = props.params; - this.deps = props.deps; - } - }; -} - -export function defineRepo( - defaults: DefinitionProps, -) { - return defineConstructable(defaults); -} - -export function defineService( - defaults: DefinitionProps, -) { - return defineConstructable(defaults); -} - -export function defineRuntime( - defaults: DefinitionProps, -) { - return defineConstructable(defaults); -} - -export function defineProvider( - defaults: DefinitionProps, -) { - return defineConstructable(defaults); -} - -export function defineSingleton< - Params extends object, - Deps extends object, - Definition extends object, ->( - definition: Definition & - DefinitionProps & - ThisType>, -): Omit { - return definition; -} - -export function defineUIHook( - definition: DefinitionProps & { - hook(props: Props): Output; - } & ThisType>, -): (props: Props) => Output { - return function definedHook(props: Props): Output { - return definition.hook.call(definition, props); - }; -} - -export function defineUIComponent< - Params extends object, - Deps extends object, - Props extends object, - Output extends ReactNode, ->( - definition: DefinitionProps & { - component(props: Props): Output; - } & ThisType>, -): (props: Props) => Output { - return function DefinedComponent(props: Props): Output { - return definition.component.call(definition, props); - }; -} - -export function defineApp( - definition: DefinitionProps & { - run(): Output; - } & ThisType>, -): Output { - return definition.run.call(definition); -} diff --git a/src/dev/dev-link.test.ts b/src/dev/dev-link.test.ts deleted file mode 100644 index 25d0572..0000000 --- a/src/dev/dev-link.test.ts +++ /dev/null @@ -1,164 +0,0 @@ -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { existsSync } from "node:fs"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { DevLink, DevLinkService, DevLinkServiceClass } from "./dev-link"; - -class TestWorkspaceStore { - private readonly paths: string[] = []; - - async create(): Promise { - const path = await mkdtemp(join(tmpdir(), "rig-test-workspace-")); - this.paths.push(path); - return path; - } - - async cleanup(): Promise { - await Promise.all( - this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), - ); - } -} - -class TestRigRepo { - constructor(readonly path: string) {} - - async create(): Promise { - await mkdir(join(this.path, "src"), { recursive: true }); - await writeFile(join(this.path, "package.json"), '{"name":"rig"}\n', "utf8"); - await writeFile(join(this.path, "src", "cli.ts"), "console.log('rig dev');\n", "utf8"); - } -} - -const workspaces = new TestWorkspaceStore(); - -afterEach(async () => { - await workspaces.cleanup(); -}); - -describe("dev link", () => { - test("links and unlinks a rig dev shim", async () => { - const home = await workspaces.create(); - const repoPath = await workspaces.create(); - await new TestRigRepo(repoPath).create(); - - const service = new DevLinkServiceClass({ homeDir: home, repoRoot: repoPath }); - const linked = await service.link(); - - expect(linked.exists).toBe(true); - expect(linked.isRigDevShim).toBe(true); - expect(linked.pointsToCurrentRepo).toBe(true); - expect(existsSync(linked.shimPath)).toBe(true); - const shim = await readFile(linked.shimPath, "utf8"); - expect(shim).toContain("Rig dev shim"); - expect(shim).toContain("bun --install=fallback run"); - - const unlinked = await service.unlink(); - expect(unlinked.exists).toBe(false); - expect(existsSync(linked.shimPath)).toBe(false); - }); - - test("refuses to overwrite a non-Rig shim without force", async () => { - const home = await workspaces.create(); - const repoPath = await workspaces.create(); - await new TestRigRepo(repoPath).create(); - const binDir = join(home, ".local", "bin"); - await mkdir(binDir, { recursive: true }); - await writeFile(join(binDir, "rig"), "#!/usr/bin/env bash\necho nope\n", "utf8"); - - await expect( - new DevLinkServiceClass({ homeDir: home, repoRoot: repoPath }).link(), - ).rejects.toThrow("Refusing to overwrite existing file"); - }); - - test("builds replaceable resources and renders link results", () => { - expect(DevLink.create({})).toBeDefined(); - - const Service = new DevLinkService({ - params: { - marker: "Rig dev shim", - unixShimName: "rig", - windowsShimName: "rig.cmd", - defaultBinDirectory: [".local", "bin"], - }, - deps: { - createPaths() { - return { - homeDir() { - return "/home/rig"; - }, - resolve(params) { - return params.pathValue; - }, - }; - }, - exists() { - return true; - }, - async chmod() {}, - async lstat() { - return { isFile: () => true }; - }, - async mkdir() {}, - async readTextFile() { - return ""; - }, - async rm() {}, - async writeTextFile() {}, - cwd() { - return "/repo"; - }, - pathEnvironment() { - return "/bin"; - }, - platform() { - return "linux"; - }, - delimiter: ":", - join(...paths) { - return paths.join("/"); - }, - resolve(...paths) { - return paths.at(-1) ?? ""; - }, - }, - }); - const service = Service.create({ options: { repoRoot: "/repo" } }); - const status = { - repoRoot: "/repo", - binDir: "/home/rig/.local/bin", - shimPath: "/home/rig/.local/bin/rig", - exists: true, - isRigDevShim: true, - pointsToCurrentRepo: true, - binDirOnPath: false, - }; - - expect(service.renderLinkResult({ status })).toContain("Add /home/rig/.local/bin to PATH"); - expect(service.renderLinkResult({ status: { ...status, binDirOnPath: true } })).not.toContain( - "Add /home/rig/.local/bin to PATH", - ); - expect(service.renderUnlinkResult({ status })).toContain("Rig dev link removed."); - }); - - test("writes a Windows command shim when requested", async () => { - const home = await workspaces.create(); - const repoPath = await workspaces.create(); - await new TestRigRepo(repoPath).create(); - - const service = new DevLinkServiceClass({ - homeDir: home, - repoRoot: repoPath, - platform: "win32", - }); - const linked = await service.link(); - const shim = await readFile(linked.shimPath, "utf8"); - - expect(linked.shimPath).toBe(join(home, ".local", "bin", "rig.cmd")); - expect(linked.pointsToCurrentRepo).toBe(true); - expect(shim).toContain("@echo off"); - expect(shim).toContain('set "RIG_DEV_REPO='); - expect(shim).toContain('run "%RIG_DEV_REPO%\\src\\bin.ts" %*'); - }); -}); diff --git a/src/dev/dev-link.ts b/src/dev/dev-link.ts deleted file mode 100644 index a2e4cbd..0000000 --- a/src/dev/dev-link.ts +++ /dev/null @@ -1,335 +0,0 @@ -import { existsSync } from "node:fs"; -import { chmod, lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises"; -import { homedir } from "node:os"; -import { delimiter, dirname, isAbsolute, join, resolve } from "node:path"; -import { defineService } from "../define"; -import { RigPathsRepo, type PathOptions } from "../config/paths"; -import { RigErrorClass } from "../errors/RigError"; - -export type DevLinkOptions = PathOptions & { - repoRoot?: string; - platform?: NodeJS.Platform; -}; - -export type DevLinkCommandOptions = { - binDir?: string; - force?: boolean; -}; - -export type DevLinkStatus = { - repoRoot: string; - binDir: string; - shimPath: string; - exists: boolean; - isRigDevShim: boolean; - pointsToCurrentRepo: boolean; - binDirOnPath: boolean; -}; - -type DevLinkPaths = Pick; - -type ExistingShim = { exists: boolean; isRigDevShim: boolean; content: string }; - -function renderDevLinkResult(params: { status: DevLinkStatus }): string { - const lines = [ - "Rig dev link is ready.", - "", - `Shim: ${params.status.shimPath}`, - `Repo: ${params.status.repoRoot}`, - `On PATH: ${params.status.binDirOnPath ? "yes" : "no"}`, - "", - "Try:", - " rig", - " rig list", - ]; - - if (!params.status.binDirOnPath) { - lines.push("", `Add ${params.status.binDir} to PATH to run rig from any shell.`); - } - return lines.join("\n"); -} - -function renderDevUnlinkResult(params: { status: DevLinkStatus }): string { - return ["Rig dev link removed.", "", `Shim: ${params.status.shimPath}`].join("\n"); -} - -/* v8 ignore next 4 -- Bun path is covered by distribution integration */ -async function readTextFile(params: { path: string }): Promise { - if (typeof Bun !== "undefined") return await Bun.file(params.path).text(); - return await readFile(params.path, "utf8"); -} - -/* v8 ignore next 4 -- Bun path is covered by distribution integration */ -async function writeTextFile(params: { path: string; content: string }): Promise { - if (typeof Bun !== "undefined") await Bun.write(params.path, params.content); - else await writeFile(params.path, params.content, "utf8"); -} - -const DevLinkServiceDeps: { - createPaths: (params: { homeDir?: string }) => DevLinkPaths; - exists: (path: string) => boolean; - chmod: (path: string, mode: number) => Promise; - lstat: (path: string) => Promise<{ isFile(): boolean }>; - mkdir: (path: string, options: { recursive: true }) => Promise; - readTextFile: (params: { path: string }) => Promise; - rm: (path: string, options: { force: true }) => Promise; - writeTextFile: (params: { path: string; content: string }) => Promise; - cwd: () => string; - pathEnvironment: () => string; - platform: () => NodeJS.Platform; - delimiter: string; - join: (...paths: string[]) => string; - resolve: (...paths: string[]) => string; -} = { - createPaths: function createPaths(params: { homeDir?: string }) { - return new RigPathsRepo({ - params: { - homeDir: params.homeDir, - defaultBaseRegistryDirValue: "~/rig/tools", - legacyDefaultBaseRegistryDirValue: "~/.rig/tools", - }, - deps: { - homedir, - cwd: process.cwd.bind(process), - dirname, - isAbsolute, - join, - resolve, - }, - }); - }, - exists: existsSync, - chmod, - lstat, - mkdir, - readTextFile, - rm, - writeTextFile, - cwd: process.cwd.bind(process), - pathEnvironment: function pathEnvironment() { - return process.env.PATH ?? ""; - }, - platform: function platform() { - return process.platform; - }, - delimiter, - join, - resolve, -}; - -export class DevLinkService extends defineService({ - params: { - marker: "Rig dev shim", - unixShimName: "rig", - windowsShimName: "rig.cmd", - defaultBinDirectory: [".local", "bin"] as const, - }, - deps: DevLinkServiceDeps, -}) { - public create(params: { options?: DevLinkOptions }) { - const config = this.params; - const deps = this.deps; - const options = params.options ?? {}; - const Paths = deps.createPaths({ homeDir: options.homeDir }); - const repoRootOverride = options.repoRoot; - const platform = options.platform ?? deps.platform(); - - function repoRoot(): string { - const path = deps.resolve(repoRootOverride ?? deps.cwd()); - if ( - !deps.exists(deps.join(path, "package.json")) || - !deps.exists(deps.join(path, "src", "cli.ts")) - ) { - throw new RigErrorClass("DEV_LINK_ERROR", "Run dev link from the Rig repository root.", { - repoRoot: path, - expected: ["package.json", "src/cli.ts"], - }); - } - return path; - } - - function binDirectory(pathValue?: string): string { - return pathValue - ? Paths.resolve({ pathValue }) - : deps.join(Paths.homeDir({}), ...config.defaultBinDirectory); - } - - function shimPath(path: string): string { - return deps.join(path, platform === "win32" ? config.windowsShimName : config.unixShimName); - } - - function repoMarker(path: string): string { - return platform === "win32" ? `RIG_DEV_REPO=${path}` : `RIG_DEV_REPO=${JSON.stringify(path)}`; - } - - function shimSource(path: string): string { - if (platform === "win32") { - return `@echo off -rem Rig dev shim. Safe to overwrite with \`rig dev link\`. -set "RIG_DEV_REPO=${path}" -bun --install=fallback run "%RIG_DEV_REPO%\\src\\bin.ts" %* -`; - } - return `#!/bin/sh -# Rig dev shim. Safe to overwrite with \`rig dev link\`. -RIG_DEV_REPO=${JSON.stringify(path)} -exec bun --install=fallback run "$RIG_DEV_REPO/src/bin.ts" "$@" -`; - } - - async function readExistingShim(path: string): Promise { - if (!deps.exists(path)) return { exists: false, isRigDevShim: false, content: "" }; - const shimStatus = await deps.lstat(path); - if (!shimStatus.isFile()) return { exists: true, isRigDevShim: false, content: "" }; - const content = await deps.readTextFile({ path }); - return { - exists: true, - isRigDevShim: content.includes(config.marker), - content, - }; - } - - function binDirOnPath(path: string): boolean { - const entries = deps - .pathEnvironment() - .split(deps.delimiter) - .map((entry) => deps.resolve(entry)); - return entries.includes(deps.resolve(path)); - } - - async function status(statusParams: { - options?: DevLinkCommandOptions; - }): Promise { - const commandOptions = statusParams.options ?? {}; - const root = repoRoot(); - const binDir = binDirectory(commandOptions.binDir); - const path = shimPath(binDir); - const existing = await readExistingShim(path); - return { - repoRoot: root, - binDir, - shimPath: path, - exists: existing.exists, - isRigDevShim: existing.isRigDevShim, - pointsToCurrentRepo: existing.content.includes(repoMarker(root)), - binDirOnPath: binDirOnPath(binDir), - }; - } - - async function link(linkParams: { options?: DevLinkCommandOptions }): Promise { - const commandOptions = linkParams.options ?? {}; - const root = repoRoot(); - const binDir = binDirectory(commandOptions.binDir); - const path = shimPath(binDir); - const existing = await readExistingShim(path); - - if (existing.exists && !existing.isRigDevShim && !commandOptions.force) { - throw new RigErrorClass("DEV_LINK_ERROR", `Refusing to overwrite existing file: ${path}`, { - shimPath: path, - hint: "Use --force to replace it.", - }); - } - - await deps.mkdir(binDir, { recursive: true }); - await deps.writeTextFile({ path, content: shimSource(root) }); - if (platform !== "win32") await deps.chmod(path, 0o755); - return await status({ options: commandOptions }); - } - - async function unlink(unlinkParams: { - options?: DevLinkCommandOptions; - }): Promise { - const commandOptions = unlinkParams.options ?? {}; - const binDir = binDirectory(commandOptions.binDir); - const path = shimPath(binDir); - const existing = await readExistingShim(path); - - if (existing.exists && !existing.isRigDevShim && !commandOptions.force) { - throw new RigErrorClass("DEV_LINK_ERROR", `Refusing to remove non-Rig dev shim: ${path}`, { - shimPath: path, - hint: "Use --force to remove it anyway.", - }); - } - - if (existing.exists) await deps.rm(path, { force: true }); - return await status({ options: commandOptions }); - } - - return { - link, - unlink, - status, - renderLinkResult: renderDevLinkResult, - renderUnlinkResult: renderDevUnlinkResult, - }; - } -} - -export const DevLink = new DevLinkService(); - -export type DevLinkServiceClass = { - link(options?: DevLinkCommandOptions): Promise; - unlink(options?: DevLinkCommandOptions): Promise; - status(options?: DevLinkCommandOptions): Promise; - renderLinkResult(status: DevLinkStatus): string; - renderUnlinkResult(status: DevLinkStatus): string; -}; - -type DevLinkServiceConstructor = { - new (options?: DevLinkOptions): DevLinkServiceClass; - readonly prototype: DevLinkServiceClass; -}; - -type DevLinkResource = ReturnType; - -type DevLinkAdapter = DevLinkServiceClass & { readonly resource: DevLinkResource }; - -const DevLinkServiceClassAdapter = function constructDevLinkService( - this: DevLinkAdapter, - options: DevLinkOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: DevLink.create({ options }), - }); -}; -Object.defineProperty(DevLinkServiceClassAdapter, "name", { value: "DevLinkServiceClass" }); -Object.defineProperties(DevLinkServiceClassAdapter.prototype, { - link: { - configurable: true, - value: function link(this: DevLinkAdapter, options?: DevLinkCommandOptions) { - return this.resource.link({ options }); - }, - writable: true, - }, - unlink: { - configurable: true, - value: function unlink(this: DevLinkAdapter, options?: DevLinkCommandOptions) { - return this.resource.unlink({ options }); - }, - writable: true, - }, - status: { - configurable: true, - value: function status(this: DevLinkAdapter, options?: DevLinkCommandOptions) { - return this.resource.status({ options }); - }, - writable: true, - }, - renderLinkResult: { - configurable: true, - value: function renderLinkResult(this: DevLinkAdapter, status: DevLinkStatus) { - return this.resource.renderLinkResult({ status }); - }, - writable: true, - }, - renderUnlinkResult: { - configurable: true, - value: function renderUnlinkResult(this: DevLinkAdapter, status: DevLinkStatus) { - return this.resource.renderUnlinkResult({ status }); - }, - writable: true, - }, -}); - -export const DevLinkServiceClass = - DevLinkServiceClassAdapter as unknown as DevLinkServiceConstructor; diff --git a/src/domains/collections/repo/collection-document-path.ts b/src/domains/collections/repo/collection-document-path.ts new file mode 100644 index 0000000..090fd43 --- /dev/null +++ b/src/domains/collections/repo/collection-document-path.ts @@ -0,0 +1,56 @@ +import { statSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { CollectionFileMetadata } from "../types/tool-collection"; + +export class CollectionDocumentPathService extends Context.Service< + CollectionDocumentPathService, + { + readonly filePath: (collectionPath: string, id: string) => Effect.Effect; + readonly metadata: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionDocumentPathService", { + make: Effect.gen(function* () { + const normalizeError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }); + const filePath = Effect.fn("CollectionDocumentPathService.filePath")(function* ( + collectionPath: string, + id: string, + ) { + const isInvalidEntryId = + !id || id === "." || id === ".." || id.includes("/") || id.includes("\\"); + if (isInvalidEntryId) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: `Invalid collection entry id: ${id}`, + }); + } + const root = resolve(collectionPath); + const path = resolve(root, `${id}.md`); + if (dirname(path) !== root) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: `Entry escapes collection: ${id}`, + }); + } + return path; + }); + const metadata = Effect.fn("CollectionDocumentPathService.metadata")(function* (path: string) { + const status = yield* Effect.try({ try: () => statSync(path), catch: normalizeError }); + return { mtimeMs: status.mtimeMs, ctimeMs: status.ctimeMs, size: status.size }; + }); + return { filePath, metadata } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionDocumentPathService, + CollectionDocumentPathService.make, + ); +} diff --git a/src/domains/collections/repo/collection-document-repository.ts b/src/domains/collections/repo/collection-document-repository.ts new file mode 100644 index 0000000..7db4557 --- /dev/null +++ b/src/domains/collections/repo/collection-document-repository.ts @@ -0,0 +1,221 @@ +import { existsSync } from "node:fs"; +import { readFile, unlink, writeFile } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { + CollectionData, + CollectionOperationContext, +} from "../types/collection-operation-context"; +import type { CollectionEntry } from "../types/tool-collection"; +import { CollectionDocumentPathService } from "./collection-document-path"; +import { CollectionEntryCodecService } from "./collection-entry-codec"; +import { FrontmatterCodecService } from "./frontmatter-codec"; + +export class CollectionDocumentIoService extends Context.Service< + CollectionDocumentIoService, + { + readonly write: ( + context: CollectionOperationContext, + document: CollectionEntry, + ) => Effect.Effect; + readonly getEntry: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect | null, RigError>; + readonly remove: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect; + readonly clear: (context: CollectionOperationContext) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionDocumentIoService", { + make: Effect.gen(function* () { + const entries = yield* CollectionEntryCodecService; + const paths = yield* CollectionDocumentPathService; + const frontmatter = yield* FrontmatterCodecService; + const normalizeError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }); + const write = Effect.fn("CollectionDocumentIoService.write")(function* ( + context: CollectionOperationContext, + document: CollectionEntry, + ) { + const path = yield* paths.filePath(context.path, document.id); + const source = yield* frontmatter.serialize(document.data, document.body); + yield* Effect.tryPromise({ + try: () => writeFile(path, source, "utf8"), + catch: normalizeError, + }); + const metadata = yield* paths.metadata(path); + context.index.upsertDoc(document, metadata.mtimeMs); + context.index.upsertFile({ id: document.id, status: "indexed", ...metadata }); + }); + const getEntry = Effect.fn("CollectionDocumentIoService.getEntry")(function* ( + context: CollectionOperationContext, + id: string, + ) { + const path = yield* paths.filePath(context.path, id); + if (!existsSync(path)) return null; + const source = yield* Effect.tryPromise({ + try: () => readFile(path, "utf8"), + catch: normalizeError, + }); + const parsed = yield* frontmatter.parse(source); + const metadata = yield* paths.metadata(path); + const row = context.index.getDoc(id); + return { + id, + data: yield* entries.validate(context.definition, parsed.data), + body: parsed.body, + createdAt: row?.created_at ?? new Date(metadata.ctimeMs).toISOString(), + updatedAt: row?.updated_at ?? new Date(metadata.mtimeMs).toISOString(), + }; + }); + const remove = Effect.fn("CollectionDocumentIoService.remove")(function* ( + context: CollectionOperationContext, + id: string, + ) { + const path = yield* paths.filePath(context.path, id); + if (!existsSync(path)) return false; + yield* Effect.tryPromise({ try: () => unlink(path), catch: normalizeError }); + context.index.deleteDoc(id); + context.index.deleteFile(id); + return true; + }); + const clear = Effect.fn("CollectionDocumentIoService.clear")(function* ( + context: CollectionOperationContext, + ) { + const ids = [...new Set([...context.index.allIds(), ...context.index.allFileIds()])]; + yield* Effect.forEach(ids, (id) => + paths + .filePath(context.path, id) + .pipe( + Effect.flatMap((path) => + existsSync(path) + ? Effect.tryPromise({ try: () => unlink(path), catch: normalizeError }) + : Effect.void, + ), + ), + ); + context.index.clearAll(); + }); + return { write, getEntry, remove, clear } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionDocumentIoService, + CollectionDocumentIoService.make, + ); +} + +export class CollectionDocumentRepositoryService extends Context.Service< + CollectionDocumentRepositoryService, + { + readonly create: ( + context: CollectionOperationContext, + input: { id?: string; data: CollectionData; body?: string }, + ) => Effect.Effect, RigError>; + readonly getEntry: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect | null, RigError>; + readonly update: ( + context: CollectionOperationContext, + id: string, + patch: { data?: CollectionData; body?: string }, + ) => Effect.Effect, RigError>; + readonly upsert: ( + context: CollectionOperationContext, + input: { id: string; data: CollectionData; body?: string }, + ) => Effect.Effect<{ id: string; created: boolean }, RigError>; + readonly remove: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect; + readonly clear: (context: CollectionOperationContext) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionDocumentRepositoryService", { + make: Effect.gen(function* () { + const documents = yield* CollectionDocumentIoService; + const entries = yield* CollectionEntryCodecService; + const paths = yield* CollectionDocumentPathService; + const create = Effect.fn("CollectionDocumentRepositoryService.create")(function* ( + context: CollectionOperationContext, + input: { id?: string; data: CollectionData; body?: string }, + ) { + const data = yield* entries.validate(context.definition, input.data); + const id = input.id ?? (yield* entries.deriveId(context.definition, data)); + if (id === undefined) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: "Collection entry needs an id.", + }); + } + if (existsSync(yield* paths.filePath(context.path, id))) { + return yield* new RigError({ code: "INPUT_ERROR", message: `Entry already exists: ${id}` }); + } + const now = new Date().toISOString(); + const document = { id, data, body: input.body ?? "", createdAt: now, updatedAt: now }; + yield* documents.write(context, document); + return document; + }); + const getEntry = Effect.fn("CollectionDocumentRepositoryService.getEntry")(function* ( + context: CollectionOperationContext, + id: string, + ) { + return yield* documents.getEntry(context, id); + }); + const update = Effect.fn("CollectionDocumentRepositoryService.update")(function* ( + context: CollectionOperationContext, + id: string, + patch: { data?: CollectionData; body?: string }, + ) { + const existing = yield* documents.getEntry(context, id); + if (!existing) { + return yield* new RigError({ code: "INPUT_ERROR", message: `Entry not found: ${id}` }); + } + const document = { + id, + data: patch.data + ? yield* entries.validate(context.definition, { ...existing.data, ...patch.data }) + : existing.data, + body: patch.body ?? existing.body, + createdAt: existing.createdAt, + updatedAt: new Date().toISOString(), + }; + yield* documents.write(context, document); + return document; + }); + const upsert = Effect.fn("CollectionDocumentRepositoryService.upsert")(function* ( + context: CollectionOperationContext, + input: { id: string; data: CollectionData; body?: string }, + ) { + const exists = existsSync(yield* paths.filePath(context.path, input.id)); + if (exists) yield* update(context, input.id, { data: input.data, body: input.body }); + else yield* create(context, input); + return { id: input.id, created: !exists }; + }); + const remove = Effect.fn("CollectionDocumentRepositoryService.remove")(function* ( + context: CollectionOperationContext, + id: string, + ) { + return yield* documents.remove(context, id); + }); + const clear = Effect.fn("CollectionDocumentRepositoryService.clear")(function* ( + context: CollectionOperationContext, + ) { + yield* documents.clear(context); + }); + return { create, getEntry, update, upsert, remove, clear } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionDocumentRepositoryService, + CollectionDocumentRepositoryService.make, + ); +} diff --git a/src/domains/collections/repo/collection-entry-codec.ts b/src/domains/collections/repo/collection-entry-codec.ts new file mode 100644 index 0000000..9d2f394 --- /dev/null +++ b/src/domains/collections/repo/collection-entry-codec.ts @@ -0,0 +1,76 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { CollectionData } from "../types/collection-operation-context"; +import type { CollectionDefinition, CollectionEntry, DocRow } from "../types/tool-collection"; + +export class CollectionEntryCodecService extends Context.Service< + CollectionEntryCodecService, + { + readonly validate: ( + definition: CollectionDefinition, + data: unknown, + ) => Effect.Effect; + readonly deriveId: ( + definition: CollectionDefinition, + data: CollectionData, + ) => Effect.Effect; + readonly rowToEntry: (row: DocRow) => Effect.Effect>; + readonly snippet: (body: string, query: string) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionEntryCodecService", { + make: Effect.gen(function* () { + const validate = Effect.fn("CollectionEntryCodecService.validate")(function* ( + definition: CollectionDefinition, + data: unknown, + ) { + if (!definition.schema) return data as CollectionData; + const result = definition.schema.safeParse(data); + if (result.success) return result.data as CollectionData; + return yield* new RigError({ + code: "VALIDATION_ERROR", + message: "Collection entry data is invalid.", + details: { errors: result.error.flatten() }, + }); + }); + const deriveId = Effect.fn("CollectionEntryCodecService.deriveId")(function* ( + definition: CollectionDefinition, + data: CollectionData, + ) { + if (definition.generateId) return definition.generateId(data); + const candidate = data.id ?? data.slug ?? data.title; + return typeof candidate === "string" + ? candidate + .toLowerCase() + .replace(/[^a-z0-9]+/g, "-") + .replace(/^-|-$/g, "") + : undefined; + }); + const rowToEntry = Effect.fn("CollectionEntryCodecService.rowToEntry")(function* (row: DocRow) { + return { + id: row.id, + data: JSON.parse(row.data_json) as CollectionData, + body: row.body, + createdAt: row.created_at, + updatedAt: row.updated_at, + }; + }); + const snippet = Effect.fn("CollectionEntryCodecService.snippet")(function* ( + body: string, + query: string, + ) { + const words = query.toLowerCase().split(/\s+/); + return ( + body + .split("\n") + .find((line) => words.some((word) => line.toLowerCase().includes(word))) + ?.slice(0, 200) ?? body.slice(0, 150) + ); + }); + return { validate, deriveId, rowToEntry, snippet } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionEntryCodecService, + CollectionEntryCodecService.make, + ); +} diff --git a/src/domains/collections/repo/collection-query.ts b/src/domains/collections/repo/collection-query.ts new file mode 100644 index 0000000..83470a1 --- /dev/null +++ b/src/domains/collections/repo/collection-query.ts @@ -0,0 +1,80 @@ +import { Context, Effect, Layer } from "effect"; +import type { + CollectionData, + CollectionOperationContext, +} from "../types/collection-operation-context"; +import type { CollectionEntry, DocRow, ListOptions, SearchResult } from "../types/tool-collection"; +import { CollectionEntryCodecService } from "./collection-entry-codec"; + +export class CollectionQueryService extends Context.Service< + CollectionQueryService, + { + readonly list: ( + context: CollectionOperationContext, + options?: ListOptions, + ) => Effect.Effect<{ entries: CollectionEntry[]; total: number }>; + readonly search: ( + context: CollectionOperationContext, + query: string, + options?: { limit?: number }, + ) => Effect.Effect<{ entries: SearchResult[] }>; + readonly count: ( + context: CollectionOperationContext, + where?: Record, + ) => Effect.Effect; + readonly getCollection: ( + context: CollectionOperationContext, + filter?: (entry: CollectionEntry) => boolean, + ) => Effect.Effect[]>; + } +>()("@rendotdev/rig/collections/CollectionQueryService", { + make: Effect.gen(function* () { + const entries = yield* CollectionEntryCodecService; + const list = Effect.fn("CollectionQueryService.list")(function* ( + context: CollectionOperationContext, + options: ListOptions = {}, + ) { + const result = context.index.listDocs(options); + return { + entries: yield* Effect.forEach(result.rows, entries.rowToEntry), + total: result.total, + }; + }); + const search = Effect.fn("CollectionQueryService.search")(function* ( + context: CollectionOperationContext, + query: string, + options?: { limit?: number }, + ) { + const rows = context.index.searchDocs(query, options?.limit ?? 10) as Array< + DocRow & { rank: number } + >; + return { + entries: yield* Effect.forEach(rows, (row) => + Effect.gen(function* () { + return { + ...(yield* entries.rowToEntry(row)), + snippet: yield* entries.snippet(row.body, query), + rank: row.rank, + }; + }), + ), + }; + }); + const count = Effect.fn("CollectionQueryService.count")(function* ( + context: CollectionOperationContext, + where?: Record, + ) { + return context.index.countDocs(where); + }); + const getCollection = Effect.fn("CollectionQueryService.getCollection")(function* ( + context: CollectionOperationContext, + filter?: (entry: CollectionEntry) => boolean, + ) { + const result = yield* list(context, { limit: 100_000 }); + return filter ? result.entries.filter((entry) => filter(entry)) : result.entries; + }); + return { list, search, count, getCollection } as const; + }), +}) { + static readonly layer = Layer.effect(CollectionQueryService, CollectionQueryService.make); +} diff --git a/src/domains/collections/repo/collection-reconciliation.ts b/src/domains/collections/repo/collection-reconciliation.ts new file mode 100644 index 0000000..c532365 --- /dev/null +++ b/src/domains/collections/repo/collection-reconciliation.ts @@ -0,0 +1,101 @@ +import { readdirSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { CollectionOperationContext } from "../types/collection-operation-context"; +import type { CollectionFileMetadata, CollectionIndexInterface } from "../types/tool-collection"; +import { CollectionDocumentPathService } from "./collection-document-path"; +import { CollectionEntryCodecService } from "./collection-entry-codec"; +import { FrontmatterCodecService } from "./frontmatter-codec"; + +export class CollectionReconciliationService extends Context.Service< + CollectionReconciliationService, + { + readonly reconcile: (context: CollectionOperationContext) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionReconciliationService", { + make: Effect.gen(function* () { + const entries = yield* CollectionEntryCodecService; + const paths = yield* CollectionDocumentPathService; + const frontmatter = yield* FrontmatterCodecService; + const normalizeError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }); + const matches = ( + tracked: ReturnType, + actual: CollectionFileMetadata, + ) => + tracked !== null && + tracked.mtimeMs === actual.mtimeMs && + tracked.ctimeMs === actual.ctimeMs && + tracked.size === actual.size; + const reconcileFile = Effect.fn("CollectionReconciliationService.reconcileFile")(function* ( + context: CollectionOperationContext, + file: string, + fileIds: Set, + ) { + const id = file.slice(0, -3); + fileIds.add(id); + const path = join(context.path, file); + const metadata = yield* paths.metadata(path); + if (matches(context.index.getFile(id), metadata)) return; + yield* Effect.gen(function* () { + const source = yield* Effect.tryPromise({ + try: () => readFile(path, "utf8"), + catch: normalizeError, + }); + const parsed = yield* frontmatter.parse(source); + const stable = yield* paths.metadata(path); + const indexed = context.index.getDoc(id); + context.index.upsertDoc( + { + id, + data: yield* entries.validate(context.definition, parsed.data), + body: parsed.body, + createdAt: indexed?.created_at ?? new Date(stable.ctimeMs).toISOString(), + updatedAt: new Date(stable.mtimeMs).toISOString(), + }, + stable.mtimeMs, + ); + context.index.upsertFile({ id, status: "indexed", ...stable }); + }).pipe( + Effect.catch(() => + Effect.sync(() => { + context.index.deleteDoc(id); + context.index.upsertFile({ id, status: "invalid", ...metadata }); + }), + ), + ); + }); + const reconcile = Effect.fn("CollectionReconciliationService.reconcile")(function* ( + context: CollectionOperationContext, + ) { + const files = yield* Effect.try({ + try: () => readdirSync(context.path).filter((file) => file.endsWith(".md")), + catch: normalizeError, + }); + const knownIds = new Set([...context.index.allIds(), ...context.index.allFileIds()]); + const fileIds = new Set(); + yield* Effect.forEach(files, (file) => reconcileFile(context, file, fileIds), { + concurrency: "unbounded", + }); + for (const id of knownIds) { + if (fileIds.has(id)) continue; + context.index.deleteDoc(id); + context.index.deleteFile(id); + } + }); + return { reconcile } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionReconciliationService, + CollectionReconciliationService.make, + ); +} diff --git a/src/domains/collections/repo/frontmatter-codec.test.ts b/src/domains/collections/repo/frontmatter-codec.test.ts new file mode 100644 index 0000000..b34caad --- /dev/null +++ b/src/domains/collections/repo/frontmatter-codec.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from "vite-plus/test"; +import { Effect } from "effect"; +import { FrontmatterCodecService, frontmatterCodecLayer } from "./frontmatter-codec"; + +describe("FrontmatterCodecService", () => { + it("parses scalar, object, and malformed YAML branches", async () => { + const parsed = await Effect.runPromise( + FrontmatterCodecService.use((service) => + service.parse(`--- +mixed: + nested: value + - scalar +items: + - name: first + invalid continuation +invalid top-level line +enabled: false +empty: null +single: 'quoted' +--- +Body`), + ).pipe(Effect.provide(frontmatterCodecLayer)), + ); + + expect(parsed).toMatchObject({ + data: { + mixed: ["scalar"], + items: [{ name: "first" }], + enabled: false, + empty: null, + single: "quoted", + }, + body: "Body", + }); + }); +}); diff --git a/src/domains/collections/repo/frontmatter-codec.ts b/src/domains/collections/repo/frontmatter-codec.ts new file mode 100644 index 0000000..15da66d --- /dev/null +++ b/src/domains/collections/repo/frontmatter-codec.ts @@ -0,0 +1,211 @@ +import { Context, Effect, Layer, Predicate } from "effect"; + +class CollectionYamlScalarService extends Context.Service< + CollectionYamlScalarService, + { readonly parse: (value: string) => Effect.Effect } +>()("@rendotdev/rig/collections/CollectionYamlScalarService", { + make: Effect.gen(function* () { + const parse = Effect.fn("CollectionYamlScalarService.parse")(function* (value: string) { + if (value === "true") return true; + if (value === "false") return false; + const isNullScalar = value === "null" || value === "~" || value === ""; + if (isNullScalar) return null; + const isDoubleQuoted = value.startsWith('"') && value.endsWith('"'); + const isSingleQuoted = value.startsWith("'") && value.endsWith("'"); + const isQuotedScalar = isDoubleQuoted || isSingleQuoted; + if (isQuotedScalar) return value.slice(1, -1); + const number = Number(value); + return !Number.isNaN(number) && value !== "" ? number : value; + }); + return { parse } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionYamlScalarService, + CollectionYamlScalarService.make, + ); +} + +class CollectionYamlParserService extends Context.Service< + CollectionYamlParserService, + { + readonly parse: (yaml: string) => Effect.Effect>; + } +>()("@rendotdev/rig/collections/CollectionYamlParserService", { + make: Effect.gen(function* () { + const scalar = yield* CollectionYamlScalarService; + const parse = Effect.fn("CollectionYamlParserService.parse")(function* (yaml: string) { + const result: Record = {}; + let currentKey: string | null = null; + let block: unknown[] | Record | null = null; + let arrayObject: Record | null = null; + for (const line of yaml.split(/\r?\n/)) { + const isArrayBlockEntry = block !== null && /^\s+-\s/.test(line); + if (isArrayBlockEntry) { + if (!Array.isArray(block)) block = []; + if (arrayObject !== null) block.push(arrayObject); + const value = line.replace(/^\s+-\s*/, ""); + const entry = value.match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); + arrayObject = entry ? { [entry[1]!]: yield* scalar.parse(entry[2]!.trim()) } : null; + if (!entry) block.push(yield* scalar.parse(value)); + continue; + } + const isArrayObjectContinuation = arrayObject !== null && /^\s{4,}\S/.test(line); + if (isArrayObjectContinuation) { + const entry = line.trim().match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); + if (entry) { + arrayObject![entry[1]!] = yield* scalar.parse(entry[2]!.trim()); + continue; + } + } + const isObjectBlockEntry = + block !== null && /^\s{2}[A-Za-z_]/.test(line) && !/^\s+-/.test(line); + if (isObjectBlockEntry) { + const isEmptyArrayBlock = Array.isArray(block) && block.length === 0; + if (isEmptyArrayBlock) block = {}; + const entry = line.trim().match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); + const canAssignObjectEntry = !Array.isArray(block) && entry !== null; + if (canAssignObjectEntry) { + (block as Record)[entry![1]!] = yield* scalar.parse(entry![2]!.trim()); + continue; + } + } + if (arrayObject !== null) { + if (Array.isArray(block)) block.push(arrayObject); + arrayObject = null; + } + const startsNextTopLevelEntry = + block !== null && currentKey !== null && /^[A-Za-z_]/.test(line); + if (startsNextTopLevelEntry) { + result[currentKey!] = block; + block = null; + } + const isBlankOrComment = !line.trim() || line.trim().startsWith("#"); + if (isBlankOrComment) continue; + const entry = line.match(/^([A-Za-z_][A-Za-z0-9_-]*)\s*:\s*(.*)$/); + if (!entry) continue; + currentKey = entry[1]!; + const value = entry[2]!.trim(); + if (value === "[]") result[currentKey] = []; + else if (value === "") block = []; + else { + const isInlineArray = value.startsWith("[") && value.endsWith("]"); + if (isInlineArray) { + result[currentKey] = yield* Effect.forEach(value.slice(1, -1).split(","), (item) => + scalar.parse(item.trim()), + ); + } else result[currentKey] = yield* scalar.parse(value); + } + } + const hasPendingArrayObject = arrayObject !== null && Array.isArray(block); + if (hasPendingArrayObject) (block as unknown[]).push(arrayObject); + const hasPendingBlock = block !== null && currentKey !== null; + if (hasPendingBlock) result[currentKey!] = block; + return result; + }); + return { parse } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionYamlParserService, + CollectionYamlParserService.make, + ); +} + +class CollectionYamlSerializerService extends Context.Service< + CollectionYamlSerializerService, + { + readonly serialize: (data: Record, indent?: string) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionYamlSerializerService", { + make: Effect.gen(function* () { + const serializeScalar = (value: unknown) => { + const isNullish = value === null || value === undefined; + if (isNullish) return "null"; + const isPrimitiveNumberOrBoolean = typeof value === "boolean" || typeof value === "number"; + if (isPrimitiveNumberOrBoolean) return String(value); + const string = String(value); + return /[:{},&*#?|>, indent = ""): string => { + const lines: string[] = []; + for (const [key, value] of Object.entries(data)) { + if (value === undefined) continue; + if (Array.isArray(value)) { + if (value.length === 0) lines.push(`${indent}${key}: []`); + else if (value.every((item) => typeof item !== "object" || item === null)) { + lines.push(`${indent}${key}: [${value.map(serializeScalar).join(", ")}]`); + } else { + lines.push(`${indent}${key}:`); + for (const item of value) { + if (Predicate.isObject(item)) { + const nested = render(item, `${indent} `).split("\n").filter(Boolean); + lines.push(`${indent} - ${nested[0]!.trim()}`); + for (const line of nested.slice(1)) lines.push(`${indent} ${line.trim()}`); + } else lines.push(`${indent} - ${serializeScalar(item)}`); + } + } + } else if (Predicate.isObject(value)) { + lines.push(`${indent}${key}:`); + lines.push(render(value, `${indent} `)); + } else lines.push(`${indent}${key}: ${serializeScalar(value)}`); + } + return `${lines.join("\n")}\n`; + }; + const serialize = Effect.fn("CollectionYamlSerializerService.serialize")(function* ( + data: Record, + indent = "", + ) { + return render(data, indent); + }); + return { serialize } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionYamlSerializerService, + CollectionYamlSerializerService.make, + ); +} + +export class FrontmatterCodecService extends Context.Service< + FrontmatterCodecService, + { + readonly parse: ( + content: string, + ) => Effect.Effect<{ data: Record; body: string }>; + readonly serialize: (data: Record, body: string) => Effect.Effect; + } +>()("@rendotdev/rig/collections/FrontmatterCodecService", { + make: Effect.gen(function* () { + const parser = yield* CollectionYamlParserService; + const serializer = yield* CollectionYamlSerializerService; + const pattern = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/; + const parse = Effect.fn("FrontmatterCodecService.parse")(function* (content: string) { + const match = content.match(pattern); + return match + ? { data: yield* parser.parse(match[1]!), body: match[2]!.replace(/^\n/, "") } + : { data: {}, body: content }; + }); + const serialize = Effect.fn("FrontmatterCodecService.serialize")(function* ( + data: Record, + body: string, + ) { + const normalizedBody = body.endsWith("\n") ? body : `${body}\n`; + return `---\n${yield* serializer.serialize(data)}---\n\n${normalizedBody}`; + }); + return { parse, serialize } as const; + }), +}) { + static readonly layer = Layer.effect(FrontmatterCodecService, FrontmatterCodecService.make); +} + +export const frontmatterCodecLayer = FrontmatterCodecService.layer.pipe( + Layer.provide( + Layer.merge( + CollectionYamlParserService.layer.pipe(Layer.provide(CollectionYamlScalarService.layer)), + CollectionYamlSerializerService.layer, + ), + ), +); diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-database.ts b/src/domains/collections/repo/sqlite/sqlite-collection-database.ts new file mode 100644 index 0000000..0f6edb3 --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-database.ts @@ -0,0 +1,188 @@ +import type { Database } from "bun:sqlite"; +import { rm } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer, Predicate, Result, Schema } from "effect"; +import { SqlitePlatformService } from "../../../../providers/database/sqlite"; +import { RigError } from "../../../../providers/errors/rig-error"; + +const COLLECTION_INDEX_SCHEMA_SQL = [ + `CREATE TABLE IF NOT EXISTS docs ( + id TEXT PRIMARY KEY, data_json TEXT NOT NULL, body TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL, updated_at TEXT NOT NULL, file_mtime REAL NOT NULL DEFAULT 0 + );`, + `CREATE TABLE IF NOT EXISTS collection_files ( + id TEXT PRIMARY KEY, mtime_ms REAL NOT NULL, ctime_ms REAL NOT NULL, + size INTEGER NOT NULL, status TEXT NOT NULL CHECK(status IN ('indexed', 'invalid')) + );`, + "CREATE TABLE IF NOT EXISTS collection_health (key TEXT PRIMARY KEY, checked_at INTEGER NOT NULL);", + "CREATE VIRTUAL TABLE IF NOT EXISTS docs_fts USING fts5(id, data_json, body, content=docs, content_rowid=rowid);", + `CREATE TRIGGER IF NOT EXISTS docs_ai AFTER INSERT ON docs BEGIN + INSERT INTO docs_fts(rowid, id, data_json, body) VALUES (new.rowid, new.id, new.data_json, new.body); + END;`, + `CREATE TRIGGER IF NOT EXISTS docs_ad AFTER DELETE ON docs BEGIN + INSERT INTO docs_fts(docs_fts, rowid, id, data_json, body) + VALUES ('delete', old.rowid, old.id, old.data_json, old.body); + END;`, + `CREATE TRIGGER IF NOT EXISTS docs_au AFTER UPDATE ON docs BEGIN + INSERT INTO docs_fts(docs_fts, rowid, id, data_json, body) + VALUES ('delete', old.rowid, old.id, old.data_json, old.body); + INSERT INTO docs_fts(rowid, id, data_json, body) VALUES (new.rowid, new.id, new.data_json, new.body); + END;`, +] as const; + +class CollectionIndexCorruptionError extends Schema.TaggedErrorClass()( + "CollectionIndexCorruptionError", + { message: Schema.String, cause: Schema.Defect() }, +) {} + +class CollectionIndexDatabaseInitializerService extends Context.Service< + CollectionIndexDatabaseInitializerService, + { + readonly initialize: ( + Database: new (path: string, options?: object) => Database, + path: string, + ) => Effect.Effect; + readonly isRecoverable: (error: unknown) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionIndexDatabaseInitializerService", { + make: Effect.gen(function* () { + const recoverable = (error: unknown) => { + if (error instanceof CollectionIndexCorruptionError) return true; + if (!Predicate.isObject(error)) return false; + const cause = error instanceof RigError ? error.details : error; + if (!Predicate.isObject(cause)) return false; + const code = "code" in cause ? String(cause.code) : undefined; + const isCorruptDatabaseCode = code === "SQLITE_NOTADB" || code?.startsWith("SQLITE_CORRUPT"); + if (isCorruptDatabaseCode) return true; + const sqliteError = code === "SQLITE_ERROR" || ("errno" in cause && cause.errno === 1); + return ( + sqliteError && + "message" in cause && + String(cause.message).toLowerCase().includes("no such column") + ); + }; + const validate = (database: Database) => { + database.query("SELECT id, data_json, body, created_at, updated_at FROM docs LIMIT 0").all(); + database + .query("SELECT id, mtime_ms, ctime_ms, size, status FROM collection_files LIMIT 0") + .all(); + database.query("SELECT rowid, id, data_json, body FROM docs_fts LIMIT 0").all(); + const checked = database + .query("SELECT checked_at FROM collection_health WHERE key = 'integrity'") + .get() as { checked_at: number } | null; + const hasRecentIntegrityCheck = + checked !== null && Date.now() - checked.checked_at < 86_400_000; + if (hasRecentIntegrityCheck) return; + const results = ( + database.query("PRAGMA quick_check;").all() as Record[] + ).flatMap((row) => Object.values(row)); + const hasFailedIntegrityCheck = results.length !== 1 || results[0] !== "ok"; + if (hasFailedIntegrityCheck) { + throw new CollectionIndexCorruptionError({ + message: `Collection index integrity check failed: ${results.join(", ")}`, + cause: results, + }); + } + database + .query( + `INSERT INTO collection_health (key, checked_at) VALUES ('integrity', $checkedAt) + ON CONFLICT(key) DO UPDATE SET checked_at = excluded.checked_at`, + ) + .run({ checkedAt: Date.now() }); + }; + const initialize = Effect.fn("CollectionIndexDatabaseInitializerService.initialize")(function* ( + Database: new (path: string, options?: object) => Database, + path: string, + ) { + return yield* Effect.try({ + try: () => { + const database = new Database(path, { create: true, strict: true }); + try { + database.run("PRAGMA busy_timeout = 5000;"); + database.run("PRAGMA journal_mode = WAL;"); + for (const sql of COLLECTION_INDEX_SCHEMA_SQL) database.run(sql); + validate(database); + return database; + } catch (error) { + database.close(false); + throw error; + } + }, + catch: (cause) => + cause instanceof CollectionIndexCorruptionError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }), + }); + }); + const isRecoverable = Effect.fn("CollectionIndexDatabaseInitializerService.isRecoverable")( + function* (error: unknown) { + return recoverable(error); + }, + ); + return { initialize, isRecoverable } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexDatabaseInitializerService, + CollectionIndexDatabaseInitializerService.make, + ); +} + +export class CollectionIndexDatabaseService extends Context.Service< + CollectionIndexDatabaseService, + { readonly open: (collectionPath: string) => Effect.Effect } +>()("@rendotdev/rig/collections/CollectionIndexDatabaseService", { + make: Effect.gen(function* () { + const sqlite = yield* SqlitePlatformService; + const initializer = yield* CollectionIndexDatabaseInitializerService; + const Database = (yield* sqlite.available) ? yield* sqlite.load : undefined; + const normalizeError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }); + const open = Effect.fn("CollectionIndexDatabaseService.open")(function* ( + collectionPath: string, + ) { + if (!Database) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: "Collections require the Bun SQLite runtime.", + }); + } + const path = join(collectionPath, ".index.sqlite"); + const first = yield* Effect.result(initializer.initialize(Database, path)); + if (Result.isSuccess(first)) return first.success; + if (!(yield* initializer.isRecoverable(first.failure))) { + return yield* normalizeError(first.failure); + } + yield* Effect.tryPromise({ + try: () => + Promise.all( + [path, `${path}-shm`, `${path}-wal`].map((file) => rm(file, { force: true })), + ), + catch: normalizeError, + }); + return yield* initializer.initialize(Database, path).pipe(Effect.mapError(normalizeError)); + }); + return { open } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexDatabaseService, + CollectionIndexDatabaseService.make, + ); +} + +export const collectionIndexDatabaseLayer = CollectionIndexDatabaseService.layer.pipe( + Layer.provide( + Layer.merge(SqlitePlatformService.layer, CollectionIndexDatabaseInitializerService.layer), + ), +); diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-index.ts b/src/domains/collections/repo/sqlite/sqlite-collection-index.ts new file mode 100644 index 0000000..3a919c6 --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-index.ts @@ -0,0 +1,85 @@ +import type { Database } from "bun:sqlite"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; +import type { + CollectionEntry, + CollectionFileRecord, + CollectionIndexInterface, + DocRow, + ListOptions, +} from "../../types/tool-collection"; +import { CollectionIndexDatabaseService } from "./sqlite-collection-database"; +import { collectionIndexLayer } from "./sqlite-collection-layer"; +import { CollectionIndexQueryReaderService } from "./sqlite-collection-query-reader"; +import { CollectionIndexRecordReaderService } from "./sqlite-collection-record-reader"; +import { CollectionIndexWriterService } from "./sqlite-collection-writer"; + +export class SqliteCollectionIndexService extends Context.Service< + SqliteCollectionIndexService, + { readonly create: (collectionPath: string) => Effect.Effect } +>()("@rendotdev/rig/collections/SqliteCollectionIndexService", { + make: Effect.gen(function* () { + const databases = yield* CollectionIndexDatabaseService; + const writers = yield* CollectionIndexWriterService; + const records = yield* CollectionIndexRecordReaderService; + const queries = yield* CollectionIndexQueryReaderService; + const runtimeContext = yield* Effect.context(); + const runPromise = Effect.runPromiseWith(runtimeContext); + const runSync = Effect.runSyncWith(runtimeContext); + const create = Effect.fn("SqliteCollectionIndexService.create")(function* ( + collectionPath: string, + ) { + let database: Database | undefined; + let initialization: Promise | undefined; + const current = () => { + if (!database) { + throw new RigError({ code: "TOOL_INVALID", message: "Collection index is not open." }); + } + return database; + }; + const initialize = async () => { + database = await runPromise(databases.open(collectionPath)); + }; + const open = () => + (initialization ??= initialize().catch((error) => { + initialization = undefined; + throw error; + })); + const close = () => { + database?.close(false); + database = undefined; + initialization = undefined; + }; + return { + open, + upsertDoc: (entry: CollectionEntry>, fileMtime: number) => + runSync(writers.upsertDoc(current(), entry, fileMtime)), + deleteDoc: (id: string) => runSync(writers.deleteDoc(current(), id)), + upsertFile: (record: CollectionFileRecord) => + runSync(writers.upsertFile(current(), record)), + deleteFile: (id: string) => runSync(writers.deleteFile(current(), id)), + getFile: (id: string) => runSync(records.getFile(current(), id)), + allFileIds: () => runSync(records.allFileIds(current())), + getDoc: (id: string): DocRow | null => runSync(records.getDoc(current(), id)), + listDocs: (options: ListOptions) => runSync(queries.listDocs(current(), options)), + searchDocs: (query: string, limit: number) => + runSync(queries.searchDocs(current(), query, limit)), + countDocs: (where?: Record) => + runSync(queries.countDocs(current(), where)), + allIds: () => runSync(records.allIds(current())), + clearAll: () => runSync(writers.clearAll(current())), + close, + }; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect( + SqliteCollectionIndexService, + SqliteCollectionIndexService.make, + ); +} + +export const sqliteCollectionIndexLayer = SqliteCollectionIndexService.layer.pipe( + Layer.provide(collectionIndexLayer), +); diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-layer.ts b/src/domains/collections/repo/sqlite/sqlite-collection-layer.ts new file mode 100644 index 0000000..8d8de56 --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-layer.ts @@ -0,0 +1,12 @@ +import { Layer } from "effect"; +import { collectionIndexDatabaseLayer } from "./sqlite-collection-database"; +import { collectionIndexQueryReaderLayer } from "./sqlite-collection-query-reader"; +import { CollectionIndexRecordReaderService } from "./sqlite-collection-record-reader"; +import { CollectionIndexWriterService } from "./sqlite-collection-writer"; + +export const collectionIndexLayer = Layer.mergeAll( + collectionIndexDatabaseLayer, + CollectionIndexWriterService.layer, + CollectionIndexRecordReaderService.layer, + collectionIndexQueryReaderLayer, +); diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-query-compiler.ts b/src/domains/collections/repo/sqlite/sqlite-collection-query-compiler.ts new file mode 100644 index 0000000..4847ac8 --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-query-compiler.ts @@ -0,0 +1,51 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; + +export class CollectionIndexQueryCompilerService extends Context.Service< + CollectionIndexQueryCompilerService, + { + readonly fieldPath: (value: string) => Effect.Effect; + readonly where: (values: Record) => Effect.Effect< + { + readonly sql: string; + readonly parameters: Record; + }, + RigError + >; + } +>()("@rendotdev/rig/collections/CollectionIndexQueryCompilerService", { + make: Effect.gen(function* () { + const fieldPath = Effect.fn("CollectionIndexQueryCompilerService.fieldPath")(function* ( + value: string, + ) { + const segments = value.split("."); + const hasInvalidSegment = segments.some((part) => !/^[A-Za-z_][A-Za-z0-9_-]*$/.test(part)); + const isInvalidPath = value.length === 0 || hasInvalidSegment; + if (isInvalidPath) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: `Invalid collection field path: ${value}`, + }); + } + return `$${segments.map((segment) => `.${segment}`).join("")}`; + }); + const where = Effect.fn("CollectionIndexQueryCompilerService.where")(function* ( + values: Record, + ) { + const parameters: Record = {}; + const conditions: string[] = []; + for (const [index, [key, value]] of Object.entries(values).entries()) { + const name = `where_${index}`; + parameters[name] = typeof value === "object" ? JSON.stringify(value) : value; + conditions.push(`json_extract(data_json, '${yield* fieldPath(key)}') = $${name}`); + } + return { sql: conditions.join(" AND "), parameters } as const; + }); + return { fieldPath, where } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexQueryCompilerService, + CollectionIndexQueryCompilerService.make, + ); +} diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-query-reader.ts b/src/domains/collections/repo/sqlite/sqlite-collection-query-reader.ts new file mode 100644 index 0000000..34fa94b --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-query-reader.ts @@ -0,0 +1,109 @@ +import type { Database } from "bun:sqlite"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; +import type { DocRow, ListOptions } from "../../types/tool-collection"; +import { CollectionIndexQueryCompilerService } from "./sqlite-collection-query-compiler"; + +export class CollectionIndexQueryReaderService extends Context.Service< + CollectionIndexQueryReaderService, + { + readonly listDocs: ( + database: Database, + options: ListOptions, + ) => Effect.Effect<{ rows: DocRow[]; total: number }, RigError>; + readonly searchDocs: ( + database: Database, + text: string, + limit: number, + ) => Effect.Effect; + readonly countDocs: ( + database: Database, + where?: Record, + ) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionIndexQueryReaderService", { + make: Effect.gen(function* () { + const query = yield* CollectionIndexQueryCompilerService; + const run = (operation: () => Value) => + Effect.try({ + try: operation, + catch: (cause) => + new RigError({ code: "INTERNAL_ERROR", message: String(cause), details: cause }), + }); + const listDocs = Effect.fn("CollectionIndexQueryReaderService.listDocs")(function* ( + database: Database, + options: ListOptions, + ) { + const filter = + options.where && Object.keys(options.where).length > 0 + ? yield* query.where(options.where) + : { sql: "", parameters: {} }; + const where = filter.sql ? `WHERE ${filter.sql}` : ""; + const descending = options.sort?.startsWith("-") ?? false; + const sort = options.sort + ? yield* query.fieldPath(descending ? options.sort.slice(1) : options.sort) + : undefined; + const order = sort + ? `ORDER BY json_extract(data_json, '${sort}') ${descending ? "DESC" : "ASC"}` + : "ORDER BY updated_at DESC"; + const limit = options.limit ?? 50; + const offset = options.offset ?? 0; + return yield* run(() => { + const total = ( + database + .query(`SELECT COUNT(*) as cnt FROM docs ${where}`) + .get(filter.parameters as any) as { cnt: number } + ).cnt; + const rows = database + .query(`SELECT * FROM docs ${where} ${order} LIMIT $limit OFFSET $offset`) + .all({ ...filter.parameters, limit, offset } as any) as DocRow[]; + return { rows, total }; + }); + }); + const searchDocs = Effect.fn("CollectionIndexQueryReaderService.searchDocs")(function* ( + database: Database, + text: string, + limit: number, + ) { + return yield* run( + () => + database + .query( + `SELECT docs.*, docs_fts.rank FROM docs_fts JOIN docs ON docs.id = docs_fts.id + WHERE docs_fts MATCH $query ORDER BY docs_fts.rank LIMIT $limit`, + ) + .all({ query: text, limit }) as DocRow[], + ); + }); + const countDocs = Effect.fn("CollectionIndexQueryReaderService.countDocs")(function* ( + database: Database, + where?: Record, + ) { + const hasNoFilters = !where || Object.keys(where).length === 0; + if (hasNoFilters) { + return yield* run( + () => (database.query("SELECT COUNT(*) as cnt FROM docs").get() as { cnt: number }).cnt, + ); + } + const filter = yield* query.where(where); + return yield* run( + () => + ( + database + .query(`SELECT COUNT(*) as cnt FROM docs WHERE ${filter.sql}`) + .get(filter.parameters as any) as { cnt: number } + ).cnt, + ); + }); + return { listDocs, searchDocs, countDocs } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexQueryReaderService, + CollectionIndexQueryReaderService.make, + ); +} + +export const collectionIndexQueryReaderLayer = CollectionIndexQueryReaderService.layer.pipe( + Layer.provide(CollectionIndexQueryCompilerService.layer), +); diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-record-reader.ts b/src/domains/collections/repo/sqlite/sqlite-collection-record-reader.ts new file mode 100644 index 0000000..08a894c --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-record-reader.ts @@ -0,0 +1,75 @@ +import type { Database } from "bun:sqlite"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; +import type { CollectionFileRecord, DocRow } from "../../types/tool-collection"; + +export class CollectionIndexRecordReaderService extends Context.Service< + CollectionIndexRecordReaderService, + { + readonly getFile: ( + database: Database, + id: string, + ) => Effect.Effect; + readonly allFileIds: (database: Database) => Effect.Effect; + readonly getDoc: (database: Database, id: string) => Effect.Effect; + readonly allIds: (database: Database) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionIndexRecordReaderService", { + make: Effect.gen(function* () { + const run = (operation: () => Value) => + Effect.try({ + try: operation, + catch: (cause) => + new RigError({ code: "INTERNAL_ERROR", message: String(cause), details: cause }), + }); + const getFile = Effect.fn("CollectionIndexRecordReaderService.getFile")(function* ( + database: Database, + id: string, + ) { + return yield* run(() => { + const row = database + .query("SELECT id, mtime_ms, ctime_ms, size, status FROM collection_files WHERE id = $id") + .get({ id }) as any; + return row + ? { + id: row.id, + mtimeMs: row.mtime_ms, + ctimeMs: row.ctime_ms, + size: row.size, + status: row.status, + } + : null; + }); + }); + const allFileIds = Effect.fn("CollectionIndexRecordReaderService.allFileIds")(function* ( + database: Database, + ) { + return yield* run(() => + (database.query("SELECT id FROM collection_files").all() as { id: string }[]).map( + (row) => row.id, + ), + ); + }); + const getDoc = Effect.fn("CollectionIndexRecordReaderService.getDoc")(function* ( + database: Database, + id: string, + ) { + return yield* run( + () => database.query("SELECT * FROM docs WHERE id = $id").get({ id }) as DocRow | null, + ); + }); + const allIds = Effect.fn("CollectionIndexRecordReaderService.allIds")(function* ( + database: Database, + ) { + return yield* run(() => + (database.query("SELECT id FROM docs").all() as { id: string }[]).map((row) => row.id), + ); + }); + return { getFile, allFileIds, getDoc, allIds } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexRecordReaderService, + CollectionIndexRecordReaderService.make, + ); +} diff --git a/src/domains/collections/repo/sqlite/sqlite-collection-writer.ts b/src/domains/collections/repo/sqlite/sqlite-collection-writer.ts new file mode 100644 index 0000000..ab4da92 --- /dev/null +++ b/src/domains/collections/repo/sqlite/sqlite-collection-writer.ts @@ -0,0 +1,95 @@ +import type { Database } from "bun:sqlite"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; +import type { CollectionEntry, CollectionFileRecord } from "../../types/tool-collection"; + +export class CollectionIndexWriterService extends Context.Service< + CollectionIndexWriterService, + { + readonly upsertDoc: ( + database: Database, + entry: CollectionEntry>, + fileMtime: number, + ) => Effect.Effect; + readonly deleteDoc: (database: Database, id: string) => Effect.Effect; + readonly upsertFile: ( + database: Database, + record: CollectionFileRecord, + ) => Effect.Effect; + readonly deleteFile: (database: Database, id: string) => Effect.Effect; + readonly clearAll: (database: Database) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionIndexWriterService", { + make: Effect.gen(function* () { + const run = (operation: () => unknown) => + Effect.try({ + try: () => void operation(), + catch: (cause) => + new RigError({ code: "INTERNAL_ERROR", message: String(cause), details: cause }), + }); + const upsertDoc = Effect.fn("CollectionIndexWriterService.upsertDoc")(function* ( + database: Database, + entry: CollectionEntry>, + fileMtime: number, + ) { + yield* run(() => + database + .query( + `INSERT INTO docs (id, data_json, body, created_at, updated_at, file_mtime) + VALUES ($id, $dataJson, $body, $createdAt, $updatedAt, $fileMtime) + ON CONFLICT(id) DO UPDATE SET data_json = excluded.data_json, + body = excluded.body, updated_at = excluded.updated_at, file_mtime = excluded.file_mtime`, + ) + .run({ + id: entry.id, + dataJson: JSON.stringify(entry.data), + body: entry.body, + createdAt: entry.createdAt, + updatedAt: entry.updatedAt, + fileMtime, + }), + ); + }); + const deleteDoc = Effect.fn("CollectionIndexWriterService.deleteDoc")(function* ( + database: Database, + id: string, + ) { + yield* run(() => database.query("DELETE FROM docs WHERE id = $id").run({ id })); + }); + const upsertFile = Effect.fn("CollectionIndexWriterService.upsertFile")(function* ( + database: Database, + record: CollectionFileRecord, + ) { + yield* run(() => + database + .query( + `INSERT INTO collection_files (id, mtime_ms, ctime_ms, size, status) + VALUES ($id, $mtimeMs, $ctimeMs, $size, $status) + ON CONFLICT(id) DO UPDATE SET mtime_ms = excluded.mtime_ms, + ctime_ms = excluded.ctime_ms, size = excluded.size, status = excluded.status`, + ) + .run(record), + ); + }); + const deleteFile = Effect.fn("CollectionIndexWriterService.deleteFile")(function* ( + database: Database, + id: string, + ) { + yield* run(() => database.query("DELETE FROM collection_files WHERE id = $id").run({ id })); + }); + const clearAll = Effect.fn("CollectionIndexWriterService.clearAll")(function* ( + database: Database, + ) { + yield* run(() => { + database.run("DELETE FROM docs"); + database.run("DELETE FROM collection_files"); + }); + }); + return { upsertDoc, deleteDoc, upsertFile, deleteFile, clearAll } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionIndexWriterService, + CollectionIndexWriterService.make, + ); +} diff --git a/src/domains/collections/runtime/collection-handle-factory.ts b/src/domains/collections/runtime/collection-handle-factory.ts new file mode 100644 index 0000000..3ddae52 --- /dev/null +++ b/src/domains/collections/runtime/collection-handle-factory.ts @@ -0,0 +1,258 @@ +import { mkdir } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer, Scope } from "effect"; +import { FileLockService, fileLockLayer } from "../../../providers/filesystem/file-lock"; +import { RigError } from "../../../providers/errors/rig-error"; +import { + CollectionOperationService, + CollectionReadService, + collectionOperationLayer, +} from "../service/collection-operation-layer"; +import type { + CollectionDefinition, + CollectionEntry, + CollectionHandle, + CollectionIndexInterface, + ListOptions, + ManagedCollectionHandle, +} from "../types/tool-collection"; + +export class CollectionHandleResourceService extends Context.Service< + CollectionHandleResourceService, + { + readonly create: ( + name: string, + path: string, + definition: CollectionDefinition, + index?: CollectionIndexInterface, + ) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionHandleResourceService", { + make: Effect.gen(function* () { + const operations = yield* CollectionOperationService; + const reads = yield* CollectionReadService; + const initialization = yield* CollectionHandleInitializationService; + const runtimeContext = yield* Effect.context(); + const create = Effect.fn("CollectionHandleResourceService.create")(function* ( + name: string, + path: string, + definition: CollectionDefinition, + provided?: CollectionIndexInterface, + ) { + const index = provided ?? (yield* operations.createIndex(path)); + const context = { path, definition, index } as const; + const run = Effect.runPromiseWith(runtimeContext); + const init = () => run(initialization.initialize(path, index, operations.reconcile(context))); + const close = () => index.close(); + const handle: ManagedCollectionHandle = { + name, + path, + create: (input) => run(operations.create(context, input)), + getEntry: (id) => run(operations.getEntry(context, id)), + update: (id, patch) => run(operations.update(context, id, patch)), + upsert: (input) => run(operations.upsert(context, input)), + remove: (id) => run(operations.remove(context, id)), + list: (options) => run(reads.list(context, options)), + search: (query, options) => run(reads.search(context, query, options)), + count: (where) => run(reads.count(context, where)), + getCollection: (filter) => run(reads.getCollection(context, filter)), + clear: () => run(operations.clear(context)), + init, + close, + }; + return handle; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionHandleResourceService, + CollectionHandleResourceService.make, + ); +} + +export class CollectionHandleInitializationService extends Context.Service< + CollectionHandleInitializationService, + { + readonly initialize: ( + path: string, + index: CollectionIndexInterface, + reconciliation: Effect.Effect, + ) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionHandleInitializationService", { + make: Effect.gen(function* () { + const locks = yield* FileLockService; + const normalizeError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: cause, + }); + const initialize = Effect.fn("CollectionHandleInitializationService.initialize")(function* ( + path: string, + index: CollectionIndexInterface, + reconciliation: Effect.Effect, + ) { + yield* locks + .withLock({ + targetPath: join(path, ".index.sqlite"), + operation: () => + Effect.gen(function* () { + yield* Effect.tryPromise({ + try: () => mkdir(path, { recursive: true }), + catch: normalizeError, + }); + yield* Effect.tryPromise({ try: () => index.open(), catch: normalizeError }); + yield* reconciliation; + }), + }) + .pipe(Effect.mapError(normalizeError)); + }); + return { initialize } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionHandleInitializationService, + CollectionHandleInitializationService.make, + ); +} + +export class LazyCollectionHandleService extends Context.Service< + LazyCollectionHandleService, + { + readonly create: >( + handle: ManagedCollectionHandle, + ) => Effect.Effect & { close(): void }>; + } +>()("@rendotdev/rig/collections/LazyCollectionHandleService", { + make: Effect.gen(function* () { + const create = Effect.fn("LazyCollectionHandleService.create")(function* < + T extends Record, + >(handle: ManagedCollectionHandle) { + let initialization: Promise | undefined; + let initialized: ManagedCollectionHandle | undefined; + const resource = () => + (initialization ??= handle + .init() + .then(() => (initialized = handle)) + .catch((error) => { + handle.close(); + initialization = undefined; + throw error; + })); + const createEntry = (entry: { id?: string; data: T; body?: string }) => + resource().then((value) => value.create(entry)); + const getEntry = (id: string) => resource().then((value) => value.getEntry(id)); + const update = (id: string, patch: { data?: Partial; body?: string }) => + resource().then((value) => value.update(id, patch)); + const upsert = (entry: { id: string; data: T; body?: string }) => + resource().then((value) => value.upsert(entry)); + const remove = (id: string) => resource().then((value) => value.remove(id)); + const list = (options?: ListOptions) => resource().then((value) => value.list(options)); + const search = (query: string, options?: { limit?: number }) => + resource().then((value) => value.search(query, options)); + const count = (where?: Record) => + resource().then((value) => value.count(where)); + const getCollection = (filter?: (entry: CollectionEntry) => boolean) => + resource().then((value) => value.getCollection(filter)); + const clear = () => resource().then((value) => value.clear()); + const close = () => initialized?.close(); + return { + name: handle.name, + path: handle.path, + create: createEntry, + getEntry, + update, + upsert, + remove, + list, + search, + count, + getCollection, + clear, + close, + }; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect( + LazyCollectionHandleService, + LazyCollectionHandleService.make, + ); +} + +export class CollectionHandleFactoryService extends Context.Service< + CollectionHandleFactoryService, + { + readonly createLazy: >( + name: string, + path: string, + definition: CollectionDefinition, + index?: CollectionIndexInterface, + ) => Effect.Effect & { close(): void }, RigError>; + readonly acquire: >( + name: string, + path: string, + definition: CollectionDefinition, + index?: CollectionIndexInterface, + ) => Effect.Effect, RigError, Scope.Scope>; + } +>()("@rendotdev/rig/collections/CollectionHandleFactoryService", { + make: Effect.gen(function* () { + const resources = yield* CollectionHandleResourceService; + const lazyHandles = yield* LazyCollectionHandleService; + const toRigError = (error: unknown) => { + if (error instanceof RigError) return error; + return new RigError({ + code: "INTERNAL_ERROR", + message: error instanceof Error ? error.message : String(error), + }); + }; + const createLazy = Effect.fn("CollectionHandleFactoryService.createLazy")(function* < + T extends Record, + >( + name: string, + path: string, + definition: CollectionDefinition, + index?: CollectionIndexInterface, + ) { + return yield* lazyHandles.create(yield* resources.create(name, path, definition, index)); + }); + const acquire = Effect.fn("CollectionHandleFactoryService.acquire")(function* < + T extends Record, + >( + name: string, + path: string, + definition: CollectionDefinition, + index?: CollectionIndexInterface, + ) { + const handle = yield* resources.create(name, path, definition, index); + return yield* Effect.acquireRelease( + Effect.tryPromise({ try: () => handle.init(), catch: toRigError }).pipe( + Effect.as(handle as CollectionHandle), + ), + () => Effect.sync(() => handle.close()), + ); + }); + return { createLazy, acquire } as const; + }), +}) { + static readonly layer = Layer.effect( + CollectionHandleFactoryService, + CollectionHandleFactoryService.make, + ); +} + +const collectionHandleInitializationLayer = CollectionHandleInitializationService.layer.pipe( + Layer.provide(fileLockLayer), +); +const collectionHandleResourceLayer = CollectionHandleResourceService.layer.pipe( + Layer.provide(Layer.mergeAll(collectionOperationLayer, collectionHandleInitializationLayer)), +); +export const collectionHandleFactoryLayer = CollectionHandleFactoryService.layer.pipe( + Layer.provide(Layer.mergeAll(collectionHandleResourceLayer, LazyCollectionHandleService.layer)), +); diff --git a/src/domains/collections/runtime/collection-handle-reliability.test.ts b/src/domains/collections/runtime/collection-handle-reliability.test.ts new file mode 100644 index 0000000..390eef3 --- /dev/null +++ b/src/domains/collections/runtime/collection-handle-reliability.test.ts @@ -0,0 +1,140 @@ +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it } from "vite-plus/test"; +import { Effect, Layer } from "effect"; +import { + CollectionHandleFactoryService, + CollectionHandleInitializationService, + CollectionHandleResourceService, + LazyCollectionHandleService, + collectionHandleFactoryLayer, +} from "./collection-handle-factory"; +import type { CollectionIndexInterface, ManagedCollectionHandle } from "../types/tool-collection"; +import { fileLockLayer } from "../../../providers/filesystem/file-lock"; +import { RigError } from "../../../providers/errors/rig-error"; +import { MemoryCollectionIndexService } from "./fixtures/memory-collection-index"; + +const makeMemoryIndex = (): CollectionIndexInterface => + Effect.runSync( + MemoryCollectionIndexService.use((service) => service.create).pipe( + Effect.provide(MemoryCollectionIndexService.layer), + ), + ); + +class TrackingMemoryCollectionIndex implements CollectionIndexInterface { + openRuns = 0; + closeRuns = 0; + + constructor(private readonly index = makeMemoryIndex()) {} + + open = () => { + this.openRuns++; + return this.index.open(); + }; + upsertDoc = (...args: Parameters) => + this.index.upsertDoc(...args); + deleteDoc = (id: string) => this.index.deleteDoc(id); + upsertFile = (...args: Parameters) => + this.index.upsertFile(...args); + deleteFile = (id: string) => this.index.deleteFile(id); + getFile = (id: string) => this.index.getFile(id); + allFileIds = () => this.index.allFileIds(); + getDoc = (id: string) => this.index.getDoc(id); + listDocs = (...args: Parameters) => + this.index.listDocs(...args); + searchDocs = (...args: Parameters) => + this.index.searchDocs(...args); + countDocs = (...args: Parameters) => + this.index.countDocs(...args); + allIds = () => this.index.allIds(); + clearAll = () => this.index.clearAll(); + close = () => { + this.closeRuns++; + this.index.close(); + }; +} + +describe("CollectionHandle reliability", () => { + it("initializes and closes scoped handles", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-collection-scoped-")); + const index = new TrackingMemoryCollectionIndex(); + try { + await Effect.runPromise( + Effect.scoped( + CollectionHandleFactoryService.use((service) => + service.acquire("notes", directory, {}, index), + ), + ).pipe(Effect.provide(collectionHandleFactoryLayer)), + ); + + expect(index.openRuns).toBe(1); + expect(index.closeRuns).toBe(1); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); + + it("normalizes collection initialization failures", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-collection-failure-")); + const initializationLayer = CollectionHandleInitializationService.layer.pipe( + Layer.provide(fileLockLayer), + ); + try { + const errorIndex = makeMemoryIndex(); + errorIndex.open = () => Promise.reject(new Error("database unavailable")); + const error = await Effect.runPromise( + CollectionHandleInitializationService.use((service) => + service.initialize(directory, errorIndex, Effect.void), + ).pipe(Effect.provide(initializationLayer), Effect.flip), + ); + expect(error).toBeInstanceOf(RigError); + expect(error.message).toBe("database unavailable"); + + const stringIndex = makeMemoryIndex(); + stringIndex.open = () => Promise.reject("database unavailable as text"); + const stringError = await Effect.runPromise( + CollectionHandleInitializationService.use((service) => + service.initialize(directory, stringIndex, Effect.void), + ).pipe(Effect.provide(initializationLayer), Effect.flip), + ); + expect(stringError).toBeInstanceOf(RigError); + expect(stringError.message).toBe("database unavailable as text"); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); + + it("normalizes raw scoped resource failures", async () => { + const failures: readonly unknown[] = [ + new RigError({ code: "INTERNAL_ERROR", message: "typed failure" }), + new Error("error failure"), + "text failure", + ]; + await Promise.all( + failures.map(async (failure) => { + const handle = { + name: "notes", + path: "/tmp/unused", + init: () => Promise.reject(failure), + close: () => undefined, + } as ManagedCollectionHandle; + const resourceLayer = Layer.succeed(CollectionHandleResourceService, { + create: () => Effect.succeed(handle), + }); + const factoryLayer = CollectionHandleFactoryService.layer.pipe( + Layer.provide(Layer.merge(resourceLayer, LazyCollectionHandleService.layer)), + ); + const received = await Effect.runPromise( + Effect.scoped( + CollectionHandleFactoryService.use((service) => + service.acquire("notes", "/tmp/unused", {}), + ), + ).pipe(Effect.provide(factoryLayer), Effect.flip), + ); + expect(received).toBeInstanceOf(RigError); + expect(received.message).toBe(failure instanceof Error ? failure.message : String(failure)); + }), + ); + }); +}); diff --git a/src/domains/collections/runtime/collection-handle.test.ts b/src/domains/collections/runtime/collection-handle.test.ts new file mode 100644 index 0000000..a4c8497 --- /dev/null +++ b/src/domains/collections/runtime/collection-handle.test.ts @@ -0,0 +1,587 @@ +import { existsSync } from "node:fs"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test"; +import { Effect } from "effect"; +import { z } from "zod"; +import { + CollectionHandleFactoryService, + collectionHandleFactoryLayer, +} from "./collection-handle-factory"; +import type { + CollectionDefinition, + CollectionHandle, + CollectionIndexInterface, +} from "../types/tool-collection"; +import { MemoryCollectionIndexService } from "./fixtures/memory-collection-index"; + +type ClosableCollectionHandle> = CollectionHandle & { + close(): void; +}; + +const makeMemoryIndex = (): CollectionIndexInterface => + Effect.runSync( + MemoryCollectionIndexService.use((service) => service.create).pipe( + Effect.provide(MemoryCollectionIndexService.layer), + ), + ); + +const makeCollectionHandle = >( + name: string, + path: string, + definition: CollectionDefinition = {}, + index: CollectionIndexInterface = makeMemoryIndex(), +): Promise> => + Effect.runPromise( + CollectionHandleFactoryService.use((service) => + service.createLazy(name, path, definition, index), + ).pipe(Effect.provide(collectionHandleFactoryLayer)), + ); + +class TrackingMemoryCollectionIndex implements CollectionIndexInterface { + upsertCalls = 0; + + constructor(private readonly index = makeMemoryIndex()) {} + + upsertDoc(...args: Parameters): void { + this.upsertCalls++; + this.index.upsertDoc(...args); + } + + open = () => this.index.open(); + deleteDoc = (id: string) => this.index.deleteDoc(id); + upsertFile = (...args: Parameters) => + this.index.upsertFile(...args); + deleteFile = (id: string) => this.index.deleteFile(id); + getFile = (id: string) => this.index.getFile(id); + allFileIds = () => this.index.allFileIds(); + getDoc = (id: string) => this.index.getDoc(id); + listDocs = (...args: Parameters) => + this.index.listDocs(...args); + searchDocs = (...args: Parameters) => + this.index.searchDocs(...args); + countDocs = (...args: Parameters) => + this.index.countDocs(...args); + allIds = () => this.index.allIds(); + clearAll = () => this.index.clearAll(); + close = () => this.index.close(); +} + +describe("CollectionHandle", () => { + let dir: string; + let handle: ClosableCollectionHandle; + + const schema = z.object({ + ticket: z.string(), + summary: z.string(), + status: z.enum(["open", "in-progress", "done"]).default("open"), + priority: z.number().optional(), + }); + + beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), "rig-collection-test-")); + handle = await makeCollectionHandle( + "test-collection", + dir, + { schema, generateId: (data: any) => data.ticket }, + makeMemoryIndex(), + ); + await handle.count(); + }); + + afterEach(async () => { + handle.close(); + await rm(dir, { recursive: true, force: true }); + }); + + it("creates an entry and reads it back", async () => { + const entry = await handle.create({ + data: { ticket: "CONSOLE-100", summary: "Fix bug", status: "open" }, + body: "## Notes\nSome content", + }); + + expect(entry.id).toBe("CONSOLE-100"); + expect(entry.data.ticket).toBe("CONSOLE-100"); + expect(entry.data.summary).toBe("Fix bug"); + expect(entry.body).toBe("## Notes\nSome content"); + + const fetched = await handle.getEntry("CONSOLE-100"); + expect(fetched).not.toBeNull(); + expect(fetched!.id).toBe("CONSOLE-100"); + expect(fetched!.data.summary).toBe("Fix bug"); + }); + + it("uses generateId from data", async () => { + const entry = await handle.create({ + data: { ticket: "CONSOLE-200", summary: "Another bug", status: "open" }, + }); + expect(entry.id).toBe("CONSOLE-200"); + }); + + it("allows explicit id override", async () => { + const entry = await handle.create({ + id: "custom-id", + data: { ticket: "CONSOLE-300", summary: "Custom", status: "open" }, + }); + expect(entry.id).toBe("custom-id"); + }); + + it("rejects entry ids that could escape the collection", async () => { + const invalidIds = ["", ".", "..", "../escaped", "/tmp/escaped", "nested/id", "nested\\id"]; + const outsidePath = join(dir, "..", "escaped.md"); + + await Promise.all( + invalidIds.map(async (id) => { + await expect( + handle.create({ + id, + data: { ticket: "SAFE-1", summary: "Unsafe id", status: "open" }, + }), + ).rejects.toThrow("Invalid collection entry id"); + await expect(handle.getEntry(id)).rejects.toThrow("Invalid collection entry id"); + await expect(handle.update(id, { body: "unsafe" })).rejects.toThrow( + "Invalid collection entry id", + ); + await expect( + handle.upsert({ + id, + data: { ticket: "SAFE-1", summary: "Unsafe id", status: "open" }, + }), + ).rejects.toThrow("Invalid collection entry id"); + await expect(handle.remove(id)).rejects.toThrow("Invalid collection entry id"); + }), + ); + + expect(existsSync(outsidePath)).toBe(false); + }); + + it("validates ids returned by generateId", async () => { + await expect( + handle.create({ + data: { ticket: "../generated-escape", summary: "Unsafe generated id", status: "open" }, + }), + ).rejects.toThrow("Invalid collection entry id"); + }); + + it("throws on duplicate create", async () => { + await handle.create({ + data: { ticket: "CONSOLE-100", summary: "First", status: "open" }, + }); + await expect( + handle.create({ + data: { ticket: "CONSOLE-100", summary: "Duplicate", status: "open" }, + }), + ).rejects.toThrow("already exists"); + }); + + it("validates data against schema", async () => { + await expect( + handle.create({ + id: "bad", + data: { ticket: 123, summary: "Bad" } as any, + }), + ).rejects.toThrow("invalid"); + }); + + it("updates entry data (merge) and body", async () => { + await handle.create({ + data: { ticket: "CONSOLE-100", summary: "Original", status: "open", priority: 1 }, + body: "Original body", + }); + + const updated = await handle.update("CONSOLE-100", { + data: { status: "done" }, + body: "Updated body", + }); + + expect(updated.data.status).toBe("done"); + expect(updated.data.summary).toBe("Original"); + expect(updated.data.priority).toBe(1); + expect(updated.body).toBe("Updated body"); + }); + + it("removes an entry", async () => { + await handle.create({ + data: { ticket: "CONSOLE-100", summary: "To delete", status: "open" }, + }); + + const removed = await handle.remove("CONSOLE-100"); + expect(removed).toBe(true); + + const fetched = await handle.getEntry("CONSOLE-100"); + expect(fetched).toBeNull(); + }); + + it("remove returns false for non-existent", async () => { + const removed = await handle.remove("non-existent"); + expect(removed).toBe(false); + }); + + it("upserts: creates if missing, updates if exists", async () => { + const r1 = await handle.upsert({ + id: "CONSOLE-100", + data: { ticket: "CONSOLE-100", summary: "Created", status: "open" }, + }); + expect(r1.created).toBe(true); + + const r2 = await handle.upsert({ + id: "CONSOLE-100", + data: { ticket: "CONSOLE-100", summary: "Updated", status: "done" }, + }); + expect(r2.created).toBe(false); + + const entry = await handle.getEntry("CONSOLE-100"); + expect(entry!.data.summary).toBe("Updated"); + expect(entry!.data.status).toBe("done"); + }); + + it("lists entries with where filter", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); + await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); + await handle.create({ data: { ticket: "A-3", summary: "Three", status: "open" } }); + + const { entries, total } = await handle.list({ where: { status: "open" } }); + expect(total).toBe(2); + expect(entries.map((e) => e.id).toSorted()).toEqual(["A-1", "A-3"]); + }); + + it("lists entries with sort", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open", priority: 3 } }); + await handle.create({ data: { ticket: "A-2", summary: "Two", status: "open", priority: 1 } }); + await handle.create({ data: { ticket: "A-3", summary: "Three", status: "open", priority: 2 } }); + + const { entries } = await handle.list({ sort: "priority" }); + expect(entries.map((e) => e.id)).toEqual(["A-2", "A-3", "A-1"]); + }); + + it("filters and sorts using valid nested field paths", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ id: "a", data: { project: { state: "open", priority: 2 } } }); + await handle.create({ id: "b", data: { project: { state: "done", priority: 1 } } }); + await handle.create({ id: "c", data: { project: { state: "open", priority: 3 } } }); + + const { entries, total } = await handle.list({ + where: { "project.state": "open" }, + sort: "-project.priority", + }); + + expect(total).toBe(2); + expect(entries.map((entry) => entry.id)).toEqual(["c", "a"]); + expect(await handle.count({ "project.state": "open" })).toBe(2); + }); + + it("rejects malformed where and sort field paths", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); + + await expect(handle.list({ where: { "status') OR 1=1 --": "open" } })).rejects.toThrow( + "Invalid collection field path", + ); + await expect(handle.list({ sort: "status; DELETE FROM docs" })).rejects.toThrow( + "Invalid collection field path", + ); + await expect(handle.count({ "project..status": "open" })).rejects.toThrow( + "Invalid collection field path", + ); + }); + + it("searches entries via FTS", async () => { + await handle.create({ + data: { ticket: "A-1", summary: "Payment retry", status: "open" }, + body: "The payment retry logic has a bug in the exponential backoff.", + }); + await handle.create({ + data: { ticket: "A-2", summary: "UI alignment", status: "open" }, + body: "The button is misaligned on mobile.", + }); + + const { entries } = await handle.search("payment retry"); + expect(entries.length).toBeGreaterThanOrEqual(1); + expect(entries[0]!.id).toBe("A-1"); + expect(entries[0]!.snippet).toBeTruthy(); + }); + + it("counts entries", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); + await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); + + expect(await handle.count()).toBe(2); + expect(await handle.count({ status: "open" })).toBe(1); + }); + + it("getCollection with filter", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); + await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); + + const all = await handle.getCollection(); + expect(all.length).toBe(2); + + const open = await handle.getCollection((e) => e.data.status === "open"); + expect(open.length).toBe(1); + expect(open[0]!.id).toBe("A-1"); + }); + + it("reconciles hand-edited files on next init", async () => { + await handle.create({ + data: { ticket: "A-1", summary: "Original", status: "open" }, + body: "Original body", + }); + handle.close(); + + const filePath = join(dir, "A-1.md"); + await writeFile( + filePath, + "---\nticket: A-1\nsummary: Hand-edited\nstatus: done\n---\n\nEdited by hand\n", + ); + + handle = await makeCollectionHandle( + "test-collection", + dir, + { schema, generateId: (data: any) => data.ticket }, + makeMemoryIndex(), + ); + await handle.count(); + + const entry = await handle.getEntry("A-1"); + expect(entry!.data.summary).toBe("Hand-edited"); + expect(entry!.data.status).toBe("done"); + expect(entry!.body).toBe("Edited by hand\n"); + }); + + it("clears all entries", async () => { + await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); + await handle.create({ data: { ticket: "A-2", summary: "Two", status: "open" } }); + + await handle.clear(); + expect(await handle.count()).toBe(0); + expect(await handle.getEntry("A-1")).toBeNull(); + }); +}); + +describe("CollectionHandle edge cases", () => { + let dir: string; + let handle: ClosableCollectionHandle; + + beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), "rig-collection-edge-")); + }); + + afterEach(async () => { + handle?.close(); + await rm(dir, { recursive: true, force: true }); + }); + + it("derives id from common fields when no generateId", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + const entry = await handle.create({ + data: { title: "My Great Note", content: "hello" }, + }); + expect(entry.id).toBe("my-great-note"); + }); + + it("derives id from slug field", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + const entry = await handle.create({ + data: { slug: "custom-slug", content: "hello" }, + }); + expect(entry.id).toBe("custom-slug"); + }); + + it("derives id from id field", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + const entry = await handle.create({ + data: { id: "my-id", content: "hello" }, + }); + expect(entry.id).toBe("my-id"); + }); + + it("throws when no id can be derived", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await expect(handle.create({ data: { count: 42 } })).rejects.toThrow("needs an id"); + }); + + it("throws on update of non-existent entry", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await expect(handle.update("ghost", { data: { x: 1 } })).rejects.toThrow("not found"); + }); + + it("handles complex frontmatter with nested objects and arrays", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ + id: "complex", + data: { + title: "Complex entry", + tags: ["a", "b", "c"], + prs: [ + { number: 123, url: "https://example.com", state: "OPEN" }, + { number: 456, url: "https://example.com/2", state: "MERGED" }, + ], + nested: { foo: "bar", baz: 42 }, + empty_arr: [], + flag: true, + nothing: null, + priority: 3, + }, + body: "## Content\nWith special chars: {foo} [bar] *baz*", + }); + + const fetched = await handle.getEntry("complex"); + expect(fetched!.data.title).toBe("Complex entry"); + expect(fetched!.data.tags).toEqual(["a", "b", "c"]); + expect(fetched!.data.prs).toHaveLength(2); + expect(fetched!.data.prs[0].number).toBe(123); + expect(fetched!.data.nested.foo).toBe("bar"); + expect(fetched!.data.empty_arr).toEqual([]); + expect(fetched!.data.flag).toBe(true); + expect(fetched!.data.nothing).toBeNull(); + expect(fetched!.data.priority).toBe(3); + }); + + it("handles frontmatter with quoted strings and special values", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ + id: "special", + data: { + url: "https://example.com/path?q=1&b=2", + description: "Has: colon in value", + }, + body: "", + }); + const fetched = await handle.getEntry("special"); + expect(fetched!.data.url).toBe("https://example.com/path?q=1&b=2"); + expect(fetched!.data.description).toBe("Has: colon in value"); + }); + + it("update with only body preserves data", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ id: "note1", data: { title: "Original" }, body: "old" }); + const updated = await handle.update("note1", { body: "new body" }); + expect(updated.data.title).toBe("Original"); + expect(updated.body).toBe("new body"); + }); + + it("list with offset and descending sort", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ id: "a", data: { title: "A", rank: 1 } }); + await handle.create({ id: "b", data: { title: "B", rank: 2 } }); + await handle.create({ id: "c", data: { title: "C", rank: 3 } }); + + const { entries } = await handle.list({ sort: "-rank", limit: 2, offset: 1 }); + expect(entries.map((e) => e.id)).toEqual(["b", "a"]); + }); + + it("search returns fallback snippet when no line matches", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ + id: "note1", + data: { title: "searchable" }, + body: "This body has no matching words for the search term.", + }); + // The memory index matches on title in data_json + const { entries } = await handle.search("searchable"); + expect(entries.length).toBe(1); + expect(entries[0]!.snippet).toBeTruthy(); + }); + + it("reconcile removes index entries for deleted files", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + await handle.create({ id: "keep", data: { title: "Keep" } }); + await handle.create({ id: "delete-me", data: { title: "Delete" } }); + handle.close(); + + // Delete the file manually + const { unlink: unlinkFile } = await import("node:fs/promises"); + await unlinkFile(join(dir, "delete-me.md")); + + // Re-init with fresh index + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + + expect(await handle.getEntry("keep")).not.toBeNull(); + // delete-me.md is gone, so getEntry reads from disk and fails + expect(await handle.getEntry("delete-me")).toBeNull(); + }); + + it("handles content without frontmatter", async () => { + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + + // Write a raw file without frontmatter + await writeFile(join(dir, "raw.md"), "Just plain content\n"); + + // Re-init to trigger reconcile + handle.close(); + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + + const entry = await handle.getEntry("raw"); + expect(entry!.data).toEqual({}); + expect(entry!.body).toBe("Just plain content\n"); + }); + + it("uses the fingerprint fast path for unchanged files", async () => { + const index = new TrackingMemoryCollectionIndex(); + handle = await makeCollectionHandle("notes", dir, {}, index); + await handle.count(); + await handle.create({ id: "stable", data: { title: "Stable" }, body: "unchanged" }); + expect(index.upsertCalls).toBe(1); + handle.close(); + + handle = await makeCollectionHandle("notes", dir, {}, index); + await handle.count(); + + expect(index.upsertCalls).toBe(1); + expect((await handle.list()).entries).toHaveLength(1); + }); +}); + +describe("CollectionHandle (schema-less)", () => { + let dir: string; + let handle: ClosableCollectionHandle; + + beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), "rig-collection-schemaless-")); + handle = await makeCollectionHandle("notes", dir, {}); + await handle.count(); + }); + + afterEach(async () => { + handle.close(); + await rm(dir, { recursive: true, force: true }); + }); + + it("creates and reads without schema validation", async () => { + const entry = await handle.create({ + id: "my-note", + data: { anything: "goes", nested: { foo: "bar" } }, + body: "Free-form content", + }); + + expect(entry.id).toBe("my-note"); + const fetched = await handle.getEntry("my-note"); + expect(fetched!.data.anything).toBe("goes"); + }); + + it("searches schema-less entries", async () => { + await handle.create({ + id: "note-1", + data: { topic: "deployment" }, + body: "We deployed the new service to production today.", + }); + + const { entries } = await handle.search("deployed production"); + expect(entries.length).toBe(1); + }); +}); diff --git a/src/domains/collections/runtime/collection-memory-index.test.ts b/src/domains/collections/runtime/collection-memory-index.test.ts new file mode 100644 index 0000000..aa115a8 --- /dev/null +++ b/src/domains/collections/runtime/collection-memory-index.test.ts @@ -0,0 +1,112 @@ +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it } from "vite-plus/test"; +import { Effect } from "effect"; +import type { CollectionIndexInterface } from "../types/tool-collection"; +import { + CollectionHandleFactoryService, + collectionHandleFactoryLayer, +} from "./collection-handle-factory"; +import { MemoryCollectionIndexService } from "./fixtures/memory-collection-index"; + +const makeMemoryIndex = (): CollectionIndexInterface => + Effect.runSync( + MemoryCollectionIndexService.use((service) => service.create).pipe( + Effect.provide(MemoryCollectionIndexService.layer), + ), + ); + +describe("collection memory index", () => { + it("filters, sorts, and searches rows across fallback values", () => { + const index = makeMemoryIndex(); + index.upsertDoc( + { + id: "two", + data: { meta: { state: { open: false } } }, + body: "alpha", + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + }, + 1, + ); + index.upsertDoc( + { + id: "one", + data: { meta: { state: { open: true } }, rank: 1 }, + body: "alpha beta", + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-02T00:00:00.000Z", + }, + 1, + ); + index.upsertDoc( + { + id: "three", + data: { meta: { state: { open: true } }, rank: 1 }, + body: "gamma", + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-03T00:00:00.000Z", + }, + 1, + ); + + expect(index.listDocs({ where: { "meta.state": { open: true } } }).total).toBe(2); + expect(index.listDocs({ sort: "rank" }).rows.map((row) => row.id)).toEqual([ + "two", + "one", + "three", + ]); + expect(index.listDocs({ sort: "-rank" }).rows.map((row) => row.id)).toEqual([ + "one", + "three", + "two", + ]); + expect(index.listDocs({}).rows.map((row) => row.id)).toEqual(["three", "one", "two"]); + expect(index.searchDocs("alpha beta", 5).map((row) => row.id)).toEqual(["one", "two"]); + + const reverseIndex = makeMemoryIndex(); + reverseIndex.upsertDoc( + { + id: "ranked", + data: { rank: 1 }, + body: "", + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-02T00:00:00.000Z", + }, + 1, + ); + reverseIndex.upsertDoc( + { + id: "missing", + data: {}, + body: "", + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + }, + 1, + ); + expect(reverseIndex.listDocs({ sort: "rank" }).total).toBe(2); + expect(reverseIndex.listDocs({}).rows.map((row) => row.id)).toEqual(["ranked", "missing"]); + }); + + it("falls back to filesystem timestamps when an entry is absent from the index", async () => { + const dir = await mkdtemp(join(tmpdir(), "rig-collection-fallback-")); + const index = makeMemoryIndex(); + const handle = await Effect.runPromise( + CollectionHandleFactoryService.use((service) => + service.createLazy("notes", dir, {}, index), + ).pipe(Effect.provide(collectionHandleFactoryLayer)), + ); + try { + await writeFile(join(dir, "manual.md"), "---\ntitle: Manual\n---\nBody", "utf8"); + await handle.count(); + index.deleteDoc("manual"); + const entry = await handle.getEntry("manual"); + expect(entry?.id).toBe("manual"); + } finally { + handle.close(); + await rm(dir, { recursive: true, force: true }); + } + }); +}); diff --git a/src/domains/collections/runtime/fixtures/memory-collection-index.ts b/src/domains/collections/runtime/fixtures/memory-collection-index.ts new file mode 100644 index 0000000..94d99c0 --- /dev/null +++ b/src/domains/collections/runtime/fixtures/memory-collection-index.ts @@ -0,0 +1,140 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../../providers/errors/rig-error"; +import type { + CollectionEntry, + CollectionFileRecord, + CollectionIndexInterface, + DocRow, + ListOptions, +} from "../../types/tool-collection"; + +const compileFieldPath = (value: string) => { + const segments = value.split("."); + const hasInvalidSegment = segments.some((part) => !/^[A-Za-z_][A-Za-z0-9_-]*$/.test(part)); + const isInvalidPath = value.length === 0 || hasInvalidSegment; + if (isInvalidPath) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Invalid collection field path: ${value}`, + details: { expected: "dot-separated field names beginning with a letter or underscore" }, + }); + } + const read = (input: unknown) => { + let current = input; + for (const segment of segments) { + const isMissingSegment = + typeof current !== "object" || current === null || !(segment in current); + if (isMissingSegment) { + return undefined; + } + current = (current as Record)[segment]; + } + return current; + }; + return { read } as const; +}; + +const createListDocs = (docs: ReadonlyMap) => (options: ListOptions) => { + let rows = [...docs.values()]; + const filters = Object.entries(options.where ?? {}).map(([path, value]) => ({ + field: compileFieldPath(path), + value, + })); + if (filters.length > 0) { + rows = rows.filter((row) => { + const data: unknown = JSON.parse(row.data_json); + return filters.every(({ field, value }) => { + const actual = field.read(data); + return typeof value === "object" && value !== null + ? JSON.stringify(actual) === JSON.stringify(value) + : actual === value; + }); + }); + } + const total = rows.length; + if (options.sort) { + const descending = options.sort.startsWith("-"); + const field = compileFieldPath(descending ? options.sort.slice(1) : options.sort); + rows.sort((left, right) => { + const leftValue = field.read(JSON.parse(left.data_json)) ?? ""; + const rightValue = field.read(JSON.parse(right.data_json)) ?? ""; + const comparison = leftValue < rightValue ? -1 : leftValue > rightValue ? 1 : 0; + return descending ? -comparison : comparison; + }); + } else { + rows.sort((left, right) => (right.updated_at > left.updated_at ? 1 : -1)); + } + const offset = options.offset ?? 0; + const limit = options.limit ?? 50; + return { rows: rows.slice(offset, offset + limit), total }; +}; + +const createSearchDocs = (docs: ReadonlyMap) => (query: string, limit: number) => { + const words = query.toLowerCase().split(/\s+/); + const scored: { row: DocRow; rank: number }[] = []; + for (const row of docs.values()) { + const text = `${row.id} ${row.data_json} ${row.body}`.toLowerCase(); + const matches = words.filter((word) => text.includes(word)).length; + if (matches > 0) { + scored.push({ row: { ...row, rank: -matches } as DocRow & { rank: number }, rank: -matches }); + } + } + scored.sort((left, right) => left.rank - right.rank); + return scored.slice(0, limit).map((entry) => entry.row); +}; + +const createMemoryCollectionIndex = (): CollectionIndexInterface => { + const docs = new Map(); + const files = new Map(); + const listDocs = createListDocs(docs); + const upsertDoc = (entry: CollectionEntry>, fileMtime: number) => { + docs.set(entry.id, { + id: entry.id, + data_json: JSON.stringify(entry.data), + body: entry.body, + created_at: entry.createdAt, + updated_at: entry.updatedAt, + file_mtime: fileMtime, + }); + }; + return { + open: async () => undefined, + upsertDoc, + deleteDoc: (id) => void docs.delete(id), + upsertFile: (record) => void files.set(record.id, record), + deleteFile: (id) => void files.delete(id), + getFile: (id) => files.get(id) ?? null, + allFileIds: () => [...files.keys()], + getDoc: (id) => docs.get(id) ?? null, + listDocs, + searchDocs: createSearchDocs(docs), + countDocs: (where) => + !where || Object.keys(where).length === 0 ? docs.size : listDocs({ where }).total, + allIds: () => [...docs.keys()], + clearAll: () => { + docs.clear(); + files.clear(); + }, + close: () => undefined, + }; +}; + +export class MemoryCollectionIndexService extends Context.Service< + MemoryCollectionIndexService, + { + readonly create: Effect.Effect; + } +>()("@rendotdev/rig/collections/MemoryCollectionIndexService", { + make: Effect.gen(function* () { + const create = Effect.sync(createMemoryCollectionIndex).pipe( + Effect.withSpan("MemoryCollectionIndexService.create"), + ); + + return { create } as const; + }), +}) { + static readonly layer = Layer.effect( + MemoryCollectionIndexService, + MemoryCollectionIndexService.make, + ); +} diff --git a/src/domains/collections/runtime/sqlite-collection-handle.test.ts b/src/domains/collections/runtime/sqlite-collection-handle.test.ts new file mode 100644 index 0000000..2f7f425 --- /dev/null +++ b/src/domains/collections/runtime/sqlite-collection-handle.test.ts @@ -0,0 +1,259 @@ +import { spawnSync } from "node:child_process"; +import { randomUUID } from "node:crypto"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { describe, expect, it } from "vite-plus/test"; + +class BunCollectionIntegrationRunner { + async run(collectionPath: string, body: string): Promise> { + const scriptPath = join(collectionPath, `integration-${randomUUID()}.ts`); + const collectionFactoryModule = pathToFileURL( + join(process.cwd(), "src/domains/collections/runtime/collection-handle-factory.ts"), + ).href; + const effectModule = pathToFileURL( + join(process.cwd(), "node_modules/effect/dist/index.js"), + ).href; + const zodModule = pathToFileURL(join(process.cwd(), "node_modules/zod/index.js")).href; + await writeFile( + scriptPath, + `import { Database } from "bun:sqlite"; +import { stat, unlink, utimes, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { Effect } from ${JSON.stringify(effectModule)}; +import { CollectionHandleFactoryService, collectionHandleFactoryLayer } from ${JSON.stringify(collectionFactoryModule)}; +import { z } from ${JSON.stringify(zodModule)}; + +const dir = ${JSON.stringify(collectionPath)}; +const makeHandle = (name, path, definition = {}) => Effect.runPromise( + CollectionHandleFactoryService.use((service) => service.createLazy(name, path, definition)).pipe( + Effect.provide(collectionHandleFactoryLayer), + ), +); +${body} +`, + "utf8", + ); + const result = spawnSync("bun", [scriptPath], { + cwd: process.cwd(), + encoding: "utf8", + env: { ...process.env, RIG_LOG: "0" }, + }); + if (result.status !== 0) throw new Error(result.stderr || result.stdout); + return JSON.parse(result.stdout.trim()) as Record; + } +} + +describe("CollectionHandle real Bun SQLite reconciliation", () => { + const runner = new BunCollectionIntegrationRunner(); + + it("filters and sorts nested paths through SQLite without interpolating unsafe paths", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-query-")); + try { + const result = await runner.run( + integrationDir, + `const handle = await makeHandle("notes", dir, {}); +await handle.count(); +await handle.create({ id: "a", data: { project: { state: "open", priority: 2 } } }); +await handle.create({ id: "b", data: { project: { state: "done", priority: 1 } } }); +await handle.create({ id: "c", data: { project: { state: "open", priority: 3 } } }); +const listed = await handle.list({ where: { "project.state": "open" }, sort: "-project.priority" }); +const count = await handle.count({ "project.state": "open" }); +let invalidMessage = ""; +try { + await handle.list({ where: { "state') OR 1=1 --": "open" } }); +} catch (error) { + invalidMessage = error instanceof Error ? error.message : String(error); +} +handle.close(); +console.log(JSON.stringify({ ids: listed.entries.map((entry) => entry.id), count, invalidMessage }));`, + ); + + expect(result).toEqual({ + ids: ["c", "a"], + count: 2, + invalidMessage: "Invalid collection field path: state') OR 1=1 --", + }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); + + it("detects same-size hand edits even when mtime is restored", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-edit-")); + try { + const result = await runner.run( + integrationDir, + `const filePath = join(dir, "note.md"); +const original = "---\\ntitle: one\\n---\\n\\nbody one\\n"; +const edited = "---\\ntitle: two\\n---\\n\\nbody two\\n"; +await writeFile(filePath, original); +const fixedTime = new Date(Math.floor(Date.now() / 1000) * 1000 - 10_000); +await utimes(filePath, fixedTime, fixedTime); +let handle = await makeHandle("notes", dir, {}); +await handle.count(); +handle.close(); +const before = await stat(filePath); +await Bun.sleep(10); +await writeFile(filePath, edited); +await utimes(filePath, before.atime, before.mtime); +const after = await stat(filePath); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +const listed = await handle.list(); +handle.close(); +console.log(JSON.stringify({ entry: listed.entries[0], sameMtime: before.mtimeMs === after.mtimeMs, sameSize: before.size === after.size, ctimeChanged: before.ctimeMs !== after.ctimeMs }));`, + ); + + expect(result).toMatchObject({ + entry: { data: { title: "two" }, body: "body two\n" }, + sameMtime: true, + sameSize: true, + ctimeChanged: true, + }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); + + it("detects hand-added and hand-deleted files across reopen", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-files-")); + try { + const result = await runner.run( + integrationDir, + `let handle = await makeHandle("notes", dir, {}); +await handle.count(); +handle.close(); +const filePath = join(dir, "added.md"); +await writeFile(filePath, "---\\ntitle: Added\\n---\\n\\nhello\\n"); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +const added = await handle.list(); +handle.close(); +await unlink(filePath); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +const deleted = await handle.list(); +handle.close(); +console.log(JSON.stringify({ added: added.entries.map((entry) => entry.id), afterDelete: deleted.entries.map((entry) => entry.id) }));`, + ); + + expect(result).toEqual({ added: ["added"], afterDelete: [] }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); + + it("rebuilds corrupt and incompatible derived indexes from Markdown", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-recovery-")); + try { + const result = await runner.run( + integrationDir, + `const indexPath = join(dir, ".index.sqlite"); +let handle = await makeHandle("notes", dir, {}); +await handle.count(); +await handle.create({ id: "durable", data: { title: "Markdown survives" }, body: "source of truth" }); +handle.close(); + +await writeFile(indexPath, "corrupted sqlite\\n"); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +const afterCorruption = await handle.list(); +handle.close(); + +await unlink(indexPath); +const incompatible = new Database(indexPath, { create: true, strict: true }); +incompatible.run("CREATE TABLE docs (id TEXT PRIMARY KEY)"); +incompatible.close(false); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +const afterIncompatibleSchema = await handle.list(); +handle.close(); + +const rebuilt = new Database(indexPath, { strict: true }); +const integrity = rebuilt.query("PRAGMA quick_check").get(); +rebuilt.close(false); +console.log(JSON.stringify({ afterCorruption, afterIncompatibleSchema, integrity }));`, + ); + + expect(result).toMatchObject({ + afterCorruption: { + entries: [{ id: "durable", data: { title: "Markdown survives" } }], + total: 1, + }, + afterIncompatibleSchema: { + entries: [{ id: "durable", data: { title: "Markdown survives" } }], + total: 1, + }, + integrity: { quick_check: "ok" }, + }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); + + it("removes malformed files from the index and fingerprints them as invalid", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-invalid-")); + try { + const result = await runner.run( + integrationDir, + `const definition = { schema: z.object({ title: z.string() }) }; +let handle = await makeHandle("notes", dir, definition); +await handle.count(); +await handle.create({ id: "note", data: { title: "Valid" } }); +handle.close(); +await writeFile(join(dir, "note.md"), "---\\ntitle: 42\\n---\\n\\ninvalid\\n"); +handle = await makeHandle("notes", dir, definition); +await handle.count(); +const malformed = await handle.list(); +handle.close(); +const db = new Database(join(dir, ".index.sqlite"), { strict: true }); +const record = db.query("SELECT status, size FROM collection_files WHERE id = ?").get("note"); +db.close(false); +handle = await makeHandle("notes", dir, definition); +await handle.count(); +const unchanged = await handle.list(); +handle.close(); +console.log(JSON.stringify({ malformedTotal: malformed.total, unchangedTotal: unchanged.total, record }));`, + ); + + expect(result).toMatchObject({ + malformedTotal: 0, + unchangedTotal: 0, + record: { status: "invalid" }, + }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); + + it("preserves persisted fingerprints on an unchanged reopen", async () => { + const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-fast-")); + try { + const result = await runner.run( + integrationDir, + `const filePath = join(dir, "stable.md"); +await writeFile(filePath, "---\\ntitle: Stable\\n---\\n\\nunchanged\\n"); +let handle = await makeHandle("notes", dir, {}); +await handle.count(); +handle.close(); +let db = new Database(join(dir, ".index.sqlite"), { strict: true }); +const before = db.query("SELECT * FROM collection_files WHERE id = ?").get("stable"); +db.close(false); +handle = await makeHandle("notes", dir, {}); +await handle.count(); +handle.close(); +db = new Database(join(dir, ".index.sqlite"), { strict: true }); +const after = db.query("SELECT * FROM collection_files WHERE id = ?").get("stable"); +db.close(false); +console.log(JSON.stringify({ before, after }));`, + ); + + expect(result.before).toEqual(result.after); + expect(result.after).toMatchObject({ status: "indexed" }); + } finally { + await rm(integrationDir, { recursive: true, force: true }); + } + }); +}); diff --git a/src/domains/collections/service/collection-operation-layer.ts b/src/domains/collections/service/collection-operation-layer.ts new file mode 100644 index 0000000..91332d5 --- /dev/null +++ b/src/domains/collections/service/collection-operation-layer.ts @@ -0,0 +1,201 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { CollectionDocumentPathService } from "../repo/collection-document-path"; +import { + CollectionDocumentIoService, + CollectionDocumentRepositoryService, +} from "../repo/collection-document-repository"; +import { CollectionEntryCodecService } from "../repo/collection-entry-codec"; +import { CollectionQueryService } from "../repo/collection-query"; +import { CollectionReconciliationService } from "../repo/collection-reconciliation"; +import { frontmatterCodecLayer } from "../repo/frontmatter-codec"; +import { + SqliteCollectionIndexService, + sqliteCollectionIndexLayer, +} from "../repo/sqlite/sqlite-collection-index"; +import type { + CollectionData, + CollectionOperationContext, +} from "../types/collection-operation-context"; +import type { + CollectionEntry, + CollectionIndexInterface, + ListOptions, + SearchResult, +} from "../types/tool-collection"; + +export class CollectionOperationService extends Context.Service< + CollectionOperationService, + { + readonly createIndex: ( + collectionPath: string, + ) => Effect.Effect; + readonly reconcile: (context: CollectionOperationContext) => Effect.Effect; + readonly create: ( + context: CollectionOperationContext, + input: { id?: string; data: CollectionData; body?: string }, + ) => Effect.Effect, RigError>; + readonly getEntry: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect | null, RigError>; + readonly update: ( + context: CollectionOperationContext, + id: string, + patch: { data?: CollectionData; body?: string }, + ) => Effect.Effect, RigError>; + readonly upsert: ( + context: CollectionOperationContext, + input: { id: string; data: CollectionData; body?: string }, + ) => Effect.Effect<{ id: string; created: boolean }, RigError>; + readonly remove: ( + context: CollectionOperationContext, + id: string, + ) => Effect.Effect; + readonly clear: (context: CollectionOperationContext) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionOperationService", { + make: Effect.gen(function* () { + const indexes = yield* SqliteCollectionIndexService; + const documents = yield* CollectionDocumentRepositoryService; + const reconciliation = yield* CollectionReconciliationService; + const createIndex = Effect.fn("CollectionOperationService.createIndex")(function* ( + collectionPath: string, + ) { + return yield* indexes.create(collectionPath); + }); + const reconcile = Effect.fn("CollectionOperationService.reconcile")(function* ( + context: CollectionOperationContext, + ) { + yield* reconciliation.reconcile(context); + }); + const create = Effect.fn("CollectionOperationService.create")(function* ( + context: CollectionOperationContext, + input: { id?: string; data: CollectionData; body?: string }, + ) { + return yield* documents.create(context, input); + }); + const getEntry = Effect.fn("CollectionOperationService.getEntry")(function* ( + context: CollectionOperationContext, + id: string, + ) { + return yield* documents.getEntry(context, id); + }); + const update = Effect.fn("CollectionOperationService.update")(function* ( + context: CollectionOperationContext, + id: string, + patch: { data?: CollectionData; body?: string }, + ) { + return yield* documents.update(context, id, patch); + }); + const upsert = Effect.fn("CollectionOperationService.upsert")(function* ( + context: CollectionOperationContext, + input: { id: string; data: CollectionData; body?: string }, + ) { + return yield* documents.upsert(context, input); + }); + const remove = Effect.fn("CollectionOperationService.remove")(function* ( + context: CollectionOperationContext, + id: string, + ) { + return yield* documents.remove(context, id); + }); + const clear = Effect.fn("CollectionOperationService.clear")(function* ( + context: CollectionOperationContext, + ) { + yield* documents.clear(context); + }); + return { + createIndex, + reconcile, + create, + getEntry, + update, + upsert, + remove, + clear, + } as const; + }), +}) { + static readonly layer = Layer.effect(CollectionOperationService, CollectionOperationService.make); +} + +export class CollectionReadService extends Context.Service< + CollectionReadService, + { + readonly list: ( + context: CollectionOperationContext, + options?: ListOptions, + ) => Effect.Effect<{ entries: CollectionEntry[]; total: number }>; + readonly search: ( + context: CollectionOperationContext, + query: string, + options?: { limit?: number }, + ) => Effect.Effect<{ entries: SearchResult[] }>; + readonly count: ( + context: CollectionOperationContext, + where?: Record, + ) => Effect.Effect; + readonly getCollection: ( + context: CollectionOperationContext, + filter?: (entry: CollectionEntry) => boolean, + ) => Effect.Effect[]>; + } +>()("@rendotdev/rig/collections/CollectionReadService", { + make: Effect.gen(function* () { + const queries = yield* CollectionQueryService; + const list = Effect.fn("CollectionReadService.list")(function* ( + context: CollectionOperationContext, + options?: ListOptions, + ) { + return yield* queries.list(context, options); + }); + const search = Effect.fn("CollectionReadService.search")(function* ( + context: CollectionOperationContext, + query: string, + options?: { limit?: number }, + ) { + return yield* queries.search(context, query, options); + }); + const count = Effect.fn("CollectionReadService.count")(function* ( + context: CollectionOperationContext, + where?: Record, + ) { + return yield* queries.count(context, where); + }); + const getCollection = Effect.fn("CollectionReadService.getCollection")(function* ( + context: CollectionOperationContext, + filter?: (entry: CollectionEntry) => boolean, + ) { + return yield* queries.getCollection(context, filter); + }); + return { list, search, count, getCollection } as const; + }), +}) { + static readonly layer = Layer.effect(CollectionReadService, CollectionReadService.make); +} + +const collectionEntryLayer = CollectionEntryCodecService.layer; +const collectionPathLayer = CollectionDocumentPathService.layer; +const collectionDocumentIoLayer = CollectionDocumentIoService.layer.pipe( + Layer.provide(Layer.mergeAll(collectionEntryLayer, collectionPathLayer, frontmatterCodecLayer)), +); +const collectionReconciliationLayer = CollectionReconciliationService.layer.pipe( + Layer.provide(Layer.mergeAll(collectionEntryLayer, collectionPathLayer, frontmatterCodecLayer)), +); +const collectionDocumentRepositoryLayer = CollectionDocumentRepositoryService.layer.pipe( + Layer.provide( + Layer.mergeAll(collectionDocumentIoLayer, collectionEntryLayer, collectionPathLayer), + ), +); +const collectionOperationRepositoryLayer = Layer.mergeAll( + sqliteCollectionIndexLayer, + collectionDocumentRepositoryLayer, + collectionReconciliationLayer, + CollectionQueryService.layer.pipe(Layer.provide(collectionEntryLayer)), +); + +export const collectionOperationLayer = CollectionOperationService.layer.pipe( + Layer.provide(collectionOperationRepositoryLayer), + Layer.merge(CollectionReadService.layer.pipe(Layer.provide(collectionOperationRepositoryLayer))), +); diff --git a/src/tools/collection-field-path.test.ts b/src/domains/collections/service/field-path.test.ts similarity index 52% rename from src/tools/collection-field-path.test.ts rename to src/domains/collections/service/field-path.test.ts index da2ba75..359f58f 100644 --- a/src/tools/collection-field-path.test.ts +++ b/src/domains/collections/service/field-path.test.ts @@ -1,28 +1,24 @@ import { describe, expect, it } from "vite-plus/test"; -import { - CollectionFieldPathCompilerClass, - CollectionFieldPathSingleton, -} from "./collection-field-path"; +import { Effect } from "effect"; +import { CollectionFieldPathService } from "./field-path"; describe("collection field paths", () => { - const Compiler = CollectionFieldPathSingleton; + const compile = (value: string) => + Effect.runSync( + CollectionFieldPathService.use((service) => service.compile(value)).pipe( + Effect.provide(CollectionFieldPathService.layer), + ), + ); it("compiles nested paths for SQLite and object traversal", () => { - const path = Compiler.compile({ value: "project.owner_id" }); + const path = compile("project.owner_id"); expect(path.segments).toEqual(["project", "owner_id"]); expect(path.sqliteJsonPath).toBe("$.project.owner_id"); expect(path.read({ project: { owner_id: "rene" } })).toBe("rene"); expect(path.read({ project: null })).toBeUndefined(); expect(path.read({ project: [] })).toBeUndefined(); - expect(CollectionFieldPathSingleton.compile({ value: "status" }).segments).toEqual(["status"]); - - const adapter = new CollectionFieldPathCompilerClass(); - expect(adapter).toBeInstanceOf(CollectionFieldPathCompilerClass); - expect(adapter.compile("project.owner_id")).toMatchObject({ - segments: ["project", "owner_id"], - sqliteJsonPath: "$.project.owner_id", - }); + expect(compile("status").segments).toEqual(["status"]); }); it.each([ @@ -37,6 +33,6 @@ describe("collection field paths", () => { "status value", "status/owner", ])("rejects malformed or injection-like paths: %s", (value) => { - expect(() => Compiler.compile({ value })).toThrow("Invalid collection field path"); + expect(() => compile(value)).toThrow("Invalid collection field path"); }); }); diff --git a/src/domains/collections/service/field-path.ts b/src/domains/collections/service/field-path.ts new file mode 100644 index 0000000..d830940 --- /dev/null +++ b/src/domains/collections/service/field-path.ts @@ -0,0 +1,52 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; + +export type CompiledCollectionFieldPath = { + readonly segments: readonly string[]; + readonly sqliteJsonPath: string; + readonly read: (value: unknown) => unknown; +}; + +export class CollectionFieldPathService extends Context.Service< + CollectionFieldPathService, + { + readonly compile: (value: string) => Effect.Effect; + } +>()("@rendotdev/rig/collections/CollectionFieldPathService", { + make: Effect.gen(function* () { + const invalid = (value: unknown) => + new RigError({ + code: "INPUT_ERROR", + message: `Invalid collection field path: ${String(value)}`, + details: { + expected: "dot-separated field names beginning with a letter or underscore", + }, + }); + const compile = Effect.fn("CollectionFieldPathService.compile")(function* (value: string) { + const isMissingPath = typeof value !== "string" || value.length === 0; + if (isMissingPath) { + return yield* invalid(value); + } + const segments = value.split("."); + if (segments.some((segment) => !/^[A-Za-z_][A-Za-z0-9_-]*$/.test(segment))) { + return yield* invalid(value); + } + const read = (input: unknown) => { + let current = input; + for (const segment of segments) { + if (!Predicate.isObject(current)) return undefined; + current = current[segment]; + } + return current; + }; + return { + segments, + sqliteJsonPath: `$${segments.map((segment) => `.${segment}`).join("")}`, + read, + }; + }); + return { compile } as const; + }), +}) { + static readonly layer = Layer.effect(CollectionFieldPathService, CollectionFieldPathService.make); +} diff --git a/src/domains/collections/types/collection-operation-context.ts b/src/domains/collections/types/collection-operation-context.ts new file mode 100644 index 0000000..6d398f0 --- /dev/null +++ b/src/domains/collections/types/collection-operation-context.ts @@ -0,0 +1,9 @@ +import type { CollectionDefinition, CollectionIndexInterface } from "./tool-collection"; + +export type CollectionOperationContext = Readonly<{ + path: string; + definition: CollectionDefinition; + index: CollectionIndexInterface; +}>; + +export type CollectionData = Record; diff --git a/src/domains/collections/types/tool-collection.ts b/src/domains/collections/types/tool-collection.ts new file mode 100644 index 0000000..d358ca0 --- /dev/null +++ b/src/domains/collections/types/tool-collection.ts @@ -0,0 +1,79 @@ +import type { z } from "zod"; + +export type CollectionDefinition = z.ZodObject> = { + schema?: T; + generateId?: (data: z.input) => string; +}; + +export type CollectionEntry> = { + id: string; + data: T; + body: string; + createdAt: string; + updatedAt: string; +}; + +export type SearchResult> = CollectionEntry & { + snippet: string; + rank: number; +}; + +export type ListOptions = { + where?: Record; + sort?: string; + limit?: number; + offset?: number; +}; + +export type CollectionHandle> = { + readonly name: string; + readonly path: string; + create(entry: { id?: string; data: T; body?: string }): Promise>; + getEntry(id: string): Promise | null>; + update(id: string, patch: { data?: Partial; body?: string }): Promise>; + upsert(entry: { id: string; data: T; body?: string }): Promise<{ id: string; created: boolean }>; + remove(id: string): Promise; + list(options?: ListOptions): Promise<{ entries: CollectionEntry[]; total: number }>; + search(query: string, options?: { limit?: number }): Promise<{ entries: SearchResult[] }>; + count(where?: Record): Promise; + getCollection(filter?: (entry: CollectionEntry) => boolean): Promise[]>; + clear(): Promise; +}; + +export type DocRow = { + id: string; + data_json: string; + body: string; + created_at: string; + updated_at: string; + file_mtime: number; +}; + +export type CollectionFileMetadata = { mtimeMs: number; ctimeMs: number; size: number }; + +export type CollectionFileRecord = CollectionFileMetadata & { + id: string; + status: "indexed" | "invalid"; +}; + +export interface CollectionIndexInterface { + open(): Promise; + upsertDoc(entry: CollectionEntry>, fileMtime: number): void; + deleteDoc(id: string): void; + upsertFile(record: CollectionFileRecord): void; + deleteFile(id: string): void; + getFile(id: string): CollectionFileRecord | null; + allFileIds(): string[]; + getDoc(id: string): DocRow | null; + listDocs(options: ListOptions): { rows: DocRow[]; total: number }; + searchDocs(query: string, limit: number): DocRow[]; + countDocs(where?: Record): number; + allIds(): string[]; + clearAll(): void; + close(): void; +} + +export type ManagedCollectionHandle = CollectionHandle & { + init(): Promise; + close(): void; +}; diff --git a/src/domains/registry/repo/registry-directory.ts b/src/domains/registry/repo/registry-directory.ts new file mode 100644 index 0000000..086433a --- /dev/null +++ b/src/domains/registry/repo/registry-directory.ts @@ -0,0 +1,30 @@ +import { mkdir } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; + +export class RegistryDirectoryRepositoryService extends Context.Service< + RegistryDirectoryRepositoryService, + { readonly ensure: (path: string) => Effect.Effect } +>()("@rendotdev/rig/registry/RegistryDirectoryRepositoryService", { + make: Effect.gen(function* () { + const ensure = Effect.fn("RegistryDirectoryRepositoryService.ensure")(function* (path: string) { + yield* Effect.tryPromise({ + try: () => mkdir(path, { recursive: true }), + catch: (cause) => + new RigError({ + code: "CONFIG_INVALID", + message: "Could not create the registry directory.", + details: { path, cause }, + }), + }); + }); + return { ensure } as const; + }), +}) { + static readonly layer = Layer.effect( + RegistryDirectoryRepositoryService, + RegistryDirectoryRepositoryService.make, + ); +} + +export const registryDirectoryRepositoryLayer = RegistryDirectoryRepositoryService.layer; diff --git a/src/domains/registry/repo/tool-discovery-repository.ts b/src/domains/registry/repo/tool-discovery-repository.ts new file mode 100644 index 0000000..3d6cb45 --- /dev/null +++ b/src/domains/registry/repo/tool-discovery-repository.ts @@ -0,0 +1,94 @@ +import { existsSync, statSync } from "node:fs"; +import { readdir } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import type { RegistryEntry } from "../types/registry"; +import type { DiscoveredTool } from "../types/tool-discovery"; +import { ToolEntryRepositoryService } from "./tool-entry-repository"; + +export class ToolDiscoveryRepositoryService extends Context.Service< + ToolDiscoveryRepositoryService, + { + readonly projectRootFor: (pathValue: string) => Effect.Effect; + readonly discoverRegistry: (entry: RegistryEntry) => Effect.Effect; + readonly inspect: ( + entry: RegistryEntry, + name: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/registry/ToolDiscoveryRepositoryService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsService; + const entries = yield* ToolEntryRepositoryService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const visibilityStartDirectory = (pathValue: string): string => { + try { + /* v8 ignore else -- file inputs are handled by the containing-directory fallback */ + if (statSync(pathValue).isDirectory()) return pathValue; + } catch { + // Missing instruction files still scope to their parent directory. + } + /* v8 ignore next -- file inputs use their containing directory */ + return dirname(pathValue); + }; + const discoverRegistry = Effect.fn("ToolDiscoveryRepositoryService.discoverRegistry")( + function* (entry: RegistryEntry) { + if (!existsSync(entry.path)) return []; + const children = yield* Effect.tryPromise({ + try: () => readdir(entry.path, { withFileTypes: true }), + catch: toError, + }); + const discovered = yield* Effect.all( + children.map((child) => + child.isDirectory() ? entries.inspect(entry, child.name) : Effect.void, + ), + ); + return discovered.filter((tool): tool is DiscoveredTool => tool !== undefined); + }, + ); + const projectRootFor = Effect.fn("ToolDiscoveryRepositoryService.projectRootFor")(function* ( + pathValue: string, + ) { + const absolute = yield* paths.resolve(pathValue); + let current = visibilityStartDirectory(absolute); + let packageRoot: string | undefined; + while (true) { + /* v8 ignore next -- repository-root discovery is covered by packaged integration */ + if (existsSync(join(current, ".git"))) return current; + /* v8 ignore next -- package-only workspaces are covered by packaged integration */ + const foundFirstPackageRoot = !packageRoot && existsSync(join(current, "package.json")); + /* v8 ignore next -- package-only workspaces are covered by packaged integration */ + if (foundFirstPackageRoot) packageRoot = current; + const parent = dirname(current); + /* v8 ignore next -- filesystem-root fallback is defensive */ + if (parent === current) return packageRoot ?? visibilityStartDirectory(absolute); + /* v8 ignore next -- multi-level traversal is covered by packaged integration */ + current = parent; + } + }); + const inspect = Effect.fn("ToolDiscoveryRepositoryService.inspect")(function* ( + entry: RegistryEntry, + name: string, + ) { + return yield* entries.inspect(entry, name); + }); + return { projectRootFor, discoverRegistry, inspect } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolDiscoveryRepositoryService, + ToolDiscoveryRepositoryService.make, + ); +} + +export const toolDiscoveryRepositoryLayer = ToolDiscoveryRepositoryService.layer.pipe( + Layer.provide(ToolEntryRepositoryService.layer), +); diff --git a/src/domains/registry/repo/tool-entry-repository.ts b/src/domains/registry/repo/tool-entry-repository.ts new file mode 100644 index 0000000..ac5b43f --- /dev/null +++ b/src/domains/registry/repo/tool-entry-repository.ts @@ -0,0 +1,74 @@ +import { existsSync, lstatSync } from "node:fs"; +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigToolEntryFiles } from "../types/tool-discovery"; +import type { RegistryEntry } from "../types/registry"; +import type { DiscoveredTool } from "../types/tool-discovery"; + +export class ToolEntryRepositoryService extends Context.Service< + ToolEntryRepositoryService, + { + readonly inspect: ( + entry: RegistryEntry, + name: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/registry/ToolEntryRepositoryService", { + make: Effect.gen(function* () { + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const rejectLegacyEntry = (toolName: string, toolDir: string): void => { + const legacyPath = join(toolDir, "tool.ts"); + if (!existsSync(legacyPath)) return; + throw new RigError({ + code: "TOOL_INVALID", + message: `Tool ${toolName} must use index.rig.ts or index.rig.tsx.`, + details: { toolDir, found: legacyPath, expected: RigToolEntryFiles }, + }); + }; + const inspect = Effect.fn("ToolEntryRepositoryService.inspect")(function* ( + entry: RegistryEntry, + name: string, + ) { + return yield* Effect.try({ + try: () => { + const toolDir = join(entry.path, name); + try { + if (!lstatSync(toolDir).isDirectory()) return undefined; + } catch { + return undefined; + } + const toolPaths = RigToolEntryFiles.map((file) => join(toolDir, file)).filter(existsSync); + if (toolPaths.length > 1) { + throw new RigError({ + code: "TOOL_INVALID", + message: `Tool ${name} has multiple Rig entry files.`, + details: { toolDir, found: toolPaths, expected: RigToolEntryFiles }, + }); + } + if (toolPaths.length === 0) { + rejectLegacyEntry(name, toolDir); + return undefined; + } + return { + name, + registryKind: entry.kind, + registryPath: entry.path, + toolDir, + toolPath: toolPaths[0]!, + }; + }, + catch: toError, + }); + }); + return { inspect } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEntryRepositoryService, ToolEntryRepositoryService.make); +} diff --git a/src/domains/registry/service/registry-state.ts b/src/domains/registry/service/registry-state.ts new file mode 100644 index 0000000..53aef17 --- /dev/null +++ b/src/domains/registry/service/registry-state.ts @@ -0,0 +1,31 @@ +import { Context, Effect, Layer } from "effect"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import type { RegistryConfigInput, RegistryConfigState } from "../types/registry"; + +export class RegistryStateService extends Context.Service< + RegistryStateService, + { + readonly forConfig: (current: RegistryConfigInput) => Effect.Effect; + } +>()("@rendotdev/rig/registry/RegistryStateService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsService; + const forConfig = Effect.fn("RegistryStateService.forConfig")(function* ( + current: RegistryConfigInput, + ) { + const baseRegistryDir = yield* paths.resolve(current.baseRegistryDir); + const customRegistries = yield* Effect.forEach(current.customRegistries, paths.resolve); + return { + baseRegistryDir, + customRegistries, + registries: [ + { kind: "base" as const, path: baseRegistryDir }, + ...customRegistries.map((path) => ({ kind: "custom" as const, path })), + ], + }; + }); + return { forConfig } as const; + }), +}) { + static readonly layer = Layer.effect(RegistryStateService, RegistryStateService.make); +} diff --git a/src/domains/registry/service/registry.test.ts b/src/domains/registry/service/registry.test.ts new file mode 100644 index 0000000..1c539be --- /dev/null +++ b/src/domains/registry/service/registry.test.ts @@ -0,0 +1,94 @@ +import { existsSync } from "node:fs"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import type { RigConfig } from "../../settings/types/config-schema"; +import { RegistryDirectoryRepositoryService } from "../repo/registry-directory"; +import { RegistryService } from "./registry"; +import { RegistryStateService } from "./registry-state"; + +const directories: string[] = []; + +const makeTestRegistryLayer = (home: string) => { + let current: RigConfig = { + version: 1 as const, + baseRegistryDir: join(home, "base"), + customRegistries: [] as string[], + cronJobs: [], + }; + const settingsLayer = Layer.succeed(RigConfigStoreService, { + migrationResult: Effect.as(Effect.void, undefined), + acknowledgeMigrationPrompt: Effect.void, + ensure: Effect.sync(() => current), + read: Effect.sync(() => current), + write: (config) => Effect.sync(() => void (current = config)), + update: (mutator) => + Effect.promise(() => Promise.resolve(mutator(current))).pipe( + Effect.tap((updated) => Effect.sync(() => void (current = updated))), + ), + }); + const pathsLayer = Layer.succeed(RigPathsService, { + expandTilde: (value) => Effect.succeed(value.replace(/^~(?=\/|$)/, home)), + resolve: (value) => Effect.succeed(resolve(value.replace(/^~(?=\/|$)/, home))), + cronWorkerPath: (name) => Effect.succeed(join(home, "rig", "cron", `${name}.ts`)), + parentDir: (value) => Effect.succeed(resolve(value, "..")), + }); + const stateLayer = RegistryStateService.layer.pipe(Layer.provide(pathsLayer)); + return RegistryService.layer.pipe( + Layer.provide( + Layer.mergeAll( + settingsLayer, + stateLayer, + pathsLayer, + RegistryDirectoryRepositoryService.layer, + ), + ), + ); +}; + +afterEach(async () => { + await Promise.all( + directories.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); +}); + +describe("RegistryService", () => { + test("adds, lists, and removes a custom registry", async () => { + const home = await mkdtemp(join(tmpdir(), "rig-registry-test-")); + directories.push(home); + const custom = join(home, "custom"); + const result = await Effect.gen(function* () { + const registry = yield* RegistryService; + const added = yield* registry.add(custom); + const listed = yield* registry.list; + const removed = yield* registry.remove(custom); + return { added, listed, removed }; + }).pipe(Effect.provide(makeTestRegistryLayer(home)), Effect.runPromise); + + expect(result.added.customRegistries).toEqual([custom]); + expect(result.listed.registries).toContainEqual({ kind: "custom", path: custom }); + expect(result.removed.customRegistries).toEqual([]); + expect(existsSync(custom)).toBe(true); + expect(existsSync(join(home, "base"))).toBe(true); + }); + + test("rejects the base registry and missing removals", async () => { + const home = await mkdtemp(join(tmpdir(), "rig-registry-test-")); + directories.push(home); + const layer = makeTestRegistryLayer(home); + + const addBase = Effect.gen(function* () { + return yield* (yield* RegistryService).add(join(home, "base")); + }).pipe(Effect.provide(layer), Effect.runPromise); + const removeMissing = Effect.gen(function* () { + return yield* (yield* RegistryService).remove(join(home, "missing")); + }).pipe(Effect.provide(layer), Effect.runPromise); + + await expect(addBase).rejects.toThrow("already configured"); + await expect(removeMissing).rejects.toThrow("not configured"); + }); +}); diff --git a/src/domains/registry/service/registry.ts b/src/domains/registry/service/registry.ts new file mode 100644 index 0000000..1756d35 --- /dev/null +++ b/src/domains/registry/service/registry.ts @@ -0,0 +1,110 @@ +import { Context, Effect, Layer } from "effect"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, + RigPathsService, + rigPathsLayer, +} from "../../../providers/paths/rig-paths"; +import { RigConfigStoreService, rigConfigStoreLayer } from "../../settings/service/config-store"; +import type { RigConfigMutator } from "../../settings/types/config-store"; +import { + RegistryDirectoryRepositoryService, + registryDirectoryRepositoryLayer, +} from "../repo/registry-directory"; +import type { RegistryConfigState } from "../types/registry"; +import { RegistryStateService } from "./registry-state"; +import { RigError } from "../../../providers/errors/rig-error"; + +export class RegistryService extends Context.Service< + RegistryService, + { + readonly list: Effect.Effect; + readonly add: (pathValue: string) => Effect.Effect; + readonly remove: (pathValue: string) => Effect.Effect; + } +>()("@rendotdev/rig/registry/RegistryService", { + make: Effect.gen(function* () { + const settings = yield* RigConfigStoreService; + const states = yield* RegistryStateService; + const directories = yield* RegistryDirectoryRepositoryService; + const paths = yield* RigPathsService; + const mapSettingsError = (error: { cause: unknown }): RigError => + error.cause instanceof RigError + ? error.cause + : new RigError({ + code: "CONFIG_INVALID", + message: "Registry configuration failed.", + details: { error: error.cause }, + }); + const updateSettings = Effect.fn("RegistryService.updateSettings")(function* ( + mutator: RigConfigMutator, + ) { + yield* settings.update(mutator).pipe(Effect.mapError(mapSettingsError)); + }); + const list = Effect.gen(function* () { + const state = yield* states.forConfig( + yield* settings.ensure.pipe(Effect.mapError(mapSettingsError)), + ); + yield* directories.ensure(state.baseRegistryDir); + return state; + }).pipe(Effect.withSpan("RegistryService.list")); + const add = Effect.fn("RegistryService.add")(function* (pathValue: string) { + const current = yield* settings.ensure.pipe(Effect.mapError(mapSettingsError)); + const state = yield* states.forConfig(current); + const target = yield* paths.resolve(pathValue); + if (target === state.baseRegistryDir) { + return yield* new RigError({ + code: "CONFIG_INVALID", + message: "The base registry is already configured.", + details: { path: target }, + }); + } + if (!state.customRegistries.includes(target)) { + yield* directories.ensure(target); + yield* updateSettings((latest) => ({ + ...latest, + customRegistries: [...latest.customRegistries, pathValue], + })); + } + return yield* list; + }); + const remove = Effect.fn("RegistryService.remove")(function* (pathValue: string) { + const current = yield* settings.ensure.pipe(Effect.mapError(mapSettingsError)); + const target = yield* paths.resolve(pathValue); + const resolved = yield* Effect.forEach(current.customRegistries, (entry) => + paths.resolve(entry).pipe(Effect.map((path) => ({ entry, path }))), + ); + const removed = new Set( + resolved.filter((entry) => entry.path === target).map((entry) => entry.entry), + ); + if (removed.size === 0) { + return yield* new RigError({ + code: "CONFIG_INVALID", + message: `Registry is not configured: ${pathValue}`, + details: { path: target }, + }); + } + yield* updateSettings((latest) => ({ + ...latest, + customRegistries: latest.customRegistries.filter((entry) => !removed.has(entry)), + })); + return yield* list; + }); + return { list, add, remove } as const; + }), +}) { + static readonly layer = Layer.effect(RegistryService, RegistryService.make); +} + +const registryStateLayer = RegistryStateService.layer.pipe(Layer.provide(rigPathsLayer)); +const registryDependenciesLayer = Layer.mergeAll( + rigConfigStoreLayer, + registryStateLayer, + registryDirectoryRepositoryLayer, + rigPathsLayer, +); +export const registryLayer: Layer.Layer< + RegistryService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = RegistryService.layer.pipe(Layer.provide(registryDependenciesLayer)); diff --git a/src/domains/registry/service/tool-discovery-collection.ts b/src/domains/registry/service/tool-discovery-collection.ts new file mode 100644 index 0000000..4662764 --- /dev/null +++ b/src/domains/registry/service/tool-discovery-collection.ts @@ -0,0 +1,38 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { DiscoveredTool } from "../types/tool-discovery"; + +export class ToolDiscoveryCollectionService extends Context.Service< + ToolDiscoveryCollectionService, + { + readonly unique: ( + discoveredByRegistry: DiscoveredTool[][], + ) => Effect.Effect; + } +>()("@rendotdev/rig/registry/ToolDiscoveryCollectionService", { + make: Effect.gen(function* () { + const unique = Effect.fn("ToolDiscoveryCollectionService.unique")(function* ( + discoveredByRegistry: DiscoveredTool[][], + ) { + const tools = new Map(); + for (const tool of discoveredByRegistry.flat()) { + const existing = tools.get(tool.name); + if (existing) { + return yield* new RigError({ + code: "DUPLICATE_TOOL", + message: `Duplicate tool name: ${tool.name}`, + details: { name: tool.name, paths: [existing.toolPath, tool.toolPath] }, + }); + } + tools.set(tool.name, tool); + } + return [...tools.values()].toSorted((left, right) => left.name.localeCompare(right.name)); + }); + return { unique } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolDiscoveryCollectionService, + ToolDiscoveryCollectionService.make, + ); +} diff --git a/src/domains/registry/service/tool-discovery.test.ts b/src/domains/registry/service/tool-discovery.test.ts new file mode 100644 index 0000000..72ccafd --- /dev/null +++ b/src/domains/registry/service/tool-discovery.test.ts @@ -0,0 +1,109 @@ +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import type { RegistryEntry } from "../types/registry"; +import { RegistryService } from "./registry"; +import { ToolDiscoveryService, toolDiscoveryLayer } from "./tool-discovery"; + +const homes: string[] = []; + +const makeDiscoveryLayer = (home: string, registries: RegistryEntry[]) => { + const state = { + baseRegistryDir: registries[0]?.path ?? join(home, "base"), + customRegistries: registries.slice(1).map((entry) => entry.path), + registries, + }; + const registryLayer = Layer.succeed(RegistryService, { + list: Effect.succeed(state), + add: () => Effect.succeed(state), + remove: () => Effect.succeed(state), + }); + const pathsLayer = Layer.succeed(RigPathsService, { + expandTilde: (value) => Effect.succeed(value.replace(/^~(?=\/|$)/, home)), + resolve: (value) => Effect.succeed(resolve(value.replace(/^~(?=\/|$)/, home))), + cronWorkerPath: (name) => Effect.succeed(join(home, "rig", "cron", `${name}.ts`)), + parentDir: (value) => Effect.succeed(resolve(value, "..")), + }); + return toolDiscoveryLayer.pipe(Layer.provide(Layer.merge(registryLayer, pathsLayer))); +}; + +const createHome = async () => { + const home = await mkdtemp(join(tmpdir(), "rig-discovery-test-")); + homes.push(home); + return home; +}; + +const runDiscovery = ( + layer: Layer.Layer, + use: (service: typeof ToolDiscoveryService.Service) => Effect.Effect, +) => ToolDiscoveryService.use(use).pipe(Effect.provide(layer), Effect.runPromise); + +afterEach(async () => { + await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); +}); + +describe("ToolDiscoveryService", () => { + test("discovers supported entry files", async () => { + const home = await createHome(); + const registryPath = join(home, "tools"); + const toolDir = join(registryPath, "view-tool"); + await mkdir(toolDir, { recursive: true }); + await writeFile(join(toolDir, "index.rig.tsx"), "export default {};\n", "utf8"); + const layer = makeDiscoveryLayer(home, [{ kind: "base", path: registryPath }]); + + const tools = await runDiscovery(layer, (service) => service.discover()); + + expect(tools).toMatchObject([{ name: "view-tool", toolPath: join(toolDir, "index.rig.tsx") }]); + await expect( + runDiscovery(layer, (service) => service.find("view-tool")), + ).resolves.toMatchObject({ name: "view-tool", toolPath: join(toolDir, "index.rig.tsx") }); + }); + + test("rejects legacy and duplicate tool entries", async () => { + const home = await createHome(); + const base = join(home, "base"); + const custom = join(home, "custom"); + await mkdir(join(base, "legacy"), { recursive: true }); + await writeFile(join(base, "legacy", "tool.ts"), "export default {};\n", "utf8"); + const legacyLayer = makeDiscoveryLayer(home, [{ kind: "base", path: base }]); + await expect(runDiscovery(legacyLayer, (service) => service.discover())).rejects.toThrow( + "Tool legacy must use index.rig.ts or index.rig.tsx.", + ); + await rm(join(base, "legacy"), { recursive: true, force: true }); + + await Promise.all( + [base, custom].map(async (registryPath) => { + await mkdir(join(registryPath, "sample"), { recursive: true }); + await writeFile(join(registryPath, "sample", "index.rig.ts"), "export default {};\n"); + }), + ); + const duplicateLayer = makeDiscoveryLayer(home, [ + { kind: "base", path: base }, + { kind: "custom", path: custom }, + ]); + await expect(runDiscovery(duplicateLayer, (service) => service.discover())).rejects.toThrow( + "Duplicate tool name: sample", + ); + await expect(runDiscovery(duplicateLayer, (service) => service.find("sample"))).rejects.toThrow( + "Duplicate tool name: sample", + ); + }); + + test("rejects invalid names and files that are not tool directories", async () => { + const home = await createHome(); + const registryPath = join(home, "tools"); + await mkdir(registryPath, { recursive: true }); + await writeFile(join(registryPath, "plain-file"), "not a tool\n", "utf8"); + const layer = makeDiscoveryLayer(home, [{ kind: "base", path: registryPath }]); + + await expect(runDiscovery(layer, (service) => service.find("../sample"))).rejects.toThrow( + "Tool not found: ../sample", + ); + await expect(runDiscovery(layer, (service) => service.find("plain-file"))).rejects.toThrow( + "Tool not found: plain-file", + ); + }); +}); diff --git a/src/domains/registry/service/tool-discovery.ts b/src/domains/registry/service/tool-discovery.ts new file mode 100644 index 0000000..663920f --- /dev/null +++ b/src/domains/registry/service/tool-discovery.ts @@ -0,0 +1,95 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { + ToolDiscoveryRepositoryService, + toolDiscoveryRepositoryLayer, +} from "../repo/tool-discovery-repository"; +import type { RegistryEntry } from "../types/registry"; +import type { DiscoveredTool, ToolDiscoveryOptions } from "../types/tool-discovery"; +import { RegistryService } from "./registry"; +import { ToolDiscoveryCollectionService } from "./tool-discovery-collection"; + +export class ToolDiscoveryService extends Context.Service< + ToolDiscoveryService, + { + readonly discover: ( + options?: ToolDiscoveryOptions, + ) => Effect.Effect; + readonly discoverRegistry: (entry: RegistryEntry) => Effect.Effect; + readonly find: (name: string) => Effect.Effect; + readonly projectRootFor: (pathValue: string) => Effect.Effect; + } +>()("@rendotdev/rig/registry/ToolDiscoveryService", { + make: Effect.gen(function* () { + const registryConfig = yield* RegistryService; + const repository = yield* ToolDiscoveryRepositoryService; + const collections = yield* ToolDiscoveryCollectionService; + const visibleRegistryEntries = (params: { + entries: RegistryEntry[]; + visibleFromPath?: string; + }): RegistryEntry[] => { + if (!params.visibleFromPath) return params.entries; + return params.entries; + }; + const projectRootFor = Effect.fn("ToolDiscoveryService.projectRootFor")(function* ( + pathValue: string, + ) { + return yield* repository.projectRootFor(pathValue); + }); + const discoverRegistry = Effect.fn("ToolDiscoveryService.discoverRegistry")(function* ( + entry: RegistryEntry, + ) { + return yield* repository.discoverRegistry(entry); + }); + const discover = Effect.fn("ToolDiscoveryService.discover")(function* ( + options: ToolDiscoveryOptions = {}, + ) { + const config = yield* registryConfig.list; + const entries = visibleRegistryEntries({ + entries: config.registries, + visibleFromPath: options.visibleFromPath, + }); + const discoveredByRegistry = yield* Effect.all(entries.map(discoverRegistry), { + concurrency: "unbounded", + }); + return yield* collections.unique(discoveredByRegistry); + }); + const find = Effect.fn("ToolDiscoveryService.find")(function* (name: string) { + if (!/^[A-Za-z0-9_-]+$/.test(name)) { + return yield* new RigError({ + code: "TOOL_NOT_FOUND", + message: `Tool not found: ${name}`, + details: { name }, + }); + } + const config = yield* registryConfig.list; + const inspected = yield* Effect.all( + config.registries.map((entry) => repository.inspect(entry, name)), + ); + const discovered = inspected.filter((tool): tool is DiscoveredTool => tool !== undefined); + if (discovered.length > 1) { + return yield* new RigError({ + code: "DUPLICATE_TOOL", + message: `Duplicate tool name: ${name}`, + details: { name, paths: discovered.map((tool) => tool.toolPath) }, + }); + } + const tool = discovered[0]; + if (!tool) { + return yield* new RigError({ + code: "TOOL_NOT_FOUND", + message: `Tool not found: ${name}`, + details: { name }, + }); + } + return tool; + }); + return { discover, discoverRegistry, find, projectRootFor } as const; + }), +}) { + static readonly layer = Layer.effect(ToolDiscoveryService, ToolDiscoveryService.make); +} + +export const toolDiscoveryLayer = ToolDiscoveryService.layer.pipe( + Layer.provide(Layer.merge(toolDiscoveryRepositoryLayer, ToolDiscoveryCollectionService.layer)), +); diff --git a/src/domains/registry/types/registry.ts b/src/domains/registry/types/registry.ts new file mode 100644 index 0000000..e440f56 --- /dev/null +++ b/src/domains/registry/types/registry.ts @@ -0,0 +1,17 @@ +export type RegistryKind = "base" | "custom"; + +export type RegistryEntry = Readonly<{ + kind: RegistryKind; + path: string; +}>; + +export type RegistryConfigInput = Readonly<{ + baseRegistryDir: string; + customRegistries: string[]; +}>; + +export type RegistryConfigState = Readonly<{ + baseRegistryDir: string; + customRegistries: string[]; + registries: RegistryEntry[]; +}>; diff --git a/src/domains/registry/types/tool-discovery.ts b/src/domains/registry/types/tool-discovery.ts new file mode 100644 index 0000000..904b8cc --- /dev/null +++ b/src/domains/registry/types/tool-discovery.ts @@ -0,0 +1,15 @@ +import type { RegistryKind } from "./registry"; + +export const RigToolEntryFiles = ["index.rig.ts", "index.rig.tsx"] as const; + +export type ToolDiscoveryOptions = Readonly<{ + visibleFromPath?: string; +}>; + +export type DiscoveredTool = Readonly<{ + name: string; + registryKind: RegistryKind; + registryPath: string; + toolDir: string; + toolPath: string; +}>; diff --git a/src/domains/scheduling/repo/cron-model-platform.ts b/src/domains/scheduling/repo/cron-model-platform.ts new file mode 100644 index 0000000..fd1a877 --- /dev/null +++ b/src/domains/scheduling/repo/cron-model-platform.ts @@ -0,0 +1,43 @@ +import { readFile } from "node:fs/promises"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; + +export class CronModelPlatformService extends Context.Service< + CronModelPlatformService, + { + readonly readJson: (path: string) => Effect.Effect; + readonly fileUrlToPath: (url: string) => Effect.Effect; + readonly pathToFileUrl: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/scheduling/CronModelPlatformService", { + make: Effect.gen(function* () { + const mapError = (cause: unknown) => + new RigError({ + code: "CRON_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const readJson = Effect.fn("CronModelPlatformService.readJson")(function* (path: string) { + return yield* Effect.tryPromise({ + try: async () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(path).json() + : JSON.parse(await readFile(path, "utf8")), + catch: mapError, + }); + }); + const fileUrlToPath = Effect.fn("CronModelPlatformService.fileUrlToPath")(function* ( + url: string, + ) { + return yield* Effect.try({ try: () => fileURLToPath(url), catch: mapError }); + }); + const pathToFileUrl = Effect.fn("CronModelPlatformService.pathToFileUrl")(function* ( + path: string, + ) { + return pathToFileURL(path).href; + }); + return { readJson, fileUrlToPath, pathToFileUrl } as const; + }), +}) { + static readonly layer = Layer.effect(CronModelPlatformService, CronModelPlatformService.make); +} diff --git a/src/domains/scheduling/repo/cron-registrar.ts b/src/domains/scheduling/repo/cron-registrar.ts new file mode 100644 index 0000000..44ac2e3 --- /dev/null +++ b/src/domains/scheduling/repo/cron-registrar.ts @@ -0,0 +1,70 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; + +type BunCronApi = { + (path: string, schedule: string, title: string): Promise; + parse(expression: string): Date | null; + remove(title: string): Promise; +}; + +export class CronRegistrarService extends Context.Service< + CronRegistrarService, + { + readonly register: (params: { + readonly path: string; + readonly schedule: string; + readonly title: string; + }) => Effect.Effect; + readonly remove: (title: string) => Effect.Effect; + readonly validate: (schedule: string) => Effect.Effect; + } +>()("@rendotdev/rig/scheduling/CronRegistrarService", { + make: Effect.gen(function* () { + const getCron = () => { + const cron = (globalThis as typeof globalThis & { Bun?: { cron?: unknown } }).Bun?.cron; + if (typeof cron === "function") return cron as BunCronApi; + throw new RigError({ + code: "CRON_ERROR", + message: "Bun cron is unavailable. Run rig with Bun.", + }); + }; + const mapError = (cause: unknown) => + cause instanceof RigError + ? cause + : new RigError({ + code: "CRON_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const register = Effect.fn("CronRegistrarService.register")(function* (params: { + readonly path: string; + readonly schedule: string; + readonly title: string; + }) { + yield* Effect.tryPromise({ + try: () => getCron()(params.path, params.schedule, params.title), + catch: mapError, + }); + }); + const remove = Effect.fn("CronRegistrarService.remove")(function* (title: string) { + yield* Effect.tryPromise({ try: () => getCron().remove(title), catch: mapError }); + }); + const validate = Effect.fn("CronRegistrarService.validate")(function* (schedule: string) { + yield* Effect.try({ + try: () => { + if (!getCron().parse(schedule)) { + throw new RigError({ + code: "CRON_ERROR", + message: `Cron schedule has no future runs: ${schedule}`, + }); + } + }, + catch: mapError, + }); + }); + return { register, remove, validate } as const; + }), +}) { + static readonly layer = Layer.effect(CronRegistrarService, CronRegistrarService.make); +} + +export const cronRegistrarLayer = CronRegistrarService.layer; diff --git a/src/domains/scheduling/repo/cron-worker-repository.ts b/src/domains/scheduling/repo/cron-worker-repository.ts new file mode 100644 index 0000000..480082b --- /dev/null +++ b/src/domains/scheduling/repo/cron-worker-repository.ts @@ -0,0 +1,58 @@ +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; +import { dirname } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { CronStateError } from "../types/cron"; + +export class CronWorkerRepositoryService extends Context.Service< + CronWorkerRepositoryService, + { + readonly read: (path: string) => Effect.Effect; + readonly write: (path: string, source: string) => Effect.Effect; + readonly remove: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/scheduling/CronWorkerRepositoryService", { + make: Effect.gen(function* () { + const errorCode = (cause: unknown) => + typeof cause === "object" && cause !== null && "code" in cause + ? String(cause.code) + : undefined; + const read = Effect.fn("CronWorkerRepositoryService.read")(function* (path: string) { + return yield* Effect.tryPromise({ + try: () => readFile(path, "utf8"), + catch: (cause) => new CronStateError({ cause }), + }).pipe( + Effect.catch((error) => + errorCode(error.cause) === "ENOENT" + ? Effect.as(Effect.void, undefined as string | undefined) + : Effect.fail(error), + ), + ); + }); + const write = Effect.fn("CronWorkerRepositoryService.write")(function* ( + path: string, + source: string, + ) { + yield* Effect.tryPromise({ + try: async () => { + await mkdir(dirname(path), { recursive: true }); + await writeFile(path, source, "utf8"); + }, + catch: (cause) => new CronStateError({ cause }), + }); + }); + const remove = Effect.fn("CronWorkerRepositoryService.remove")(function* (path: string) { + yield* Effect.tryPromise({ + try: () => rm(path, { force: true }), + catch: (cause) => new CronStateError({ cause }), + }); + }); + return { read, write, remove } as const; + }), +}) { + static readonly layer = Layer.effect( + CronWorkerRepositoryService, + CronWorkerRepositoryService.make, + ); +} + +export const cronWorkerRepositoryLayer = CronWorkerRepositoryService.layer; diff --git a/src/domains/scheduling/runtime/rig-cron-operations.ts b/src/domains/scheduling/runtime/rig-cron-operations.ts new file mode 100644 index 0000000..94e88c2 --- /dev/null +++ b/src/domains/scheduling/runtime/rig-cron-operations.ts @@ -0,0 +1,206 @@ +import { Context, Effect, Layer } from "effect"; +import { + RigPathsConfigService, + RigPathsService, + rigPathsLayer, +} from "../../../providers/paths/rig-paths"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolRunnerService } from "../../tools/runtime/tool-runner"; +import { toolServicesLayer } from "../../tools/runtime/tool-services-layer"; +import { RigConfigStoreService, rigConfigStoreLayer } from "../../settings/service/config-store"; +import type { RigCronJob } from "../../settings/types/config-schema"; +import type { CronAddOptions, CronRunResult } from "../types/cron"; +import { CronModelService, cronModelLayer } from "../service/cron-model"; +import { + CronStateTransactionService, + cronStateTransactionLayer, +} from "../service/cron-state-transaction"; +import { RigCronError } from "../types/cron"; + +class RigCronToolService extends Context.Service< + RigCronToolService, + { + readonly validate: (params: { + readonly id: string; + readonly tool: string; + readonly command: string; + readonly input: unknown; + }) => Effect.Effect; + readonly runJob: ( + job: RigCronJob, + ) => Effect.Effect<{ readonly envelope: unknown; readonly exitCode: number }, RigError>; + } +>()("@rendotdev/rig/cron/RigCronToolService", { + make: Effect.gen(function* () { + const runner = yield* ToolRunnerService; + const model = yield* CronModelService; + const validate = Effect.fn("RigCronToolService.validate")(function* (params: { + readonly id: string; + readonly tool: string; + readonly command: string; + readonly input: unknown; + }) { + const result = yield* runner.run(params.tool, params.command, { + input: params.input === undefined ? undefined : JSON.stringify(params.input), + dryRun: true, + }); + if (result.exitCode !== 0) { + return yield* new RigError({ + code: "CRON_ERROR", + message: `Cron command validation failed: ${params.id}`, + details: { envelope: result.envelope }, + }); + } + return undefined; + }); + const runJob = Effect.fn("RigCronToolService.runJob")(function* (job: RigCronJob) { + const target = yield* model.parseCommandTarget(job.command); + return yield* runner.run(target.tool, target.command, { + input: job.input === undefined ? undefined : JSON.stringify(job.input), + }); + }); + return { validate, runJob } as const; + }), +}) { + static readonly layer = Layer.effect(RigCronToolService, RigCronToolService.make); +} + +export class RigCronQueryService extends Context.Service< + RigCronQueryService, + { + readonly list: Effect.Effect<{ readonly cronJobs: RigCronJob[] }, RigCronError>; + readonly run: (name: string) => Effect.Effect; + } +>()("@rendotdev/rig/cron/RigCronQueryService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const model = yield* CronModelService; + const tools = yield* RigCronToolService; + const list = config.ensure.pipe( + Effect.map((value) => ({ + cronJobs: [...value.cronJobs].toSorted((left, right) => + left.name.localeCompare(right.name), + ), + })), + Effect.mapError((cause) => new RigCronError({ operation: "list", cause })), + Effect.withSpan("RigCronQueryService.list"), + ); + const run = Effect.fn("RigCronQueryService.run")(function* (nameValue: string) { + const name = yield* model + .parseJobName(nameValue) + .pipe(Effect.mapError((cause) => new RigCronError({ operation: "run", cause }))); + const current = yield* config.ensure.pipe( + Effect.mapError((cause) => new RigCronError({ operation: "run", cause })), + ); + const job = current.cronJobs.find((candidate) => candidate.name === name); + if (!job) { + return yield* new RigCronError({ + operation: "run", + cause: new RigError({ + code: "CRON_ERROR", + message: `Cron job not found: ${name}`, + details: { name }, + }), + }); + } + const result = yield* tools + .runJob(job) + .pipe(Effect.mapError((cause) => new RigCronError({ operation: "run", cause }))); + return { job, envelope: result.envelope, exitCode: result.exitCode }; + }); + return { list, run } as const; + }), +}) { + static readonly layer = Layer.effect(RigCronQueryService, RigCronQueryService.make); +} + +export class RigCronMutationService extends Context.Service< + RigCronMutationService, + { + readonly add: ( + params: CronAddOptions, + ) => Effect.Effect<{ readonly job: RigCronJob; readonly workerPath: string }, RigCronError>; + readonly remove: ( + name: string, + ) => Effect.Effect< + { readonly name: string; readonly removed: boolean; readonly workerPath: string }, + RigCronError + >; + } +>()("@rendotdev/rig/cron/RigCronMutationService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const pathValues = yield* RigPathsConfigService; + const paths = yield* RigPathsService; + const model = yield* CronModelService; + const transaction = yield* CronStateTransactionService; + const tools = yield* RigCronToolService; + const mapError = (operation: "add" | "remove") => (cause: unknown) => + new RigCronError({ operation, cause }); + const add = Effect.fn("RigCronMutationService.add")(function* (options: CronAddOptions) { + const name = yield* model.parseJobName(options.name).pipe(Effect.mapError(mapError("add"))); + const target = yield* model + .parseCommandTarget(options.command) + .pipe(Effect.mapError(mapError("add"))); + const input = yield* model.readInput(options).pipe(Effect.mapError(mapError("add"))); + yield* model.validateSchedule(options.schedule).pipe(Effect.mapError(mapError("add"))); + yield* tools.validate({ ...target, input }).pipe(Effect.mapError(mapError("add"))); + yield* config.ensure.pipe(Effect.mapError(mapError("add"))); + const job: RigCronJob = { + name, + command: target.id, + schedule: options.schedule, + ...(input === undefined ? {} : { input }), + }; + const workerPath = yield* paths.cronWorkerPath(name); + const workerSource = yield* model + .renderWorker({ name, homeDir: pathValues.homeDir, moduleUrl: options.moduleUrl }) + .pipe(Effect.mapError(mapError("add"))); + const snapshot = yield* transaction + .snapshot(workerPath) + .pipe(Effect.mapError(mapError("add"))); + yield* transaction + .replace({ snapshot, workerPath, workerSource, job }) + .pipe(Effect.mapError(mapError("add"))); + return { job, workerPath }; + }); + const remove = Effect.fn("RigCronMutationService.remove")(function* (nameValue: string) { + const name = yield* model.parseJobName(nameValue).pipe(Effect.mapError(mapError("remove"))); + yield* config.ensure.pipe(Effect.mapError(mapError("remove"))); + const workerPath = yield* paths.cronWorkerPath(name); + const snapshot = yield* transaction + .snapshot(workerPath) + .pipe(Effect.mapError(mapError("remove"))); + const removed = yield* transaction + .remove({ snapshot, workerPath, name }) + .pipe(Effect.mapError(mapError("remove"))); + return { name, removed, workerPath }; + }); + return { add, remove } as const; + }), +}) { + static readonly layer = Layer.effect(RigCronMutationService, RigCronMutationService.make); +} + +const rigCronToolLayer = RigCronToolService.layer.pipe( + Layer.provide(Layer.merge(toolServicesLayer, cronModelLayer)), +); +const cronTransactionConfiguredLayer = cronStateTransactionLayer.pipe( + Layer.provide(rigConfigStoreLayer), +); +const rigCronQueryLayer = RigCronQueryService.layer.pipe( + Layer.provide(Layer.mergeAll(rigConfigStoreLayer, cronModelLayer, rigCronToolLayer)), +); +const rigCronMutationLayer = RigCronMutationService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigConfigStoreLayer, + rigPathsLayer, + cronModelLayer, + cronTransactionConfiguredLayer, + rigCronToolLayer, + ), + ), +); + +export const rigCronOperationsLayer = Layer.merge(rigCronQueryLayer, rigCronMutationLayer); diff --git a/src/domains/scheduling/runtime/rig-cron.ts b/src/domains/scheduling/runtime/rig-cron.ts new file mode 100644 index 0000000..c9dec81 --- /dev/null +++ b/src/domains/scheduling/runtime/rig-cron.ts @@ -0,0 +1,46 @@ +import { Context, Effect, Layer } from "effect"; +import type { RigCronJob } from "../../settings/types/config-schema"; +import { + RigCronMutationService, + rigCronOperationsLayer, + RigCronQueryService, +} from "./rig-cron-operations"; +import type { CronAddOptions, CronRunResult } from "../types/cron"; +import { RigCronError } from "../types/cron"; + +export class RigCronService extends Context.Service< + RigCronService, + { + readonly list: Effect.Effect<{ readonly cronJobs: RigCronJob[] }, RigCronError>; + readonly add: ( + params: CronAddOptions, + ) => Effect.Effect<{ readonly job: RigCronJob; readonly workerPath: string }, RigCronError>; + readonly remove: ( + name: string, + ) => Effect.Effect< + { readonly name: string; readonly removed: boolean; readonly workerPath: string }, + RigCronError + >; + readonly run: (name: string) => Effect.Effect; + } +>()("@rendotdev/rig/scheduling/RigCronService", { + make: Effect.gen(function* () { + const queries = yield* RigCronQueryService; + const mutations = yield* RigCronMutationService; + const list = queries.list.pipe(Effect.withSpan("RigCronService.list")); + const add = Effect.fn("RigCronService.add")(function* (params: CronAddOptions) { + return yield* mutations.add(params); + }); + const remove = Effect.fn("RigCronService.remove")(function* (name: string) { + return yield* mutations.remove(name); + }); + const run = Effect.fn("RigCronService.run")(function* (name: string) { + return yield* queries.run(name); + }); + return { list, add, remove, run } as const; + }), +}) { + static readonly layer = Layer.effect(RigCronService, RigCronService.make); +} + +export const rigCronLayer = RigCronService.layer.pipe(Layer.provide(rigCronOperationsLayer)); diff --git a/src/domains/scheduling/service/cron-model.ts b/src/domains/scheduling/service/cron-model.ts new file mode 100644 index 0000000..4225d7f --- /dev/null +++ b/src/domains/scheduling/service/cron-model.ts @@ -0,0 +1,101 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolIdentifierService, toolIdentifierLayer } from "../../tools/service/tool-identifier"; +import { CronModelPlatformService } from "../repo/cron-model-platform"; +import { CronRegistrarService, cronRegistrarLayer } from "../repo/cron-registrar"; + +export class CronModelService extends Context.Service< + CronModelService, + { + readonly parseJobName: (value: string) => Effect.Effect; + readonly parseCommandTarget: ( + id: string, + ) => Effect.Effect< + { readonly id: string; readonly tool: string; readonly command: string }, + RigError + >; + readonly readInput: (params: { + readonly input?: string; + readonly inputFile?: string; + }) => Effect.Effect; + readonly renderWorker: (params: { + readonly name: string; + readonly homeDir?: string; + readonly moduleUrl: string; + }) => Effect.Effect; + readonly moduleUrl: (metaUrl: string) => Effect.Effect; + readonly validateSchedule: (schedule: string) => Effect.Effect; + } +>()("@rendotdev/rig/scheduling/CronModelService", { + make: Effect.gen(function* () { + const platform = yield* CronModelPlatformService; + const identifiers = yield* ToolIdentifierService; + const registrar = yield* CronRegistrarService; + const parseJobName = Effect.fn("CronModelService.parseJobName")(function* (value: string) { + if (/^[A-Za-z0-9_-]+$/u.test(value)) return value; + return yield* new RigError({ + code: "INPUT_ERROR", + message: "Cron job names may only contain letters, numbers, hyphens, and underscores.", + details: { name: value }, + }); + }); + const parseCommandTarget = Effect.fn("CronModelService.parseCommandTarget")(function* ( + id: string, + ) { + const target = yield* identifiers.parseCommandTarget(id); + return { id, tool: target.tool, command: target.command } as const; + }); + const readInput = Effect.fn("CronModelService.readInput")(function* (params: { + readonly input?: string; + readonly inputFile?: string; + }) { + const hasConflictingInputSources = Boolean(params.input && params.inputFile); + if (hasConflictingInputSources) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: "Use --input or --input-file, not both.", + }); + } + if (params.inputFile) return yield* platform.readJson(params.inputFile); + if (params.input === undefined) return undefined; + return yield* Effect.try({ + try: () => JSON.parse(params.input!) as unknown, + catch: (cause) => + new RigError({ code: "INPUT_ERROR", message: "Input JSON is invalid.", details: cause }), + }); + }); + const renderWorker = Effect.fn("CronModelService.renderWorker")(function* (params: { + readonly name: string; + readonly homeDir?: string; + readonly moduleUrl: string; + }) { + const entrypoint = yield* platform.fileUrlToPath(params.moduleUrl); + const environment = params.homeDir ? { RIG_HOME: params.homeDir } : {}; + return `export default {\n async scheduled() {\n const proc = Bun.spawn([\n process.execPath,\n "--install=fallback",\n ${JSON.stringify(entrypoint)},\n "cron",\n "run",\n ${JSON.stringify(params.name)},\n ], {\n stdout: "pipe",\n stderr: "pipe",\n env: { ...process.env, ...${JSON.stringify(environment)} },\n });\n\n const [stdout, stderr, exitCode] = await Promise.all([\n new Response(proc.stdout).text(),\n new Response(proc.stderr).text(),\n proc.exited,\n ]);\n if (stdout) console.log(stdout.trimEnd());\n if (stderr) console.error(stderr.trimEnd());\n if (exitCode !== 0) throw new Error("Rig cron job failed: " + ${JSON.stringify(params.name)});\n },\n};\n`; + }); + const moduleUrl = Effect.fn("CronModelService.moduleUrl")(function* (metaUrl: string) { + return yield* platform.pathToFileUrl(yield* platform.fileUrlToPath(metaUrl)); + }); + const validateSchedule = Effect.fn("CronModelService.validateSchedule")(function* ( + schedule: string, + ) { + yield* registrar.validate(schedule); + }); + return { + parseJobName, + parseCommandTarget, + readInput, + renderWorker, + moduleUrl, + validateSchedule, + } as const; + }), +}) { + static readonly layer = Layer.effect(CronModelService, CronModelService.make); +} + +export const cronModelLayer = CronModelService.layer.pipe( + Layer.provide( + Layer.mergeAll(CronModelPlatformService.layer, toolIdentifierLayer, cronRegistrarLayer), + ), +); diff --git a/src/domains/scheduling/service/cron-state-transaction.ts b/src/domains/scheduling/service/cron-state-transaction.ts new file mode 100644 index 0000000..b2430a1 --- /dev/null +++ b/src/domains/scheduling/service/cron-state-transaction.ts @@ -0,0 +1,345 @@ +import { Context, Effect, Layer, Result } from "effect"; +import type { RigCronJob } from "../../settings/types/config-schema"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import { CronRegistrarService, cronRegistrarLayer } from "../repo/cron-registrar"; +import { + CronWorkerRepositoryService, + cronWorkerRepositoryLayer, +} from "../repo/cron-worker-repository"; +import { CronStateError, type CronWorkerSnapshot } from "../types/cron"; + +export class CronRollbackService extends Context.Service< + CronRollbackService, + { + readonly restoreReplacedJob: ( + job: RigCronJob, + previousJob?: RigCronJob, + ) => Effect.Effect; + readonly restoreRemovedJob: (previousJob: RigCronJob) => Effect.Effect; + readonly restoreWorker: ( + snapshot: CronWorkerSnapshot, + workerPath: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/cron/CronRollbackService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const workers = yield* CronWorkerRepositoryService; + const sameJob = (left: RigCronJob, right: RigCronJob) => + JSON.stringify(left) === JSON.stringify(right); + const restoreReplacedJob = Effect.fn("CronRollbackService.restoreReplacedJob")(function* ( + job: RigCronJob, + previousJob?: RigCronJob, + ) { + yield* config + .update((current) => { + const stored = current.cronJobs.find((candidate) => candidate.name === job.name); + const shouldPreserveCurrentJob = !stored || !sameJob(stored, job); + if (shouldPreserveCurrentJob) return current; + const remaining = current.cronJobs.filter((candidate) => candidate.name !== job.name); + return { + ...current, + cronJobs: previousJob ? [...remaining, previousJob] : remaining, + }; + }) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + }); + const restoreRemovedJob = Effect.fn("CronRollbackService.restoreRemovedJob")(function* ( + previousJob: RigCronJob, + ) { + yield* config + .update((current) => + current.cronJobs.some((job) => job.name === previousJob.name) + ? current + : { ...current, cronJobs: [...current.cronJobs, previousJob] }, + ) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + }); + const restoreWorker = Effect.fn("CronRollbackService.restoreWorker")(function* ( + snapshot: CronWorkerSnapshot, + workerPath: string, + ) { + if (snapshot.workerSource === undefined) yield* workers.remove(workerPath); + else yield* workers.write(workerPath, snapshot.workerSource); + }); + return { restoreReplacedJob, restoreRemovedJob, restoreWorker } as const; + }), +}) { + static readonly layer = Layer.effect(CronRollbackService, CronRollbackService.make); +} + +export class CronReplaceTransactionService extends Context.Service< + CronReplaceTransactionService, + { + readonly replace: (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly workerSource: string; + readonly job: RigCronJob; + }) => Effect.Effect; + } +>()("@rendotdev/rig/cron/CronReplaceTransactionService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const registrar = yield* CronRegistrarService; + const workers = yield* CronWorkerRepositoryService; + const rollback = yield* CronRollbackService; + const capture = Effect.fn("CronReplaceTransactionService.capture")(function* ( + errors: unknown[], + operation: Effect.Effect, + ) { + const result = yield* Effect.result(operation); + if (Result.isFailure(result)) errors.push(result.failure.cause); + }); + const replace = Effect.fn("CronReplaceTransactionService.replace")(function* (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly workerSource: string; + readonly job: RigCronJob; + }) { + let previousJob: RigCronJob | undefined; + let workerChanged = false; + let configChanged = false; + let registrationAttempted = false; + yield* Effect.gen(function* () { + yield* workers.write(params.workerPath, params.workerSource); + workerChanged = true; + yield* config + .update((current) => { + previousJob = current.cronJobs.find((job) => job.name === params.job.name); + return { + ...current, + cronJobs: [ + ...current.cronJobs.filter((job) => job.name !== params.job.name), + params.job, + ], + }; + }) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + configChanged = true; + registrationAttempted = true; + yield* registrar + .register({ + path: params.workerPath, + schedule: params.job.schedule, + title: params.job.name, + }) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + }).pipe( + Effect.catch((cause) => + Effect.gen(function* () { + const errors: unknown[] = []; + if (configChanged) + yield* capture(errors, rollback.restoreReplacedJob(params.job, previousJob)); + if (workerChanged) + yield* capture(errors, rollback.restoreWorker(params.snapshot, params.workerPath)); + if (registrationAttempted) { + const restoreRegistration = previousJob + ? registrar.register({ + path: params.workerPath, + schedule: previousJob.schedule, + title: previousJob.name, + }) + : registrar.remove(params.job.name); + yield* capture( + errors, + restoreRegistration.pipe( + Effect.mapError((error) => new CronStateError({ cause: error })), + ), + ); + } + return yield* new CronStateError({ + cause, + ...(errors.length > 0 ? { rollbackErrors: errors } : {}), + }); + }), + ), + ); + }); + return { replace } as const; + }), +}) { + static readonly layer = Layer.effect( + CronReplaceTransactionService, + CronReplaceTransactionService.make, + ); +} + +export class CronRemoveTransactionService extends Context.Service< + CronRemoveTransactionService, + { + readonly remove: (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly name: string; + }) => Effect.Effect; + } +>()("@rendotdev/rig/cron/CronRemoveTransactionService", { + make: Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const registrar = yield* CronRegistrarService; + const workers = yield* CronWorkerRepositoryService; + const rollback = yield* CronRollbackService; + const capture = Effect.fn("CronRemoveTransactionService.capture")(function* ( + errors: unknown[], + operation: Effect.Effect, + ) { + const result = yield* Effect.result(operation); + if (Result.isFailure(result)) errors.push(result.failure.cause); + }); + const remove = Effect.fn("CronRemoveTransactionService.remove")(function* (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly name: string; + }) { + const previousJob = (yield* config.read.pipe( + Effect.mapError((cause) => new CronStateError({ cause })), + )).cronJobs.find((job) => job.name === params.name); + let configChanged = false; + let registrationRemovalAttempted = false; + let workerRemovalAttempted = false; + return yield* Effect.gen(function* () { + registrationRemovalAttempted = true; + yield* registrar + .remove(params.name) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + yield* config + .update((current) => { + return { + ...current, + cronJobs: current.cronJobs.filter((job) => job.name !== params.name), + }; + }) + .pipe(Effect.mapError((cause) => new CronStateError({ cause }))); + configChanged = true; + workerRemovalAttempted = true; + yield* workers.remove(params.workerPath); + return previousJob !== undefined; + }).pipe( + Effect.catch((cause) => + Effect.gen(function* () { + const errors: unknown[] = []; + const shouldRestoreConfig = configChanged && previousJob !== undefined; + if (shouldRestoreConfig) + yield* capture(errors, rollback.restoreRemovedJob(previousJob)); + if (workerRemovalAttempted) { + yield* capture(errors, rollback.restoreWorker(params.snapshot, params.workerPath)); + } + const shouldRestoreRegistration = + registrationRemovalAttempted && previousJob !== undefined; + if (shouldRestoreRegistration) { + yield* capture( + errors, + registrar + .register({ + path: params.workerPath, + schedule: previousJob.schedule, + title: previousJob.name, + }) + .pipe(Effect.mapError((error) => new CronStateError({ cause: error }))), + ); + } + return yield* new CronStateError({ + cause, + ...(errors.length > 0 ? { rollbackErrors: errors } : {}), + }); + }), + ), + ); + }); + return { remove } as const; + }), +}) { + static readonly layer = Layer.effect( + CronRemoveTransactionService, + CronRemoveTransactionService.make, + ); +} + +export class CronStateTransactionService extends Context.Service< + CronStateTransactionService, + { + readonly snapshot: (workerPath: string) => Effect.Effect; + readonly replace: (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly workerSource: string; + readonly job: RigCronJob; + }) => Effect.Effect; + readonly remove: (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly name: string; + }) => Effect.Effect; + readonly restoreReplacedJob: ( + job: RigCronJob, + previousJob?: RigCronJob, + ) => Effect.Effect; + readonly restoreRemovedJob: (previousJob: RigCronJob) => Effect.Effect; + } +>()("@rendotdev/rig/cron/CronStateTransactionService", { + make: Effect.gen(function* () { + const workers = yield* CronWorkerRepositoryService; + const replaceTransaction = yield* CronReplaceTransactionService; + const removeTransaction = yield* CronRemoveTransactionService; + const rollback = yield* CronRollbackService; + const snapshot = Effect.fn("CronStateTransactionService.snapshot")(function* ( + workerPath: string, + ) { + const workerSource = yield* workers.read(workerPath); + return workerSource === undefined ? {} : { workerSource }; + }); + const replace = Effect.fn("CronStateTransactionService.replace")(function* (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly workerSource: string; + readonly job: RigCronJob; + }) { + yield* replaceTransaction.replace(params); + }); + const remove = Effect.fn("CronStateTransactionService.remove")(function* (params: { + readonly snapshot: CronWorkerSnapshot; + readonly workerPath: string; + readonly name: string; + }) { + return yield* removeTransaction.remove(params); + }); + const restoreReplacedJob = Effect.fn("CronStateTransactionService.restoreReplacedJob")( + function* (job: RigCronJob, previousJob?: RigCronJob) { + yield* rollback.restoreReplacedJob(job, previousJob); + }, + ); + const restoreRemovedJob = Effect.fn("CronStateTransactionService.restoreRemovedJob")(function* ( + previousJob: RigCronJob, + ) { + yield* rollback.restoreRemovedJob(previousJob); + }); + return { snapshot, replace, remove, restoreReplacedJob, restoreRemovedJob } as const; + }), +}) { + static readonly layer = Layer.effect( + CronStateTransactionService, + CronStateTransactionService.make, + ); +} + +const cronRollbackLayer = CronRollbackService.layer.pipe(Layer.provide(cronWorkerRepositoryLayer)); + +const cronReplaceTransactionLayer = CronReplaceTransactionService.layer.pipe( + Layer.provide(Layer.mergeAll(cronWorkerRepositoryLayer, cronRollbackLayer, cronRegistrarLayer)), +); + +const cronRemoveTransactionLayer = CronRemoveTransactionService.layer.pipe( + Layer.provide(Layer.mergeAll(cronWorkerRepositoryLayer, cronRollbackLayer, cronRegistrarLayer)), +); + +export const cronStateTransactionLayer = CronStateTransactionService.layer.pipe( + Layer.provide( + Layer.mergeAll( + cronWorkerRepositoryLayer, + cronRollbackLayer, + cronReplaceTransactionLayer, + cronRemoveTransactionLayer, + ), + ), +); diff --git a/src/domains/scheduling/service/scheduling-services.test.ts b/src/domains/scheduling/service/scheduling-services.test.ts new file mode 100644 index 0000000..0bb2672 --- /dev/null +++ b/src/domains/scheduling/service/scheduling-services.test.ts @@ -0,0 +1,293 @@ +import { Effect, Layer } from "effect"; +import { describe, expect, test } from "vite-plus/test"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import { RigConfigError } from "../../settings/types/config-error"; +import type { RigConfig, RigCronJob } from "../../settings/types/config-schema"; +import { CronRegistrarService } from "../repo/cron-registrar"; +import { CronWorkerRepositoryService } from "../repo/cron-worker-repository"; +import { CronStateError } from "../types/cron"; +import { + CronRemoveTransactionService, + CronReplaceTransactionService, + CronRollbackService, + CronStateTransactionService, +} from "./cron-state-transaction"; +import { CronModelService, cronModelLayer } from "./cron-model"; + +type TestState = { + config: RigConfig; + workers: Map; + registerCalls: Array<{ path: string; schedule: string; title: string }>; + removeCalls: string[]; + failRegister: unknown[]; + failRemove: unknown[]; + failWorkerRemove?: unknown; + failConfigUpdates: number[]; + updateCalls: number; +}; + +const emptyConfig: RigConfig = { + version: 1, + baseRegistryDir: "~/rig/tools", + customRegistries: [], + cronJobs: [], +}; + +const createState = (jobs: RigCronJob[] = []): TestState => ({ + config: { ...emptyConfig, cronJobs: jobs }, + workers: new Map(), + registerCalls: [], + removeCalls: [], + failRegister: [], + failRemove: [], + failConfigUpdates: [], + updateCalls: 0, +}); + +const testError = (cause: unknown) => + new RigError({ code: "CRON_ERROR", message: String(cause), details: cause }); + +const configLayer = (state: TestState) => + Layer.succeed(RigConfigStoreService, { + // @effect-diagnostics-next-line effectSucceedWithVoid:off + migrationResult: Effect.succeed(undefined), + acknowledgeMigrationPrompt: Effect.void, + ensure: Effect.sync(() => state.config), + read: Effect.sync(() => state.config), + write: (config) => Effect.sync(() => void (state.config = config)), + update: (mutator) => + Effect.gen(function* () { + state.updateCalls += 1; + const next = yield* Effect.tryPromise({ + try: () => Promise.resolve(mutator(state.config)), + catch: (cause) => new RigConfigError({ operation: "update", cause }), + }); + if (state.failConfigUpdates.includes(state.updateCalls)) { + return yield* new RigConfigError({ + operation: "update", + cause: new Error("config unavailable"), + }); + } + state.config = next; + return next; + }), + }); + +const registrarLayer = (state: TestState) => + Layer.succeed(CronRegistrarService, { + register: (params) => + Effect.suspend(() => { + state.registerCalls.push(params); + const failure = state.failRegister.shift(); + return failure === undefined ? Effect.void : Effect.fail(testError(failure)); + }), + remove: (title) => + Effect.suspend(() => { + state.removeCalls.push(title); + const failure = state.failRemove.shift(); + return failure === undefined ? Effect.void : Effect.fail(testError(failure)); + }), + validate: () => Effect.void, + }); + +const workerRepositoryLayer = (state: TestState) => + Layer.succeed(CronWorkerRepositoryService, { + read: (path) => Effect.succeed(state.workers.get(path)), + write: (path, source) => + Effect.sync(() => { + state.workers.set(path, source); + }), + remove: (path) => + Effect.suspend(() => { + if (state.failWorkerRemove !== undefined) { + return Effect.fail(new CronStateError({ cause: state.failWorkerRemove })); + } + state.workers.delete(path); + return Effect.void; + }), + }); + +const transactionLayer = (state: TestState) => { + const dependencies = Layer.mergeAll( + configLayer(state), + registrarLayer(state), + workerRepositoryLayer(state), + ); + const rollback = CronRollbackService.layer.pipe(Layer.provide(dependencies)); + const replace = CronReplaceTransactionService.layer.pipe( + Layer.provide(Layer.merge(dependencies, rollback)), + ); + const remove = CronRemoveTransactionService.layer.pipe( + Layer.provide(Layer.merge(dependencies, rollback)), + ); + return CronStateTransactionService.layer.pipe( + Layer.provide(Layer.mergeAll(workerRepositoryLayer(state), rollback, replace, remove)), + ); +}; + +const runTransaction = ( + state: TestState, + operation: ( + service: CronStateTransactionService["Service"], + ) => Effect.Effect, +) => + Effect.runPromise( + CronStateTransactionService.use(operation).pipe(Effect.provide(transactionLayer(state))), + ); + +describe("cron model", () => { + test("validates identifiers, parses input, and renders workers", async () => { + const result = await Effect.runPromise( + CronModelService.use((model) => + Effect.gen(function* () { + const target = yield* model.parseCommandTarget("sample.example"); + const input = yield* model.readInput({ input: '{"text":"hello"}' }); + const worker = yield* model.renderWorker({ + name: "weekly", + moduleUrl: new URL("../cli.ts", import.meta.url).href, + }); + return { target, input, worker }; + }), + ).pipe(Effect.provide(cronModelLayer)), + ); + + expect(result.target).toEqual({ id: "sample.example", tool: "sample", command: "example" }); + expect(result.input).toEqual({ text: "hello" }); + expect(result.worker).toContain('"cron"'); + expect(result.worker).toContain('"weekly"'); + }); + + test("rejects invalid names, targets, and conflicting input sources", async () => { + const effects = await Effect.runPromise( + CronModelService.use((model) => + Effect.succeed([ + model.parseJobName("bad name"), + model.parseCommandTarget("missing-separator"), + model.readInput({ input: "{}", inputFile: "input.json" }), + ] as const), + ).pipe(Effect.provide(cronModelLayer)), + ); + + await expect(Effect.runPromise(effects[0])).rejects.toThrow("Cron job names may only contain"); + await expect(Effect.runPromise(effects[1])).rejects.toThrow("Command id must use"); + await expect(Effect.runPromise(effects[2])).rejects.toThrow("Use --input or --input-file"); + }); +}); + +describe("cron state transactions", () => { + test("restores config and worker state when registration fails", async () => { + const state = createState(); + state.workers.set("/cron/weekly.ts", "previous worker"); + state.failRegister.push(new Error("launchd unavailable")); + + await expect( + runTransaction(state, (transaction) => + transaction.replace({ + snapshot: { workerSource: "previous worker" }, + workerPath: "/cron/weekly.ts", + workerSource: "replacement worker", + job: { name: "weekly", command: "sample.example", schedule: "@weekly" }, + }), + ), + ).rejects.toMatchObject({ _tag: "CronStateError" }); + + expect(state.config.cronJobs).toEqual([]); + expect(state.workers.get("/cron/weekly.ts")).toBe("previous worker"); + expect(state.removeCalls).toEqual(["weekly"]); + }); + + test("restores a removed job, worker, and registration after a local failure", async () => { + const job = { name: "weekly", command: "sample.example", schedule: "@weekly" }; + const state = createState([job]); + state.workers.set("/cron/weekly.ts", "worker"); + state.failWorkerRemove = new Error("disk unavailable"); + + await expect( + runTransaction(state, (transaction) => + transaction.remove({ + snapshot: { workerSource: "worker" }, + workerPath: "/cron/weekly.ts", + name: "weekly", + }), + ), + ).rejects.toMatchObject({ _tag: "CronStateError" }); + + expect(state.config.cronJobs).toEqual([job]); + expect(state.workers.get("/cron/weekly.ts")).toBe("worker"); + expect(state.registerCalls).toEqual([ + { path: "/cron/weekly.ts", schedule: "@weekly", title: "weekly" }, + ]); + }); + + test("restores registration when config removal fails after unregistering", async () => { + const job = { name: "weekly", command: "sample.example", schedule: "@weekly" }; + const state = createState([job]); + state.workers.set("/cron/weekly.ts", "worker"); + state.failConfigUpdates.push(1); + + await expect( + runTransaction(state, (transaction) => + transaction.remove({ + snapshot: { workerSource: "worker" }, + workerPath: "/cron/weekly.ts", + name: "weekly", + }), + ), + ).rejects.toMatchObject({ _tag: "CronStateError" }); + + expect(state.config.cronJobs).toEqual([job]); + expect(state.workers.get("/cron/weekly.ts")).toBe("worker"); + expect(state.removeCalls).toEqual(["weekly"]); + expect(state.registerCalls).toEqual([ + { path: "/cron/weekly.ts", schedule: "@weekly", title: "weekly" }, + ]); + }); + + test("reports rollback failures without hiding the original failure", async () => { + const previous = { name: "weekly", command: "sample.example", schedule: "@weekly" }; + const state = createState([previous]); + state.workers.set("/cron/weekly.ts", "previous worker"); + state.failRegister.push(new Error("replacement unavailable"), new Error("restore unavailable")); + + await expect( + runTransaction(state, (transaction) => + transaction.replace({ + snapshot: { workerSource: "previous worker" }, + workerPath: "/cron/weekly.ts", + workerSource: "replacement worker", + job: { ...previous, schedule: "@daily" }, + }), + ), + ).rejects.toMatchObject({ + _tag: "CronStateError", + rollbackErrors: [expect.objectContaining({ message: "Error: restore unavailable" })], + }); + expect(state.config.cronJobs).toEqual([previous]); + expect(state.workers.get("/cron/weekly.ts")).toBe("previous worker"); + }); + + test("keeps newer config state during optimistic rollback", async () => { + const newer = { name: "weekly", command: "sample.example", schedule: "@daily" }; + const failed = { ...newer, schedule: "@weekly" }; + const state = createState([newer]); + + await runTransaction(state, (transaction) => + transaction.restoreReplacedJob(failed, { ...failed, schedule: "@monthly" }), + ); + + expect(state.config.cronJobs).toEqual([newer]); + }); + + test("returns empty snapshots and false removals for missing jobs", async () => { + const state = createState(); + expect( + await runTransaction(state, (transaction) => transaction.snapshot("/cron/missing.ts")), + ).toEqual({}); + expect( + await runTransaction(state, (transaction) => + transaction.remove({ snapshot: {}, workerPath: "/cron/missing.ts", name: "missing" }), + ), + ).toBe(false); + }); +}); diff --git a/src/domains/scheduling/types/cron.ts b/src/domains/scheduling/types/cron.ts new file mode 100644 index 0000000..742cdb9 --- /dev/null +++ b/src/domains/scheduling/types/cron.ts @@ -0,0 +1,31 @@ +import { Schema } from "effect"; +import type { RigCronJob } from "../../settings/types/config-schema"; + +export type CronAddOptions = Readonly<{ + name: string; + command: string; + schedule: string; + input?: string; + inputFile?: string; + moduleUrl: string; +}>; + +export type CronRunResult = Readonly<{ + job: RigCronJob; + envelope: unknown; + exitCode: number; +}>; + +export type CronWorkerSnapshot = Readonly<{ workerSource?: string }>; + +const RigCronOperation = Schema.Literals(["list", "add", "remove", "run"]); + +export class RigCronError extends Schema.TaggedErrorClass()("RigCronError", { + operation: RigCronOperation, + cause: Schema.Defect(), +}) {} + +export class CronStateError extends Schema.TaggedErrorClass()("CronStateError", { + cause: Schema.Defect(), + rollbackErrors: Schema.optional(Schema.Array(Schema.Defect())), +}) {} diff --git a/src/domains/settings/config/config-defaults.test.ts b/src/domains/settings/config/config-defaults.test.ts index c0cd6a4..1a61304 100644 --- a/src/domains/settings/config/config-defaults.test.ts +++ b/src/domains/settings/config/config-defaults.test.ts @@ -1,15 +1,17 @@ -import { describe, expect, test } from "vite-plus/test"; -import { - RigConfigDefaultsClass, - RigConfigDefaultsSingleton, - rigConfigDefaults, -} from "./config-defaults.ts"; +import { Effect } from "effect"; +import { describe, expect, it } from "vite-plus/test"; +import { RigConfigDefaultsService } from "./config-defaults"; -describe("Rig config defaults", () => { - test("builds fresh config arrays for every create operation", () => { - const Defaults = RigConfigDefaultsSingleton; - const first = Defaults.create({}); - const second = Defaults.create({}); +describe("RigConfigDefaultsService", () => { + it("creates independent default config values for each layer instance", async () => { + const load = Effect.gen(function* () { + return yield* (yield* RigConfigDefaultsService).get; + }).pipe(Effect.provide(RigConfigDefaultsService.layer), Effect.runPromise); + + const first = await load; + const second = await Effect.gen(function* () { + return yield* (yield* RigConfigDefaultsService).get; + }).pipe(Effect.provide(RigConfigDefaultsService.layer), Effect.runPromise); expect(first).toEqual({ version: 1, @@ -17,23 +19,8 @@ describe("Rig config defaults", () => { customRegistries: [], cronJobs: [], }); + expect(first).not.toBe(second); expect(first.customRegistries).not.toBe(second.customRegistries); expect(first.cronJobs).not.toBe(second.cronJobs); - - first.customRegistries.push("~/custom"); - first.cronJobs.push({ name: "daily", command: "tool.run", schedule: "@daily" }); - expect(second.customRegistries).toEqual([]); - expect(second.cronJobs).toEqual([]); - expect(RigConfigDefaultsSingleton.create({}).cronJobs).toEqual([]); - }); - - test("preserves the legacy constructor and shared defaults adapter", () => { - const defaults = new RigConfigDefaultsClass(); - - expect(defaults).toBeInstanceOf(RigConfigDefaultsClass); - expect(rigConfigDefaults).toBeInstanceOf(RigConfigDefaultsClass); - expect(defaults.create()).toEqual(RigConfigDefaultsSingleton.create({})); - expect(defaults.create().customRegistries).not.toBe(defaults.create().customRegistries); - expect(rigConfigDefaults.create().cronJobs).not.toBe(rigConfigDefaults.create().cronJobs); }); }); diff --git a/src/domains/settings/config/config-defaults.ts b/src/domains/settings/config/config-defaults.ts index f76347f..4bda555 100644 --- a/src/domains/settings/config/config-defaults.ts +++ b/src/domains/settings/config/config-defaults.ts @@ -1,40 +1,22 @@ -import { defineSingleton } from "../../../define.ts"; -import type { RigConfig } from "../types/config-schema.ts"; +import { Context, Effect, Layer } from "effect"; +import type { RigConfig } from "../types/config-schema"; -export const RigConfigDefaultsSingleton = defineSingleton({ - params: { baseRegistryDir: "~/rig/tools" }, - deps: {}, - create(params: Record): RigConfig { - void params; - return { - version: 1, - baseRegistryDir: this.params.baseRegistryDir, +export class RigConfigDefaultsService extends Context.Service< + RigConfigDefaultsService, + { + readonly get: Effect.Effect; + } +>()("@rendotdev/rig/settings/RigConfigDefaultsService", { + make: Effect.gen(function* () { + const get = Effect.succeed({ + version: 1 as const, + baseRegistryDir: "~/rig/tools", customRegistries: [], cronJobs: [], - }; - }, -}); + } satisfies RigConfig).pipe(Effect.withSpan("RigConfigDefaultsService.get")); -export type RigConfigDefaultsClass = { - create(): RigConfig; -}; - -type RigConfigDefaultsConstructor = { - new (): RigConfigDefaultsClass; - readonly prototype: RigConfigDefaultsClass; -}; - -function RigConfigDefaultsConstructorAdapter(this: RigConfigDefaultsClass) { - Object.defineProperty(this, "create", { - configurable: true, - value: function create() { - return RigConfigDefaultsSingleton.create({}); - }, - writable: true, - }); + return { get } as const; + }), +}) { + static readonly layer = Layer.effect(RigConfigDefaultsService, RigConfigDefaultsService.make); } - -export const RigConfigDefaultsClass = - RigConfigDefaultsConstructorAdapter as unknown as RigConfigDefaultsConstructor; - -export const rigConfigDefaults = new RigConfigDefaultsClass(); diff --git a/src/domains/settings/config/directory-migration.ts b/src/domains/settings/config/directory-migration.ts new file mode 100644 index 0000000..e2e1be3 --- /dev/null +++ b/src/domains/settings/config/directory-migration.ts @@ -0,0 +1 @@ +export const RigHomeDirectoryMigrationPromptId = "v0.0.19-home-directory"; diff --git a/src/domains/settings/config/index.ts b/src/domains/settings/config/index.ts deleted file mode 100644 index 08781c9..0000000 --- a/src/domains/settings/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./config-defaults.ts"; diff --git a/src/domains/settings/index.ts b/src/domains/settings/index.ts deleted file mode 100644 index 86d5e96..0000000 --- a/src/domains/settings/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from "./config/index.ts"; -export * from "./repo/index.ts"; -export * from "./service/index.ts"; -export * from "./types/index.ts"; diff --git a/src/domains/settings/repo/config-codec.ts b/src/domains/settings/repo/config-codec.ts new file mode 100644 index 0000000..78c92a6 --- /dev/null +++ b/src/domains/settings/repo/config-codec.ts @@ -0,0 +1,46 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigConfigSchema, type RigConfig } from "../types/config-schema"; + +export class RigConfigCodecService extends Context.Service< + RigConfigCodecService, + { + readonly decode: (raw: string, path: string) => Effect.Effect; + readonly validate: (config: RigConfig) => Effect.Effect; + } +>()("@rendotdev/rig/settings/RigConfigCodecService", { + make: Effect.gen(function* () { + const decode = Effect.fn("RigConfigCodecService.decode")(function* (raw: string, path: string) { + const parsed = yield* Effect.try({ + try: () => JSON.parse(raw) as unknown, + catch: (error) => + new RigError({ + code: "CONFIG_INVALID", + message: `Config is not valid JSON at ${path}.`, + details: { error }, + }), + }); + const result = RigConfigSchema.safeParse(parsed); + if (result.success) return result.data; + return yield* new RigError({ + code: "CONFIG_INVALID", + message: "Rig config is invalid.", + details: result.error.flatten(), + }); + }); + const validate = Effect.fn("RigConfigCodecService.validate")(function* (config: RigConfig) { + const result = RigConfigSchema.safeParse(config); + if (result.success) return result.data; + return yield* new RigError({ + code: "CONFIG_INVALID", + message: "Rig config is invalid.", + details: result.error.flatten(), + }); + }); + return { decode, validate } as const; + }), +}) { + static readonly layer = Layer.effect(RigConfigCodecService, RigConfigCodecService.make); +} + +export const rigConfigCodecLayer = RigConfigCodecService.layer; diff --git a/src/domains/settings/repo/config-repository.ts b/src/domains/settings/repo/config-repository.ts new file mode 100644 index 0000000..09811b5 --- /dev/null +++ b/src/domains/settings/repo/config-repository.ts @@ -0,0 +1,116 @@ +import { existsSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { + AtomicFileWriterService, + atomicFileWriterLayer, +} from "../../../providers/filesystem/atomic-file-writer"; +import { FileLockService, fileLockLayer } from "../../../providers/filesystem/file-lock"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + rigPathsConfigLayer, +} from "../../../providers/paths/rig-paths"; +import { RigConfigDefaultsService } from "../config/config-defaults"; +import { RigConfigError, type RigConfigOperation } from "../types/config-error"; +import type { RigConfig } from "../types/config-schema"; +import type { RigConfigMutator } from "../types/config-store"; +import { RigConfigCodecService, rigConfigCodecLayer } from "./config-codec"; + +export class RigConfigRepositoryService extends Context.Service< + RigConfigRepositoryService, + { + readonly initialize: Effect.Effect; + readonly read: Effect.Effect; + readonly write: (config: RigConfig) => Effect.Effect; + readonly update: (mutator: RigConfigMutator) => Effect.Effect; + } +>()("@rendotdev/rig/settings/RigConfigRepositoryService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const locks = yield* FileLockService; + const writer = yield* AtomicFileWriterService; + const defaults = yield* RigConfigDefaultsService; + const codec = yield* RigConfigCodecService; + const mapError = (operation: RigConfigOperation) => (cause: unknown) => + new RigConfigError({ operation, cause }); + const readUnlocked = Effect.tryPromise({ + try: () => readFile(paths.configPath, "utf8"), + catch: mapError("read"), + }).pipe( + Effect.flatMap((raw) => + codec.decode(raw, paths.configPath).pipe(Effect.mapError(mapError("read"))), + ), + Effect.withSpan("RigConfigRepositoryService.readUnlocked"), + ); + const writeUnlocked = Effect.fn("RigConfigRepositoryService.writeUnlocked")( + function* (config: RigConfig) { + const valid = yield* codec.validate(config); + yield* writer.write(paths.configPath, `${JSON.stringify(valid, null, 2)}\n`); + return valid; + }, + Effect.mapError(mapError("write")), + ); + const read = readUnlocked.pipe(Effect.withSpan("RigConfigRepositoryService.read")); + const initialize = locks + .withLock({ + targetPath: paths.configPath, + operation: () => + existsSync(paths.configPath) + ? readUnlocked + : defaults.get.pipe(Effect.flatMap(writeUnlocked)), + }) + .pipe( + Effect.mapError(mapError("ensure")), + Effect.withSpan("RigConfigRepositoryService.initialize"), + ); + const write = Effect.fn("RigConfigRepositoryService.write")(function* (config: RigConfig) { + yield* locks + .withLock({ + targetPath: paths.configPath, + operation: () => writeUnlocked(config), + }) + .pipe(Effect.mapError(mapError("write"))); + }); + const update = Effect.fn("RigConfigRepositoryService.update")(function* ( + mutator: RigConfigMutator, + ) { + return yield* locks + .withLock({ + targetPath: paths.configPath, + operation: () => + Effect.gen(function* () { + const current = existsSync(paths.configPath) + ? yield* readUnlocked + : yield* defaults.get; + const next = yield* Effect.tryPromise({ + try: () => Promise.resolve(mutator(current)), + catch: mapError("update"), + }); + return yield* writeUnlocked(next).pipe(Effect.mapError(mapError("update"))); + }), + }) + .pipe(Effect.mapError(mapError("update"))); + }); + return { initialize, read, write, update } as const; + }), +}) { + static readonly layer = Layer.effect(RigConfigRepositoryService, RigConfigRepositoryService.make); +} + +export const rigConfigRepositoryLayer: Layer.Layer< + RigConfigRepositoryService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = RigConfigRepositoryService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + fileLockLayer, + atomicFileWriterLayer, + RigConfigDefaultsService.layer, + rigConfigCodecLayer, + ), + ), +); diff --git a/src/domains/settings/repo/directory-migration.ts b/src/domains/settings/repo/directory-migration.ts new file mode 100644 index 0000000..72073d9 --- /dev/null +++ b/src/domains/settings/repo/directory-migration.ts @@ -0,0 +1,145 @@ +import { existsSync } from "node:fs"; +import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + RigPathsService, + rigPathsConfigLayer, + rigPathsLayer, +} from "../../../providers/paths/rig-paths"; +import { DirectoryMigrationError } from "../types/directory-migration"; + +export class DirectoryMigrationInspectionService extends Context.Service< + DirectoryMigrationInspectionService, + { + readonly directoryExists: (path: string) => Effect.Effect; + readonly visibleEntries: (path: string) => Effect.Effect; + readonly hasDefaultConfig: Effect.Effect; + } +>()("@rendotdev/rig/settings/DirectoryMigrationInspectionService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const attempt = (run: () => Promise) => + Effect.tryPromise({ + try: run, + catch: (cause) => new DirectoryMigrationError({ operation: "migrate", cause }), + }); + const directoryExists = Effect.fn("DirectoryMigrationInspectionService.directoryExists")( + function* (path: string) { + return yield* Effect.promise(async () => { + try { + return (await stat(path)).isDirectory(); + } catch { + return false; + } + }); + }, + ); + const visibleEntries = Effect.fn("DirectoryMigrationInspectionService.visibleEntries")( + function* (path: string) { + return (yield* attempt(() => readdir(path))).filter((entry) => entry !== ".DS_Store"); + }, + ); + const hasDefaultConfig = Effect.promise(async () => { + try { + const parsed = JSON.parse(await readFile(paths.configPath, "utf8")) as Record< + string, + unknown + >; + return ( + parsed.version === 1 && + parsed.baseRegistryDir === paths.defaultBaseRegistryDir && + Array.isArray(parsed.customRegistries) && + parsed.customRegistries.length === 0 && + (parsed.cronJobs === undefined || + (Array.isArray(parsed.cronJobs) && parsed.cronJobs.length === 0)) + ); + } catch { + return false; + } + }).pipe(Effect.withSpan("DirectoryMigrationInspectionService.hasDefaultConfig")); + return { directoryExists, visibleEntries, hasDefaultConfig } as const; + }), +}) { + static readonly layer = Layer.effect( + DirectoryMigrationInspectionService, + DirectoryMigrationInspectionService.make, + ); +} + +export class DirectoryMigrationRepositoryService extends Context.Service< + DirectoryMigrationRepositoryService, + { + readonly hasLegacyState: Effect.Effect; + readonly currentExists: Effect.Effect; + readonly migrate: Effect.Effect; + } +>()("@rendotdev/rig/settings/DirectoryMigrationRepositoryService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const pathOperations = yield* RigPathsService; + const isLegacyBaseRegistry = Effect.fn( + "DirectoryMigrationRepositoryService.isLegacyBaseRegistry", + )(function* (value: unknown) { + return ( + typeof value === "string" && + (value === paths.legacyDefaultBaseRegistryDir || + (yield* pathOperations.resolve(value)) === join(paths.legacyRigDir, "tools")) + ); + }); + const rewriteMigratedConfig = Effect.fn( + "DirectoryMigrationRepositoryService.rewriteMigratedConfig", + )(function* () { + if (!existsSync(paths.configPath)) return false; + const parsed = yield* Effect.tryPromise({ + try: async () => + JSON.parse(await readFile(paths.configPath, "utf8")) as Record, + catch: (cause) => new DirectoryMigrationError({ operation: "migrate", cause }), + }); + if (!(yield* isLegacyBaseRegistry(parsed.baseRegistryDir))) return false; + parsed.baseRegistryDir = paths.defaultBaseRegistryDir; + yield* Effect.tryPromise({ + try: () => writeFile(paths.configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8"), + catch: (cause) => new DirectoryMigrationError({ operation: "migrate", cause }), + }); + return true; + }); + const hasLegacyState = Effect.sync( + () => + existsSync(paths.legacyRigDir) && + (existsSync(join(paths.legacyRigDir, "rig.json")) || + existsSync(join(paths.legacyRigDir, "tools"))), + ).pipe(Effect.withSpan("DirectoryMigrationRepositoryService.hasLegacyState")); + const currentExists = Effect.sync(() => existsSync(paths.rigDir)).pipe( + Effect.withSpan("DirectoryMigrationRepositoryService.currentExists"), + ); + const migrate = Effect.gen(function* () { + yield* Effect.tryPromise({ + try: async () => { + if (existsSync(paths.rigDir)) await rm(paths.rigDir, { recursive: true, force: true }); + await mkdir(paths.homeDir, { recursive: true }); + await rename(paths.legacyRigDir, paths.rigDir); + }, + catch: (cause) => new DirectoryMigrationError({ operation: "migrate", cause }), + }); + return yield* rewriteMigratedConfig(); + }).pipe(Effect.withSpan("DirectoryMigrationRepositoryService.migrate")); + return { hasLegacyState, currentExists, migrate } as const; + }), +}) { + static readonly layer = Layer.effect( + DirectoryMigrationRepositoryService, + DirectoryMigrationRepositoryService.make, + ); +} + +export const directoryMigrationRepositoryLayer: Layer.Layer< + DirectoryMigrationRepositoryService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = DirectoryMigrationRepositoryService.layer.pipe( + Layer.provide(Layer.merge(rigPathsConfigLayer, rigPathsLayer)), +); diff --git a/src/domains/settings/repo/file-lock.test.ts b/src/domains/settings/repo/file-lock.test.ts deleted file mode 100644 index 79a0795..0000000 --- a/src/domains/settings/repo/file-lock.test.ts +++ /dev/null @@ -1,334 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { existsSync } from "node:fs"; -import { - mkdir, - mkdtemp, - readFile, - readdir, - rename, - rm, - stat, - utimes, - writeFile, -} from "node:fs/promises"; -import { hostname, tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { - AtomicFileWriterClass, - AtomicFileWriter, - AtomicFileWriterRepo, - BoundedFileLockClass, - BoundedFileLock, - BoundedFileLockRepo, - FileSystemErrorsSingleton, - type AtomicFileWriterDeps, - type BoundedFileLockDeps, -} from "./file-lock"; - -// ─── Module-level test helpers ──────────────────────────────────────────────── - -function testGetProcessPid(): number { - return process.pid; -} - -function testKillProcess(pid: number, signal: number): void { - process.kill(pid, signal); -} - -function testNow(): number { - return Date.now(); -} - -function testTimestamp(): string { - return new Date().toISOString(); -} - -function testSleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -async function noopSleep(): Promise {} - -async function throwRenameError(): Promise { - throw new Error("rename failed"); -} - -// ─── Shared test infrastructure ─────────────────────────────────────────────── - -class LockTestDirectories { - private readonly paths: string[] = []; - - async create(): Promise { - const path = await mkdtemp(join(tmpdir(), "rig-file-lock-test-")); - this.paths.push(path); - return path; - } - - async cleanup(): Promise { - await Promise.all( - this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), - ); - } -} - -const directories = new LockTestDirectories(); - -afterEach(async () => { - await directories.cleanup(); -}); - -async function createStaleLock(target: string, ownerSource: string): Promise { - const lockPath = `${target}.lock`; - await mkdir(lockPath, { recursive: true }); - await writeFile(join(lockPath, "owner.json"), ownerSource, "utf8"); - await utimes(lockPath, new Date(0), new Date(0)); - return lockPath; -} - -function makeProductionLockDeps(overrides?: Partial): BoundedFileLockDeps { - return { - mkdir: mkdir as unknown as BoundedFileLockDeps["mkdir"], - readFile: readFile as unknown as BoundedFileLockDeps["readFile"], - rename: rename as unknown as BoundedFileLockDeps["rename"], - rm: rm as unknown as BoundedFileLockDeps["rm"], - stat: stat as unknown as BoundedFileLockDeps["stat"], - writeFile: writeFile as unknown as BoundedFileLockDeps["writeFile"], - dirname, - join, - randomUUID, - hostname, - getProcessPid: testGetProcessPid, - killProcess: testKillProcess, - now: testNow, - timestamp: testTimestamp, - sleep: testSleep, - fsErrors: FileSystemErrorsSingleton, - ...overrides, - }; -} - -// ─── FileSystemErrorsSingleton ──────────────────────────────────────────────── - -describe("FileSystemErrorsSingleton", () => { - test("identifies ENOENT errors as missing", () => { - const e = Object.assign(new Error("ENOENT"), { code: "ENOENT" }); - expect(FileSystemErrorsSingleton.isMissing({ error: e })).toBe(true); - expect(FileSystemErrorsSingleton.isExisting({ error: e })).toBe(false); - expect(FileSystemErrorsSingleton.isProcessMissing({ error: e })).toBe(false); - }); - - test("identifies EEXIST errors as existing", () => { - const e = Object.assign(new Error("EEXIST"), { code: "EEXIST" }); - expect(FileSystemErrorsSingleton.isExisting({ error: e })).toBe(true); - expect(FileSystemErrorsSingleton.isMissing({ error: e })).toBe(false); - }); - - test("identifies ESRCH errors as process missing", () => { - const e = Object.assign(new Error("ESRCH"), { code: "ESRCH" }); - expect(FileSystemErrorsSingleton.isProcessMissing({ error: e })).toBe(true); - expect(FileSystemErrorsSingleton.isMissing({ error: e })).toBe(false); - }); - - test("returns false for non-errors and errors without a matching code", () => { - expect(FileSystemErrorsSingleton.isMissing({ error: null })).toBe(false); - expect(FileSystemErrorsSingleton.isMissing({ error: "ENOENT" })).toBe(false); - expect(FileSystemErrorsSingleton.isMissing({ error: 42 })).toBe(false); - expect(FileSystemErrorsSingleton.isMissing({ error: new Error("no code") })).toBe(false); - }); -}); - -// ─── BoundedFileLockRepo ────────────────────────────────────────────────────── - -describe("BoundedFileLockRepo", () => { - test("create returns a lock resource whose run uses named params", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lock = BoundedFileLock.create({ targetPath: target }); - - const result = await lock.run({ operation: () => "done" }); - expect(result).toBe("done"); - expect(existsSync(`${target}.lock`)).toBe(false); - }); - - test("create accepts per-lock options and releases correctly", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lock = BoundedFileLock.create({ targetPath: target, options: { retryMs: 5 } }); - - await lock.run({ operation: () => undefined }); - expect(existsSync(`${target}.lock`)).toBe(false); - }); - - test("create returns independent resources for different target paths", async () => { - const directory = await directories.create(); - const lock1 = BoundedFileLock.create({ targetPath: join(directory, "a.json") }); - const lock2 = BoundedFileLock.create({ targetPath: join(directory, "b.json") }); - - const [r1, r2] = await Promise.all([ - lock1.run({ operation: () => "a" }), - lock2.run({ operation: () => "b" }), - ]); - - expect(r1).toBe("a"); - expect(r2).toBe("b"); - }); - - test("builder injects clock and sleep to produce a deterministic timeout", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lockPath = `${target}.lock`; - await mkdir(lockPath); - - let tick = 0; - const Service = new BoundedFileLockRepo({ - params: { timeoutMs: 50, staleMs: 30_000, retryMs: 0 }, - deps: makeProductionLockDeps({ - now: function now() { - tick += 60; - return tick; - }, - sleep: noopSleep, - }), - }); - - const lock = Service.create({ targetPath: target }); - await expect(lock.run({ operation: () => "done" })).rejects.toThrow(/Timed out/); - - await rm(lockPath, { recursive: true, force: true }); - }); -}); - -// ─── AtomicFileWriterRepo ───────────────────────────────────────────────────── - -describe("AtomicFileWriterRepo", () => { - test("write accepts named params and creates nested directories", async () => { - const directory = await directories.create(); - const target = join(directory, "nested", "sub", "state.json"); - - await AtomicFileWriter.write({ path: target, content: "hello\n" }); - expect(await readFile(target, "utf8")).toBe("hello\n"); - }); - - test("builder calls the rm dep when rename throws", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - let cleanedPath: string | undefined; - - const Service = new AtomicFileWriterRepo({ - params: {}, - deps: { - mkdir: mkdir as unknown as AtomicFileWriterDeps["mkdir"], - writeFile: writeFile as unknown as AtomicFileWriterDeps["writeFile"], - rename: throwRenameError, - rm: async (path: string): Promise => { - cleanedPath = path; - }, - dirname, - randomUUID, - getProcessPid: testGetProcessPid, - }, - }); - - await expect(Service.write({ path: target, content: "data\n" })).rejects.toThrow( - "rename failed", - ); - expect(cleanedPath?.startsWith(target)).toBe(true); - expect(cleanedPath?.includes(".tmp-")).toBe(true); - }); -}); - -// ─── Adapter compatibility: bounded file lock ───────────────────────────────── - -describe("bounded file lock", () => { - test("releases after operation failures and missing lock directories", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lockPath = `${target}.lock`; - const lock = new BoundedFileLockClass(target); - - await expect( - lock.run(() => { - throw new Error("operation failed"); - }), - ).rejects.toThrow("operation failed"); - expect(existsSync(lockPath)).toBe(false); - - await expect( - lock.run(async () => { - await rm(lockPath, { recursive: true }); - }), - ).resolves.toBeUndefined(); - }); - - test("does not release a lock whose ownership token changed", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lockPath = `${target}.lock`; - - await new BoundedFileLockClass(target).run(async () => { - await writeFile( - join(lockPath, "owner.json"), - `${JSON.stringify({ token: "replacement" })}\n`, - "utf8", - ); - }); - - expect(existsSync(lockPath)).toBe(true); - await rm(lockPath, { recursive: true }); - }); - - test("propagates malformed lease metadata during release", async () => { - const directory = await directories.create(); - const target = join(directory, "state.json"); - const lockPath = `${target}.lock`; - - await expect( - new BoundedFileLockClass(target).run(async () => { - await writeFile(join(lockPath, "owner.json"), "not json\n", "utf8"); - }), - ).rejects.toThrow(/JSON|Unexpected token/); - expect(existsSync(lockPath)).toBe(true); - }); - - test("recovers stale locks with malformed, incomplete, and remote owners", async () => { - const directory = await directories.create(); - const owners = [ - "not json\n", - `${JSON.stringify({ token: "missing-fields" })}\n`, - `${JSON.stringify({ - token: "remote", - pid: process.pid, - hostname: `${hostname()}-remote`, - acquiredAt: new Date(0).toISOString(), - })}\n`, - ]; - - await Promise.all( - owners.map(async (owner, index) => { - const target = join(directory, `state-${index}.json`); - const lockPath = await createStaleLock(target, owner); - await new BoundedFileLockClass(target, { staleMs: 1 }).run(() => undefined); - expect(existsSync(lockPath)).toBe(false); - }), - ); - }); -}); - -// ─── Adapter compatibility: atomic file writer ──────────────────────────────── - -describe("atomic file writer", () => { - test("atomically replaces files and cleans temporary files after rename failures", async () => { - const directory = await directories.create(); - const writer = new AtomicFileWriterClass(); - const target = join(directory, "state.json"); - await writer.write(target, "first\n"); - await writer.write(target, "second\n"); - expect(await readFile(target, "utf8")).toBe("second\n"); - - const directoryTarget = join(directory, "cannot-replace"); - await mkdir(directoryTarget); - await expect(writer.write(directoryTarget, "content\n")).rejects.toThrow(/rename|directory/); - expect((await readdir(directory)).filter((entry) => entry.includes(".tmp-"))).toEqual([]); - }); -}); diff --git a/src/domains/settings/repo/file-lock.ts b/src/domains/settings/repo/file-lock.ts deleted file mode 100644 index 774e30f..0000000 --- a/src/domains/settings/repo/file-lock.ts +++ /dev/null @@ -1,354 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { hostname } from "node:os"; -import { dirname, join } from "node:path"; -import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises"; -import { defineRepo, defineSingleton } from "../../../define.ts"; -import { RigErrorClass } from "../../../providers/errors/index.ts"; - -export type FileLockOptions = { - timeoutMs?: number; - staleMs?: number; - retryMs?: number; -}; - -type LockOwner = { - token: string; - pid: number; - hostname: string; - acquiredAt: string; -}; - -// ─── Module-level dep implementations ──────────────────────────────────────── - -function hasCode(error: unknown, code: string): boolean { - return ( - typeof error === "object" && - error !== null && - "code" in error && - (error as { code?: unknown }).code === code - ); -} - -function isMissing(params: { error: unknown }): boolean { - return hasCode(params.error, "ENOENT"); -} - -function isExisting(params: { error: unknown }): boolean { - return hasCode(params.error, "EEXIST"); -} - -function isProcessMissing(params: { error: unknown }): boolean { - return hasCode(params.error, "ESRCH"); -} - -function getProcessPid(): number { - return process.pid; -} - -function killProcess(pid: number, signal: number): void { - process.kill(pid, signal); -} - -function now(): number { - return Date.now(); -} - -function timestamp(): string { - return new Date().toISOString(); -} - -function sleep(milliseconds: number): Promise { - return new Promise((resolve) => setTimeout(resolve, milliseconds)); -} - -// ─── FileSystemErrorsSingleton ──────────────────────────────────────────────── - -export const FileSystemErrorsSingleton = defineSingleton({ - params: {}, - deps: {}, - isMissing, - isExisting, - isProcessMissing, -}); - -// ─── BoundedFileLockRepo ────────────────────────────────────────────────────── - -export type BoundedFileLockDeps = { - mkdir: (path: string, options?: { recursive?: boolean }) => Promise; - readFile: (path: string, encoding: "utf8") => Promise; - rename: (oldPath: string, newPath: string) => Promise; - rm: (path: string, options?: { recursive?: boolean; force?: boolean }) => Promise; - stat: (path: string) => Promise<{ mtimeMs: number }>; - writeFile: (path: string, content: string, encoding: "utf8") => Promise; - dirname: (path: string) => string; - join: (...parts: string[]) => string; - randomUUID: () => string; - hostname: () => string; - getProcessPid: () => number; - killProcess: (pid: number, signal: number) => void; - now: () => number; - timestamp: () => string; - sleep: (ms: number) => Promise; - fsErrors: typeof FileSystemErrorsSingleton; -}; - -export class BoundedFileLockRepo extends defineRepo({ - params: { - timeoutMs: 5_000, - staleMs: 30_000, - retryMs: 20, - }, - deps: { - mkdir: mkdir as unknown as BoundedFileLockDeps["mkdir"], - readFile: readFile as unknown as BoundedFileLockDeps["readFile"], - rename: rename as unknown as BoundedFileLockDeps["rename"], - rm: rm as unknown as BoundedFileLockDeps["rm"], - stat: stat as unknown as BoundedFileLockDeps["stat"], - writeFile: writeFile as unknown as BoundedFileLockDeps["writeFile"], - dirname, - join, - randomUUID: randomUUID as unknown as BoundedFileLockDeps["randomUUID"], - hostname, - getProcessPid, - killProcess, - now, - timestamp, - sleep, - fsErrors: FileSystemErrorsSingleton, - } as BoundedFileLockDeps, -}) { - public create(params: { targetPath: string; options?: FileLockOptions }) { - const deps = this.deps; - const lockPath = `${params.targetPath}.lock`; - const timeoutMs = params.options?.timeoutMs ?? this.params.timeoutMs; - const staleMs = params.options?.staleMs ?? this.params.staleMs; - const retryMs = params.options?.retryMs ?? this.params.retryMs; - - async function releaseLease(owner: LockOwner): Promise { - try { - const current = JSON.parse( - await deps.readFile(deps.join(lockPath, "owner.json"), "utf8"), - ) as { token?: unknown }; - if (current.token !== owner.token) return; - await deps.rm(lockPath, { recursive: true, force: true }); - } catch (error) { - if (deps.fsErrors.isMissing({ error })) return; - throw error; - } - } - - async function tryAcquire(): Promise { - const owner: LockOwner = { - token: deps.randomUUID(), - pid: deps.getProcessPid(), - hostname: deps.hostname(), - acquiredAt: deps.timestamp(), - }; - try { - await deps.mkdir(lockPath); - } catch (error) { - /* v8 ignore else -- lock contention is the only recoverable mkdir failure */ - if (deps.fsErrors.isExisting({ error })) return undefined; - /* v8 ignore next */ - throw error; - } - - try { - await deps.writeFile( - deps.join(lockPath, "owner.json"), - `${JSON.stringify(owner)}\n`, - "utf8", - ); - return owner; - } catch (error) { - /* v8 ignore start -- owner writes can only fail through platform I/O faults */ - await deps.rm(lockPath, { recursive: true, force: true }); - throw error; - /* v8 ignore stop */ - } - } - - async function readOwner(): Promise { - try { - const parsed = JSON.parse( - await deps.readFile(deps.join(lockPath, "owner.json"), "utf8"), - ) as Partial; - if ( - typeof parsed.token !== "string" || - typeof parsed.pid !== "number" || - typeof parsed.hostname !== "string" || - typeof parsed.acquiredAt !== "string" - ) { - return undefined; - } - return parsed as LockOwner; - } catch { - return undefined; - } - } - - function isOwnerAlive(owner: LockOwner): boolean { - if (owner.hostname !== deps.hostname()) return false; - try { - deps.killProcess(owner.pid, 0); - return true; - } catch (error) { - return !deps.fsErrors.isProcessMissing({ error }); - } - } - - async function recoverStaleLock(): Promise { - let lockStat; - try { - lockStat = await deps.stat(lockPath); - } catch (error) { - /* v8 ignore next -- requires the lock to vanish between mkdir and stat */ - if (deps.fsErrors.isMissing({ error })) return; - /* v8 ignore next */ - throw error; - } - if (deps.now() - lockStat.mtimeMs <= staleMs) return; - - const owner = await readOwner(); - if (owner && isOwnerAlive(owner)) return; - - const stalePath = `${lockPath}.stale-${deps.randomUUID()}`; - try { - await deps.rename(lockPath, stalePath); - } catch (error) { - /* v8 ignore next -- requires another process to rename the stale lock first */ - if (deps.fsErrors.isMissing({ error })) return; - /* v8 ignore next */ - throw error; - } - await deps.rm(stalePath, { recursive: true, force: true }); - } - - async function acquireAttempt(startedAt: number): Promise { - const owner = await tryAcquire(); - if (owner) return owner; - await recoverStaleLock(); - if (deps.now() - startedAt >= timeoutMs) { - throw new RigErrorClass("CONFIG_INVALID", `Timed out waiting for lock: ${lockPath}`, { - lockPath, - timeoutMs, - }); - } - await deps.sleep(retryMs); - return acquireAttempt(startedAt); - } - - async function acquire(): Promise { - await deps.mkdir(deps.dirname(lockPath), { recursive: true }); - return acquireAttempt(deps.now()); - } - - async function run(runParams: { operation: () => T | Promise }): Promise { - const owner = await acquire(); - try { - return await runParams.operation(); - } finally { - await releaseLease(owner); - } - } - - return { run }; - } -} - -export const BoundedFileLock = new BoundedFileLockRepo(); - -// ─── AtomicFileWriterRepo ───────────────────────────────────────────────────── - -export type AtomicFileWriterDeps = { - mkdir: (path: string, options?: { recursive?: boolean }) => Promise; - rename: (oldPath: string, newPath: string) => Promise; - rm: (path: string, options?: { force?: boolean }) => Promise; - writeFile: (path: string, content: string, encoding: "utf8") => Promise; - dirname: (path: string) => string; - randomUUID: () => string; - getProcessPid: () => number; -}; - -export class AtomicFileWriterRepo extends defineRepo({ - params: {}, - deps: { - mkdir: mkdir as unknown as AtomicFileWriterDeps["mkdir"], - rename: rename as unknown as AtomicFileWriterDeps["rename"], - rm: rm as unknown as AtomicFileWriterDeps["rm"], - writeFile: writeFile as unknown as AtomicFileWriterDeps["writeFile"], - dirname, - randomUUID: randomUUID as unknown as AtomicFileWriterDeps["randomUUID"], - getProcessPid, - } as AtomicFileWriterDeps, -}) { - public async write(params: { path: string; content: string }): Promise { - await this.deps.mkdir(this.deps.dirname(params.path), { recursive: true }); - const temporaryPath = `${params.path}.tmp-${this.deps.getProcessPid()}-${this.deps.randomUUID()}`; - try { - await this.deps.writeFile(temporaryPath, params.content, "utf8"); - await this.deps.rename(temporaryPath, params.path); - } catch (error) { - await this.deps.rm(temporaryPath, { force: true }); - throw error; - } - } -} - -export const AtomicFileWriter = new AtomicFileWriterRepo(); - -// ─── BoundedFileLockClass (class-free constructible adapter) ────────────────── - -export interface BoundedFileLockClass { - run(operation: () => T | Promise): Promise; -} - -function boundedFileLockRun( - this: { resource: ReturnType }, - operation: () => T | Promise, -): Promise { - return this.resource.run({ operation }); -} - -export const BoundedFileLockClass: new ( - targetPath: string, - options?: FileLockOptions, -) => BoundedFileLockClass = (function () { - function BoundedFileLockAdapter( - this: { resource: ReturnType }, - targetPath: string, - options: FileLockOptions = {}, - ): void { - this.resource = BoundedFileLock.create({ targetPath, options }); - } - - Object.defineProperty(BoundedFileLockAdapter.prototype, "run", { - enumerable: false, - configurable: true, - writable: true, - value: boundedFileLockRun, - }); - - return BoundedFileLockAdapter; -})() as unknown as new (targetPath: string, options?: FileLockOptions) => BoundedFileLockClass; - -// ─── AtomicFileWriterClass (class-free constructible adapter) ───────────────── - -export interface AtomicFileWriterClass { - write(path: string, content: string): Promise; -} - -export const AtomicFileWriterClass: new () => AtomicFileWriterClass = (function () { - function AtomicFileWriterAdapter(): void {} - - Object.defineProperty(AtomicFileWriterAdapter.prototype, "write", { - enumerable: false, - configurable: true, - writable: true, - value: function write(path: string, content: string): Promise { - return AtomicFileWriter.write({ path, content }); - }, - }); - - return AtomicFileWriterAdapter; -})() as unknown as new () => AtomicFileWriterClass; diff --git a/src/domains/settings/repo/index.ts b/src/domains/settings/repo/index.ts deleted file mode 100644 index 11c75c6..0000000 --- a/src/domains/settings/repo/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./file-lock.ts"; -export * from "./rig-paths.ts"; diff --git a/src/domains/settings/repo/migration-prompt-store.ts b/src/domains/settings/repo/migration-prompt-store.ts new file mode 100644 index 0000000..ac648f3 --- /dev/null +++ b/src/domains/settings/repo/migration-prompt-store.ts @@ -0,0 +1,92 @@ +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer, Predicate } from "effect"; +import { + AtomicFileWriterService, + atomicFileWriterLayer, +} from "../../../providers/filesystem/atomic-file-writer"; +import { FileLockService, fileLockLayer } from "../../../providers/filesystem/file-lock"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + rigPathsConfigLayer, +} from "../../../providers/paths/rig-paths"; +import { DirectoryMigrationError } from "../types/directory-migration"; + +type RigMigrationPromptState = { + version: 1; + prompts: Record; +}; + +export class MigrationPromptStoreService extends Context.Service< + MigrationPromptStoreService, + { + readonly hasPrompted: (promptId: string) => Effect.Effect; + readonly markPrompted: (promptId: string) => Effect.Effect; + } +>()("@rendotdev/rig/settings/MigrationPromptStoreService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const fileLock = yield* FileLockService; + const fileWriter = yield* AtomicFileWriterService; + const emptyPromptState = (): RigMigrationPromptState => ({ version: 1, prompts: {} }); + const isPromptState = (value: unknown): value is RigMigrationPromptState => + Predicate.isObject(value) && value.version === 1 && Predicate.isObject(value.prompts); + const readState = Effect.fn("MigrationPromptStoreService.readState")(function* () { + return yield* Effect.tryPromise({ + try: async () => { + try { + const value: unknown = JSON.parse( + await readFile(paths.migrationPromptStatePath, "utf8"), + ); + return isPromptState(value) ? value : emptyPromptState(); + } catch { + return emptyPromptState(); + } + }, + catch: (cause) => new DirectoryMigrationError({ operation: "hasPrompted", cause }), + }); + }); + const hasPrompted = Effect.fn("MigrationPromptStoreService.hasPrompted")(function* ( + promptId: string, + ) { + return (yield* readState()).prompts[promptId] !== undefined; + }); + const markPrompted = Effect.fn("MigrationPromptStoreService.markPrompted")(function* ( + promptId: string, + ) { + yield* fileLock + .withLock({ + targetPath: paths.migrationPromptStatePath, + operation: () => + Effect.gen(function* () { + const state = yield* readState(); + state.prompts[promptId] ??= { shownAt: new Date().toISOString() }; + yield* fileWriter.write( + paths.migrationPromptStatePath, + `${JSON.stringify(state, null, 2)}\n`, + ); + }), + }) + .pipe( + Effect.mapError( + (cause) => new DirectoryMigrationError({ operation: "markPrompted", cause }), + ), + ); + }); + return { hasPrompted, markPrompted } as const; + }), +}) { + static readonly layer = Layer.effect( + MigrationPromptStoreService, + MigrationPromptStoreService.make, + ); +} + +export const migrationPromptStoreLayer: Layer.Layer< + MigrationPromptStoreService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = MigrationPromptStoreService.layer.pipe( + Layer.provide(Layer.mergeAll(rigPathsConfigLayer, fileLockLayer, atomicFileWriterLayer)), +); diff --git a/src/domains/settings/repo/rig-paths.test.ts b/src/domains/settings/repo/rig-paths.test.ts deleted file mode 100644 index d1f820e..0000000 --- a/src/domains/settings/repo/rig-paths.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { posix } from "node:path"; -import { describe, expect, test, vi } from "vite-plus/test"; -import { type PathOptions, RigPathsClass, RigPaths, RigPathsRepo } from "./rig-paths"; - -const pathDeps = { - dirname: posix.dirname, - isAbsolute: posix.isAbsolute, - join: posix.join, - resolve: posix.resolve, -}; - -describe("Rig paths", () => { - test("builds every path from stable config and replaceable dependencies", () => { - const homedir = vi.fn<() => string>(() => "/fallback-home"); - const cwd = vi.fn<() => string>(() => "/working"); - const Paths = new RigPathsRepo({ - params: { - homeDir: "configured-home", - defaultBaseRegistryDirValue: "~/rig/tools", - legacyDefaultBaseRegistryDirValue: "~/.rig/tools", - }, - deps: { homedir, cwd, ...pathDeps }, - }); - - expect(Paths.homeDir({})).toBe("/working/configured-home"); - expect(Paths.expandTilde({ pathValue: "~" })).toBe("/working/configured-home"); - expect(Paths.expandTilde({ pathValue: "~/tools" })).toBe("/working/configured-home/tools"); - expect(Paths.expandTilde({ pathValue: "relative" })).toBe("relative"); - expect(Paths.resolve({ pathValue: "relative/file.ts" })).toBe("/working/relative/file.ts"); - expect(Paths.resolve({ pathValue: "/absolute/file.ts" })).toBe("/absolute/file.ts"); - expect(Paths.rigDir({})).toBe("/working/configured-home/rig"); - expect(Paths.legacyRigDir({})).toBe("/working/configured-home/.rig"); - expect(Paths.configPath({})).toBe("/working/configured-home/rig/rig.json"); - expect(Paths.runtimeDir({})).toBe("/working/configured-home/rig/runtime"); - expect(Paths.runtimeSdkPath({})).toBe("/working/configured-home/rig/runtime/sdk.ts"); - expect(Paths.runtimeTypesPath({})).toBe("/working/configured-home/rig/runtime/types.d.ts"); - expect(Paths.runtimeGlobalsPath({})).toBe("/working/configured-home/rig/runtime/globals.d.ts"); - expect(Paths.runtimeToolTsconfigPath({})).toBe( - "/working/configured-home/rig/runtime/tsconfig.tools.json", - ); - expect(Paths.cronDir({})).toBe("/working/configured-home/rig/cron"); - expect(Paths.logsDir({})).toBe("/working/configured-home/rig/.logs"); - expect(Paths.cronWorkerPath({ name: "daily" })).toBe( - "/working/configured-home/rig/cron/daily.ts", - ); - expect(Paths.updateCheckCachePath({})).toBe("/working/configured-home/rig/update-check.json"); - expect(Paths.toolMetadataCachePath({})).toBe("/working/configured-home/rig/tool-metadata.json"); - expect(Paths.migrationPromptStatePath({})).toBe( - "/working/configured-home/rig/migration-prompts.json", - ); - expect(Paths.defaultBaseRegistryDir({})).toBe("~/rig/tools"); - expect(Paths.legacyDefaultBaseRegistryDir({})).toBe("~/.rig/tools"); - expect(Paths.parentDir({ pathValue: "/working/file.ts" })).toBe("/working"); - expect(homedir).not.toHaveBeenCalled(); - expect(cwd).toHaveBeenCalled(); - }); - - test("uses the replaceable home directory dependency without an override", () => { - const homedir = vi.fn<() => string>(() => "/fallback-home"); - const Paths = new RigPathsRepo({ - params: { - homeDir: undefined, - defaultBaseRegistryDirValue: "~/rig/tools", - legacyDefaultBaseRegistryDirValue: "~/.rig/tools", - }, - deps: { homedir, cwd: () => "/working", ...pathDeps }, - }); - - expect(Paths.homeDir({})).toBe("/fallback-home"); - expect(Paths.rigDir({})).toBe("/fallback-home/rig"); - expect(homedir).toHaveBeenCalled(); - expect(RigPaths.homeDir({})).toBeTruthy(); - }); - - test("preserves legacy getters, positional methods, options, and instanceof", () => { - const options: PathOptions = { homeDir: "/legacy-home" }; - const paths = new RigPathsClass(options); - - expect(paths).toBeInstanceOf(RigPathsClass); - expect(paths.homeDir).toBe("/legacy-home"); - expect(paths.rigDir).toBe("/legacy-home/rig"); - expect(paths.legacyRigDir).toBe("/legacy-home/.rig"); - expect(paths.configPath).toBe("/legacy-home/rig/rig.json"); - expect(paths.runtimeDir).toBe("/legacy-home/rig/runtime"); - expect(paths.runtimeSdkPath).toBe("/legacy-home/rig/runtime/sdk.ts"); - expect(paths.runtimeTypesPath).toBe("/legacy-home/rig/runtime/types.d.ts"); - expect(paths.runtimeGlobalsPath).toBe("/legacy-home/rig/runtime/globals.d.ts"); - expect(paths.runtimeToolTsconfigPath).toBe("/legacy-home/rig/runtime/tsconfig.tools.json"); - expect(paths.cronDir).toBe("/legacy-home/rig/cron"); - expect(paths.logsDir).toBe("/legacy-home/rig/.logs"); - expect(paths.updateCheckCachePath).toBe("/legacy-home/rig/update-check.json"); - expect(paths.toolMetadataCachePath).toBe("/legacy-home/rig/tool-metadata.json"); - expect(paths.migrationPromptStatePath).toBe("/legacy-home/rig/migration-prompts.json"); - expect(paths.defaultBaseRegistryDir).toBe("~/rig/tools"); - expect(paths.legacyDefaultBaseRegistryDir).toBe("~/.rig/tools"); - expect(paths.expandTilde("~/tools")).toBe("/legacy-home/tools"); - expect(paths.resolve("relative/file.ts")).toBe( - posix.resolve(process.cwd(), "relative/file.ts"), - ); - expect(paths.cronWorkerPath("daily")).toBe("/legacy-home/rig/cron/daily.ts"); - expect(paths.parentDir("/legacy-home/file.ts")).toBe("/legacy-home"); - expect(new RigPathsClass().homeDir).toBeTruthy(); - }); -}); diff --git a/src/domains/settings/repo/rig-paths.ts b/src/domains/settings/repo/rig-paths.ts deleted file mode 100644 index cd464d1..0000000 --- a/src/domains/settings/repo/rig-paths.ts +++ /dev/null @@ -1,233 +0,0 @@ -import { homedir } from "node:os"; -import { dirname, isAbsolute, join, resolve as resolvePath } from "node:path"; -import { defineRepo } from "../../../define.ts"; - -export type PathOptions = { - homeDir?: string; -}; - -const RigPathsProductionDeps = { - homedir, - cwd: process.cwd.bind(process), - dirname, - isAbsolute, - join, - resolve: resolvePath, -}; - -export class RigPathsRepo extends defineRepo({ - params: { - homeDir: undefined, - defaultBaseRegistryDirValue: "~/rig/tools", - legacyDefaultBaseRegistryDirValue: "~/.rig/tools", - } as PathOptions & { - defaultBaseRegistryDirValue: string; - legacyDefaultBaseRegistryDirValue: string; - }, - deps: RigPathsProductionDeps, -}) { - public homeDir(params: Record): string { - void params; - return this.params.homeDir - ? this.deps.resolve(this.deps.cwd(), this.params.homeDir) - : this.deps.homedir(); - } - - public expandTilde(params: { pathValue: string }): string { - if (params.pathValue === "~") return this.homeDir({}); - if (params.pathValue.startsWith("~/")) { - return this.deps.join(this.homeDir({}), params.pathValue.slice(2)); - } - return params.pathValue; - } - - public resolve(params: { pathValue: string }): string { - const expanded = this.expandTilde({ pathValue: params.pathValue }); - return this.deps.isAbsolute(expanded) - ? this.deps.resolve(expanded) - : this.deps.resolve(this.deps.cwd(), expanded); - } - - public rigDir(params: Record): string { - void params; - return this.deps.join(this.homeDir({}), "rig"); - } - - public legacyRigDir(params: Record): string { - void params; - return this.deps.join(this.homeDir({}), ".rig"); - } - - public configPath(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "rig.json"); - } - - public runtimeDir(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "runtime"); - } - - public runtimeSdkPath(params: Record): string { - void params; - return this.deps.join(this.runtimeDir({}), "sdk.ts"); - } - - public runtimeTypesPath(params: Record): string { - void params; - return this.deps.join(this.runtimeDir({}), "types.d.ts"); - } - - public runtimeGlobalsPath(params: Record): string { - void params; - return this.deps.join(this.runtimeDir({}), "globals.d.ts"); - } - - public runtimeToolTsconfigPath(params: Record): string { - void params; - return this.deps.join(this.runtimeDir({}), "tsconfig.tools.json"); - } - - public cronDir(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "cron"); - } - - public logsDir(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), ".logs"); - } - - public cronWorkerPath(params: { name: string }): string { - return this.deps.join(this.cronDir({}), `${params.name}.ts`); - } - - public updateCheckCachePath(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "update-check.json"); - } - - public toolMetadataCachePath(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "tool-metadata.json"); - } - - public migrationPromptStatePath(params: Record): string { - void params; - return this.deps.join(this.rigDir({}), "migration-prompts.json"); - } - - public defaultBaseRegistryDir(params: Record): string { - void params; - return this.params.defaultBaseRegistryDirValue; - } - - public legacyDefaultBaseRegistryDir(params: Record): string { - void params; - return this.params.legacyDefaultBaseRegistryDirValue; - } - - public parentDir(params: { pathValue: string }): string { - return this.deps.dirname(params.pathValue); - } -} - -export const RigPaths = new RigPathsRepo(); - -export type RigPathsClass = { - readonly homeDir: string; - readonly rigDir: string; - readonly legacyRigDir: string; - readonly configPath: string; - readonly runtimeDir: string; - readonly runtimeSdkPath: string; - readonly runtimeTypesPath: string; - readonly runtimeGlobalsPath: string; - readonly runtimeToolTsconfigPath: string; - readonly cronDir: string; - readonly logsDir: string; - readonly updateCheckCachePath: string; - readonly toolMetadataCachePath: string; - readonly migrationPromptStatePath: string; - readonly defaultBaseRegistryDir: string; - readonly legacyDefaultBaseRegistryDir: string; - expandTilde(pathValue: string): string; - resolve(pathValue: string): string; - cronWorkerPath(name: string): string; - parentDir(pathValue: string): string; -}; - -type RigPathsConstructor = { - new (options?: PathOptions): RigPathsClass; - readonly prototype: RigPathsClass; -}; - -function RigPathsConstructorAdapter(this: RigPathsClass, options: PathOptions = {}) { - const Paths = new RigPathsRepo({ - params: { - homeDir: options.homeDir, - defaultBaseRegistryDirValue: "~/rig/tools", - legacyDefaultBaseRegistryDirValue: "~/.rig/tools", - }, - deps: RigPathsProductionDeps, - }); - - Object.defineProperties(this, { - homeDir: { configurable: true, get: () => Paths.homeDir({}) }, - rigDir: { configurable: true, get: () => Paths.rigDir({}) }, - legacyRigDir: { configurable: true, get: () => Paths.legacyRigDir({}) }, - configPath: { configurable: true, get: () => Paths.configPath({}) }, - runtimeDir: { configurable: true, get: () => Paths.runtimeDir({}) }, - runtimeSdkPath: { configurable: true, get: () => Paths.runtimeSdkPath({}) }, - runtimeTypesPath: { configurable: true, get: () => Paths.runtimeTypesPath({}) }, - runtimeGlobalsPath: { configurable: true, get: () => Paths.runtimeGlobalsPath({}) }, - runtimeToolTsconfigPath: { - configurable: true, - get: () => Paths.runtimeToolTsconfigPath({}), - }, - cronDir: { configurable: true, get: () => Paths.cronDir({}) }, - logsDir: { configurable: true, get: () => Paths.logsDir({}) }, - updateCheckCachePath: { - configurable: true, - get: () => Paths.updateCheckCachePath({}), - }, - toolMetadataCachePath: { - configurable: true, - get: () => Paths.toolMetadataCachePath({}), - }, - migrationPromptStatePath: { - configurable: true, - get: () => Paths.migrationPromptStatePath({}), - }, - defaultBaseRegistryDir: { - configurable: true, - get: () => Paths.defaultBaseRegistryDir({}), - }, - legacyDefaultBaseRegistryDir: { - configurable: true, - get: () => Paths.legacyDefaultBaseRegistryDir({}), - }, - expandTilde: { - configurable: true, - value: (pathValue: string) => Paths.expandTilde({ pathValue }), - writable: true, - }, - resolve: { - configurable: true, - value: (pathValue: string) => Paths.resolve({ pathValue }), - writable: true, - }, - cronWorkerPath: { - configurable: true, - value: (name: string) => Paths.cronWorkerPath({ name }), - writable: true, - }, - parentDir: { - configurable: true, - value: (pathValue: string) => Paths.parentDir({ pathValue }), - writable: true, - }, - }); -} - -export const RigPathsClass = RigPathsConstructorAdapter as unknown as RigPathsConstructor; diff --git a/src/domains/settings/service/config-store.integration.test.ts b/src/domains/settings/service/config-store.integration.test.ts new file mode 100644 index 0000000..25ee975 --- /dev/null +++ b/src/domains/settings/service/config-store.integration.test.ts @@ -0,0 +1,127 @@ +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, +} from "../../../providers/paths/rig-paths"; +import { RigHomeDirectoryMigrationPromptId } from "../config/directory-migration"; +import { RigConfigError } from "../types/config-error"; +import { RigConfigStoreService, rigConfigStoreLayer } from "./config-store"; + +const homes: string[] = []; + +const makeTestConfigLayer = (homeDir: string) => { + const optionsLayer = Layer.succeed(RigPathsOptionsConfigService, { homeDir }); + return rigConfigStoreLayer.pipe( + Layer.provide(Layer.merge(optionsLayer, RigPathsPlatformService.layer)), + ); +}; + +const createHome = async () => { + const home = await mkdtemp(join(tmpdir(), "rig-config-test-")); + homes.push(home); + return home; +}; + +const runConfig = ( + home: string, + use: (service: typeof RigConfigStoreService.Service) => Effect.Effect, +) => + Effect.gen(function* () { + return yield* use(yield* RigConfigStoreService); + }).pipe(Effect.provide(makeTestConfigLayer(home)), Effect.runPromise); + +afterEach(async () => { + await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); +}); + +describe("RigConfigStoreService", () => { + test("creates first-run settings without generating tool runtime support", async () => { + const home = await createHome(); + const config = await runConfig(home, (service) => service.ensure); + + expect(config).toMatchObject({ version: 1, baseRegistryDir: "~/rig/tools" }); + expect(existsSync(join(home, "rig", "rig.json"))).toBe(true); + expect(existsSync(join(home, "rig", "runtime"))).toBe(false); + }); + + test("migrates legacy state and rewrites the legacy registry path", async () => { + const home = await createHome(); + await mkdir(join(home, ".rig", "tools", "legacy"), { recursive: true }); + await writeFile( + join(home, ".rig", "rig.json"), + `${JSON.stringify({ version: 1, baseRegistryDir: "~/.rig/tools", customRegistries: [] })}\n`, + ); + + const result = await runConfig(home, (service) => + Effect.gen(function* () { + const config = yield* service.ensure; + return { config, migration: yield* service.migrationResult }; + }), + ); + + expect(result.migration).toMatchObject({ status: "migrated", configUpdated: true }); + expect(result.config.baseRegistryDir).toBe("~/rig/tools"); + expect(existsSync(join(home, ".rig"))).toBe(false); + }); + + test("records a manual migration prompt exactly once", async () => { + const home = await createHome(); + await mkdir(join(home, ".rig", "tools", "legacy"), { recursive: true }); + await mkdir(join(home, "rig", "tools", "current"), { recursive: true }); + await writeFile( + join(home, ".rig", "rig.json"), + `${JSON.stringify({ version: 1, baseRegistryDir: "~/.rig/tools", customRegistries: [] })}\n`, + ); + await writeFile( + join(home, "rig", "rig.json"), + `${JSON.stringify({ version: 1, baseRegistryDir: "~/rig/tools", customRegistries: [], cronJobs: [] })}\n`, + ); + + await runConfig(home, (service) => + Effect.gen(function* () { + yield* service.ensure; + expect(yield* service.migrationResult).toMatchObject({ status: "manual" }); + yield* service.acknowledgeMigrationPrompt; + yield* service.acknowledgeMigrationPrompt; + }), + ); + + const state = await readFile(join(home, "rig", "migration-prompts.json"), "utf8"); + expect(state).toContain(RigHomeDirectoryMigrationPromptId); + }); + + test("serializes concurrent updates without losing changes", async () => { + const home = await createHome(); + await runConfig(home, (service) => service.ensure); + await Promise.all( + Array.from({ length: 12 }, (_, index) => + runConfig(home, (service) => + service.update((config) => ({ + ...config, + customRegistries: [...config.customRegistries, `registry-${index}`], + })), + ), + ), + ); + + const config = await runConfig(home, (service) => service.read); + expect(config.customRegistries.toSorted()).toEqual( + Array.from({ length: 12 }, (_, index) => `registry-${index}`).toSorted(), + ); + expect(existsSync(`${join(home, "rig", "rig.json")}.lock`)).toBe(false); + }); + + test("releases the lock after a failed mutator", async () => { + const home = await createHome(); + await runConfig(home, (service) => service.ensure); + await expect( + runConfig(home, (service) => service.update(() => Promise.reject(new Error("failed")))), + ).rejects.toBeInstanceOf(RigConfigError); + expect(existsSync(`${join(home, "rig", "rig.json")}.lock`)).toBe(false); + }); +}); diff --git a/src/domains/settings/service/config-store.test.ts b/src/domains/settings/service/config-store.test.ts new file mode 100644 index 0000000..275cefa --- /dev/null +++ b/src/domains/settings/service/config-store.test.ts @@ -0,0 +1,57 @@ +/* oxlint-disable vitest/no-standalone-expect -- @effect/vitest owns this Effect-aware test. */ +import { it } from "@effect/vitest"; +import { Effect, Layer } from "effect"; +import { expect } from "vite-plus/test"; +import { RigConfigError } from "../types/config-error"; +import type { RigConfig } from "../types/config-schema"; +import { RigConfigStoreService } from "./config-store"; + +const initial: RigConfig = { + version: 1, + baseRegistryDir: "~/rig/tools", + customRegistries: [], + cronJobs: [], +}; + +it.effect("supports replacement with a test layer", () => { + let stored = initial; + const layer = Layer.succeed(RigConfigStoreService, { + // @effect-diagnostics-next-line effectSucceedWithVoid:off + migrationResult: Effect.succeed(undefined), + acknowledgeMigrationPrompt: Effect.void, + ensure: Effect.sync(() => stored), + read: Effect.sync(() => stored), + write: (config) => Effect.sync(() => void (stored = config)), + update: (mutator) => + Effect.promise(() => Promise.resolve(mutator(stored))).pipe( + Effect.tap((config) => Effect.sync(() => void (stored = config))), + ), + }); + + return Effect.gen(function* () { + const config = yield* RigConfigStoreService; + yield* config.write({ ...stored, customRegistries: ["written"] }); + expect((yield* config.update((current) => current)).customRegistries).toEqual(["written"]); + }).pipe(Effect.provide(layer)); +}); + +it.effect("retains failures in the typed error channel", () => { + const failure = new Error("config unavailable"); + const error = new RigConfigError({ operation: "ensure", cause: failure }); + const layer = Layer.succeed(RigConfigStoreService, { + // @effect-diagnostics-next-line effectSucceedWithVoid:off + migrationResult: Effect.succeed(undefined), + acknowledgeMigrationPrompt: Effect.void, + ensure: Effect.fail(error), + read: Effect.fail(error), + write: () => Effect.fail(error), + update: () => Effect.fail(error), + }); + + return Effect.gen(function* () { + const config = yield* RigConfigStoreService; + const received = yield* config.ensure.pipe(Effect.flip); + expect(received.operation).toBe("ensure"); + expect(received.cause).toBe(failure); + }).pipe(Effect.provide(layer)); +}); diff --git a/src/domains/settings/service/config-store.ts b/src/domains/settings/service/config-store.ts new file mode 100644 index 0000000..ea697f1 --- /dev/null +++ b/src/domains/settings/service/config-store.ts @@ -0,0 +1,72 @@ +import { Context, Effect, Layer } from "effect"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, + rigPathsConfigLayer, +} from "../../../providers/paths/rig-paths"; +import { RigConfigRepositoryService, rigConfigRepositoryLayer } from "../repo/config-repository"; +import { + MigrationPromptStoreService, + migrationPromptStoreLayer, +} from "../repo/migration-prompt-store"; +import { RigConfigError, type RigConfigOperation } from "../types/config-error"; +import type { RigConfig } from "../types/config-schema"; +import type { RigConfigMutator } from "../types/config-store"; +import type { RigDirectoryMigrationResult } from "../types/directory-migration"; +import { DirectoryMigrationService, directoryMigrationLayer } from "./directory-migration"; + +export class RigConfigStoreService extends Context.Service< + RigConfigStoreService, + { + readonly migrationResult: Effect.Effect; + readonly acknowledgeMigrationPrompt: Effect.Effect; + readonly ensure: Effect.Effect; + readonly read: Effect.Effect; + readonly write: (config: RigConfig) => Effect.Effect; + readonly update: (mutator: RigConfigMutator) => Effect.Effect; + } +>()("@rendotdev/rig/settings/RigConfigStoreService", { + make: Effect.gen(function* () { + const repository = yield* RigConfigRepositoryService; + const migrations = yield* DirectoryMigrationService; + const prompts = yield* MigrationPromptStoreService; + let migration: RigDirectoryMigrationResult | undefined; + const mapError = (operation: RigConfigOperation) => (cause: unknown) => + cause instanceof RigConfigError ? cause : new RigConfigError({ operation, cause }); + const migrationResult = Effect.sync(() => migration).pipe( + Effect.withSpan("RigConfigStoreService.migrationResult"), + ); + const acknowledgeMigrationPrompt = Effect.gen(function* () { + if (migration?.status === "manual") yield* prompts.markPrompted(migration.promptId); + }).pipe( + Effect.mapError(mapError("acknowledgeMigration")), + Effect.withSpan("RigConfigStoreService.acknowledgeMigrationPrompt"), + ); + const ensure = Effect.gen(function* () { + migration = yield* migrations.migrateIfNeeded; + return yield* repository.initialize; + }).pipe(Effect.mapError(mapError("ensure")), Effect.withSpan("RigConfigStoreService.ensure")); + const read = repository.read.pipe(Effect.withSpan("RigConfigStoreService.read")); + const write = Effect.fn("RigConfigStoreService.write")(function* (config: RigConfig) { + yield* repository.write(config); + }); + const update = Effect.fn("RigConfigStoreService.update")(function* (mutator: RigConfigMutator) { + return yield* repository.update(mutator); + }); + return { migrationResult, acknowledgeMigrationPrompt, ensure, read, write, update } as const; + }), +}) { + static readonly layer = Layer.effect(RigConfigStoreService, RigConfigStoreService.make); +} + +const rigConfigStoreDependenciesLayer = Layer.mergeAll( + rigConfigRepositoryLayer, + directoryMigrationLayer, + migrationPromptStoreLayer, +).pipe(Layer.provideMerge(rigPathsConfigLayer)); + +export const rigConfigStoreLayer: Layer.Layer< + RigConfigStoreService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = RigConfigStoreService.layer.pipe(Layer.provide(rigConfigStoreDependenciesLayer)); diff --git a/src/domains/settings/service/directory-migration.ts b/src/domains/settings/service/directory-migration.ts index 9f21950..4d867a5 100644 --- a/src/domains/settings/service/directory-migration.ts +++ b/src/domains/settings/service/directory-migration.ts @@ -1,378 +1,146 @@ -import { existsSync } from "node:fs"; -import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises"; -import { join } from "node:path"; -import { defineService } from "../../../define.ts"; -import { AtomicFileWriterClass, BoundedFileLockClass } from "../repo/file-lock.ts"; -import { RigPathsClass } from "../repo/rig-paths.ts"; - -export const RigHomeDirectoryMigrationPromptId = "v0.0.19-home-directory"; - -export type RigDirectoryMigrationResult = { - promptId: string; - status: "migrated" | "manual"; - legacyDir: string; - currentDir: string; - configUpdated: boolean; - reason?: string; -}; - -type RigMigrationPromptState = { - version: 1; - prompts: Record; -}; - -type RigMigrationPromptStoreDeps = { - paths: RigPathsClass; - lock: Pick; - writer: Pick; - readFile: typeof readFile; - nowIso: () => string; -}; - -function createRigMigrationPromptStoreDeps(paths: RigPathsClass): RigMigrationPromptStoreDeps { - return { - paths, - lock: new BoundedFileLockClass(paths.migrationPromptStatePath), - writer: new AtomicFileWriterClass(), - readFile, - nowIso() { - return new Date().toISOString(); - }, - }; -} - -function emptyPromptState(_params: {}): RigMigrationPromptState { - return { version: 1, prompts: {} }; -} - -function isPromptState(params: { value: unknown }): params is { value: RigMigrationPromptState } { - return ( - typeof params.value === "object" && - params.value !== null && - !Array.isArray(params.value) && - (params.value as { version?: unknown }).version === 1 && - typeof (params.value as { prompts?: unknown }).prompts === "object" && - (params.value as { prompts?: unknown }).prompts !== null && - !Array.isArray((params.value as { prompts?: unknown }).prompts) - ); -} - -const RigMigrationPromptStoreProductionDeps = createRigMigrationPromptStoreDeps( - new RigPathsClass(), -); - -export class RigMigrationPromptStoreService extends defineService({ - params: {}, - deps: RigMigrationPromptStoreProductionDeps, -}) { - private async read(_params: {}): Promise { - try { - const candidate = { - value: JSON.parse( - await this.deps.readFile(this.deps.paths.migrationPromptStatePath, "utf8"), - ) as unknown, - }; - if (!isPromptState(candidate)) return emptyPromptState({}); - return candidate.value; - } catch { - return emptyPromptState({}); - } +import { Context, Effect, Layer } from "effect"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + RigPathsService, + rigPathsConfigLayer, + rigPathsLayer, +} from "../../../providers/paths/rig-paths"; +import { RigHomeDirectoryMigrationPromptId } from "../config/directory-migration"; +import { + DirectoryMigrationInspectionService, + DirectoryMigrationRepositoryService, + directoryMigrationRepositoryLayer, +} from "../repo/directory-migration"; +import { + MigrationPromptStoreService, + migrationPromptStoreLayer, +} from "../repo/migration-prompt-store"; +import { + DirectoryMigrationError, + type RigDirectoryMigrationResult, +} from "../types/directory-migration"; + +class DirectoryMigrationCurrentStateService extends Context.Service< + DirectoryMigrationCurrentStateService, + { + readonly canReplaceCurrentDirectory: Effect.Effect; } - - private async write(params: { state: RigMigrationPromptState }): Promise { - await this.deps.writer.write( - this.deps.paths.migrationPromptStatePath, - `${JSON.stringify(params.state, null, 2)}\n`, +>()("@rendotdev/rig/settings/DirectoryMigrationCurrentStateService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const pathOperations = yield* RigPathsService; + const inspection = yield* DirectoryMigrationInspectionService; + const hasEmptyRegistry = Effect.fn("DirectoryMigrationCurrentStateService.hasEmptyRegistry")( + function* () { + const path = yield* pathOperations.resolve(paths.defaultBaseRegistryDir); + return ( + !(yield* inspection.directoryExists(path)) || + (yield* inspection.visibleEntries(path)).every((entry) => entry === "tsconfig.json") + ); + }, ); - } - - public async hasPrompted(params: { promptId: string }): Promise { - const state = await this.read({}); - return state.prompts[params.promptId] !== undefined; - } - - public async markPrompted(params: { promptId: string }): Promise { - await this.deps.lock.run(async () => { - const state = await this.read({}); - state.prompts[params.promptId] ??= { shownAt: this.deps.nowIso() }; - await this.write({ state }); - }); - } -} - -export const RigMigrationPromptStore = new RigMigrationPromptStoreService(); - -export type RigMigrationPromptStoreClass = { - hasPrompted(promptId: string): Promise; - markPrompted(promptId: string): Promise; -}; - -type RigMigrationPromptStoreConstructor = { - new (paths: RigPathsClass): RigMigrationPromptStoreClass; - readonly prototype: RigMigrationPromptStoreClass; -}; - -type RigMigrationPromptStoreAdapter = RigMigrationPromptStoreClass & { - readonly resource: RigMigrationPromptStoreService; -}; - -const RigMigrationPromptStoreClassAdapter = function constructRigMigrationPromptStore( - this: RigMigrationPromptStoreAdapter, - paths: RigPathsClass, -): void { - Object.defineProperty(this, "resource", { - value: new RigMigrationPromptStoreService({ - params: {}, - deps: createRigMigrationPromptStoreDeps(paths), - }), - }); -}; -Object.defineProperty(RigMigrationPromptStoreClassAdapter, "name", { - value: "RigMigrationPromptStoreClass", -}); -Object.defineProperties(RigMigrationPromptStoreClassAdapter.prototype, { - hasPrompted: { - configurable: true, - value: function hasPrompted(this: RigMigrationPromptStoreAdapter, promptId: string) { - return this.resource.hasPrompted({ promptId }); - }, - writable: true, - }, - markPrompted: { - configurable: true, - value: function markPrompted(this: RigMigrationPromptStoreAdapter, promptId: string) { - return this.resource.markPrompted({ promptId }); - }, - writable: true, - }, -}); - -export const RigMigrationPromptStoreClass = - RigMigrationPromptStoreClassAdapter as unknown as RigMigrationPromptStoreConstructor; - -type RigDirectoryMigrationServiceDeps = { - paths: RigPathsClass; - promptStore: RigMigrationPromptStoreClass; - exists: typeof existsSync; - mkdir: typeof mkdir; - readdir: typeof readdir; - readFile: typeof readFile; - rename: typeof rename; - rm: typeof rm; - stat: typeof stat; - writeFile: typeof writeFile; - join: typeof join; -}; - -function createRigDirectoryMigrationServiceDeps( - paths: RigPathsClass, - promptStore: RigMigrationPromptStoreClass = new RigMigrationPromptStoreClass(paths), -): RigDirectoryMigrationServiceDeps { - return { - paths, - promptStore, - exists: existsSync, - mkdir, - readdir, - readFile, - rename, - rm, - stat, - writeFile, - join, - }; -} - -const RigDirectoryMigrationServiceProductionDeps = createRigDirectoryMigrationServiceDeps( - new RigPathsClass(), -); - -export class RigDirectoryMigrationService extends defineService({ - params: {}, - deps: RigDirectoryMigrationServiceProductionDeps, -}) { - private async directoryExists(params: { path: string }): Promise { - try { - return (await this.deps.stat(params.path)).isDirectory(); - } catch { - return false; - } - } - - private async visibleEntries(params: { path: string }): Promise { - return (await this.deps.readdir(params.path)).filter(function keepVisible(entry) { - return entry !== ".DS_Store"; - }); - } - - private isLegacyBaseRegistry(params: { value: unknown }): boolean { - return ( - typeof params.value === "string" && - (params.value === this.deps.paths.legacyDefaultBaseRegistryDir || - this.deps.paths.resolve(params.value) === - this.deps.join(this.deps.paths.legacyRigDir, "tools")) + const hasEmptyCron = Effect.fn("DirectoryMigrationCurrentStateService.hasEmptyCron")( + function* () { + return ( + !(yield* inspection.directoryExists(paths.cronDir)) || + (yield* inspection.visibleEntries(paths.cronDir)).length === 0 + ); + }, ); - } - - private async rewriteMigratedConfig(_params: {}): Promise { - const configPath = this.deps.paths.configPath; - if (!this.deps.exists(configPath)) return false; - - const parsed = JSON.parse(await this.deps.readFile(configPath, "utf8")) as Record< - string, - unknown - >; - if (!this.isLegacyBaseRegistry({ value: parsed.baseRegistryDir })) return false; - - parsed.baseRegistryDir = this.deps.paths.defaultBaseRegistryDir; - await this.deps.writeFile(configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8"); - return true; - } - - private async hasDefaultCurrentConfig(params: { currentDir: string }): Promise { - try { - const parsed = JSON.parse( - await this.deps.readFile(this.deps.join(params.currentDir, "rig.json"), "utf8"), - ) as { - version?: unknown; - baseRegistryDir?: unknown; - customRegistries?: unknown; - cronJobs?: unknown; - }; - return ( - parsed.version === 1 && - parsed.baseRegistryDir === this.deps.paths.defaultBaseRegistryDir && - Array.isArray(parsed.customRegistries) && - parsed.customRegistries.length === 0 && - (parsed.cronJobs === undefined || - (Array.isArray(parsed.cronJobs) && parsed.cronJobs.length === 0)) - ); - } catch { - return false; - } - } - - private async hasEmptyGeneratedRegistry(params: { currentDir: string }): Promise { - const toolsDir = this.deps.join(params.currentDir, "tools"); - if (!(await this.directoryExists({ path: toolsDir }))) return true; - const entries = await this.visibleEntries({ path: toolsDir }); - return entries.every(function isGeneratedEntry(entry) { - return entry === "tsconfig.json"; - }); - } - - private async hasEmptyCronDirectory(params: { currentDir: string }): Promise { - const cronDir = this.deps.join(params.currentDir, "cron"); - if (!(await this.directoryExists({ path: cronDir }))) return true; - return (await this.visibleEntries({ path: cronDir })).length === 0; - } - - private async hasOnlyGeneratedCurrentEntries(params: { currentDir: string }): Promise { - const entries = await this.visibleEntries({ path: params.currentDir }); - return entries.every(function isGeneratedEntry(entry) { - return [ + const hasOnlyGeneratedEntries = Effect.fn( + "DirectoryMigrationCurrentStateService.hasOnlyGeneratedEntries", + )(function* () { + const generated = new Set([ "rig.json", "runtime", "tools", "update-check.json", "cron", "migration-prompts.json", - ].includes(entry); + ]); + return (yield* inspection.visibleEntries(paths.rigDir)).every((entry) => + generated.has(entry), + ); }); - } - - private async canReplaceCurrentDirectory(params: { currentDir: string }): Promise { - return ( - (await this.hasDefaultCurrentConfig(params)) && - (await this.hasEmptyGeneratedRegistry(params)) && - (await this.hasEmptyCronDirectory(params)) && - (await this.hasOnlyGeneratedCurrentEntries(params)) - ); - } + const canReplaceCurrentDirectory = Effect.gen(function* () { + return ( + (yield* inspection.hasDefaultConfig) && + (yield* hasEmptyRegistry()) && + (yield* hasEmptyCron()) && + (yield* hasOnlyGeneratedEntries()) + ); + }).pipe(Effect.withSpan("DirectoryMigrationCurrentStateService.canReplaceCurrentDirectory")); + return { canReplaceCurrentDirectory } as const; + }), +}) { + static readonly layer = Layer.effect( + DirectoryMigrationCurrentStateService, + DirectoryMigrationCurrentStateService.make, + ); +} - private async hasLegacyState(params: { legacyDir: string }): Promise { - return ( - (await this.directoryExists({ path: params.legacyDir })) && - (this.deps.exists(this.deps.join(params.legacyDir, "rig.json")) || - this.deps.exists(this.deps.join(params.legacyDir, "tools"))) - ); +export class DirectoryMigrationService extends Context.Service< + DirectoryMigrationService, + { + readonly migrateIfNeeded: Effect.Effect< + RigDirectoryMigrationResult | undefined, + DirectoryMigrationError + >; } - - public async migrateIfNeeded(_params: {}): Promise { - const legacyDir = this.deps.paths.legacyRigDir; - const currentDir = this.deps.paths.rigDir; - - if (!(await this.hasLegacyState({ legacyDir }))) return undefined; - - const currentExists = this.deps.exists(currentDir); - if (currentExists && !(await this.canReplaceCurrentDirectory({ currentDir }))) { - if (await this.deps.promptStore.hasPrompted(RigHomeDirectoryMigrationPromptId)) { - return undefined; +>()("@rendotdev/rig/settings/DirectoryMigrationService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const promptStore = yield* MigrationPromptStoreService; + const state = yield* DirectoryMigrationCurrentStateService; + const repository = yield* DirectoryMigrationRepositoryService; + const migrateIfNeeded = Effect.gen(function* () { + if (!(yield* repository.hasLegacyState)) return undefined; + const requiresManualMigration = + (yield* repository.currentExists) && !(yield* state.canReplaceCurrentDirectory); + if (requiresManualMigration) { + if (yield* promptStore.hasPrompted(RigHomeDirectoryMigrationPromptId)) return undefined; + return { + promptId: RigHomeDirectoryMigrationPromptId, + status: "manual" as const, + legacyDir: paths.legacyRigDir, + currentDir: paths.rigDir, + configUpdated: false, + reason: "Rig found data in both the old and new folders.", + }; } return { promptId: RigHomeDirectoryMigrationPromptId, - status: "manual", - legacyDir, - currentDir, - configUpdated: false, - reason: "Rig found data in both the old and new folders.", + status: "migrated" as const, + legacyDir: paths.legacyRigDir, + currentDir: paths.rigDir, + configUpdated: yield* repository.migrate, }; - } - - if (currentExists) await this.deps.rm(currentDir, { recursive: true, force: true }); - await this.deps.mkdir(this.deps.paths.homeDir, { recursive: true }); - await this.deps.rename(legacyDir, currentDir); - - return { - promptId: RigHomeDirectoryMigrationPromptId, - status: "migrated", - legacyDir, - currentDir, - configUpdated: await this.rewriteMigratedConfig({}), - }; - } + }).pipe(Effect.withSpan("DirectoryMigrationService.migrateIfNeeded")); + return { migrateIfNeeded } as const; + }), +}) { + static readonly layer = Layer.effect(DirectoryMigrationService, DirectoryMigrationService.make); } -export const RigDirectoryMigration = new RigDirectoryMigrationService(); - -export type RigDirectoryMigrationServiceClass = { - migrateIfNeeded(): Promise; -}; - -type RigDirectoryMigrationServiceConstructor = { - new ( - paths: RigPathsClass, - promptStore?: RigMigrationPromptStoreClass, - ): RigDirectoryMigrationServiceClass; - readonly prototype: RigDirectoryMigrationServiceClass; -}; - -type RigDirectoryMigrationServiceAdapter = RigDirectoryMigrationServiceClass & { - readonly resource: RigDirectoryMigrationService; -}; - -const RigDirectoryMigrationServiceClassAdapter = function constructRigDirectoryMigrationService( - this: RigDirectoryMigrationServiceAdapter, - paths: RigPathsClass, - promptStore?: RigMigrationPromptStoreClass, -): void { - Object.defineProperty(this, "resource", { - value: new RigDirectoryMigrationService({ - params: {}, - deps: createRigDirectoryMigrationServiceDeps(paths, promptStore), - }), - }); -}; -Object.defineProperty(RigDirectoryMigrationServiceClassAdapter, "name", { - value: "RigDirectoryMigrationServiceClass", -}); -Object.defineProperty(RigDirectoryMigrationServiceClassAdapter.prototype, "migrateIfNeeded", { - configurable: true, - value: function migrateIfNeeded(this: RigDirectoryMigrationServiceAdapter) { - return this.resource.migrateIfNeeded({}); - }, - writable: true, -}); - -export const RigDirectoryMigrationServiceClass = - RigDirectoryMigrationServiceClassAdapter as unknown as RigDirectoryMigrationServiceConstructor; +const directoryMigrationCurrentStateLayer = DirectoryMigrationCurrentStateService.layer.pipe( + Layer.provide(DirectoryMigrationInspectionService.layer), +); +const currentStateForMigrationLayer = directoryMigrationCurrentStateLayer.pipe( + Layer.provide(rigPathsLayer), +); +export const directoryMigrationLayer: Layer.Layer< + DirectoryMigrationService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = DirectoryMigrationService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + migrationPromptStoreLayer, + currentStateForMigrationLayer, + directoryMigrationRepositoryLayer, + ), + ), +); diff --git a/src/domains/settings/service/index.ts b/src/domains/settings/service/index.ts deleted file mode 100644 index 25e67b0..0000000 --- a/src/domains/settings/service/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./directory-migration.ts"; diff --git a/src/domains/settings/types/config-error.ts b/src/domains/settings/types/config-error.ts new file mode 100644 index 0000000..d4ef61e --- /dev/null +++ b/src/domains/settings/types/config-error.ts @@ -0,0 +1,16 @@ +import { Schema } from "effect"; + +const RigConfigOperation = Schema.Literals([ + "acknowledgeMigration", + "ensure", + "read", + "write", + "update", +]); + +export type RigConfigOperation = typeof RigConfigOperation.Type; + +export class RigConfigError extends Schema.TaggedErrorClass()("RigConfigError", { + operation: RigConfigOperation, + cause: Schema.Defect(), +}) {} diff --git a/src/domains/settings/types/config-schema.ts b/src/domains/settings/types/config-schema.ts index 116017a..47c1b85 100644 --- a/src/domains/settings/types/config-schema.ts +++ b/src/domains/settings/types/config-schema.ts @@ -1,6 +1,6 @@ import { z } from "zod"; -export const RigCronJobSchema = z.object({ +const RigCronJobSchema = z.object({ name: z.string().regex(/^[A-Za-z0-9_-]+$/), command: z.string().min(1), schedule: z.string().min(1), diff --git a/src/domains/settings/types/config-store.ts b/src/domains/settings/types/config-store.ts new file mode 100644 index 0000000..f279d8c --- /dev/null +++ b/src/domains/settings/types/config-store.ts @@ -0,0 +1,3 @@ +import type { RigConfig } from "./config-schema"; + +export type RigConfigMutator = (config: RigConfig) => RigConfig | Promise; diff --git a/src/domains/settings/types/directory-migration.ts b/src/domains/settings/types/directory-migration.ts new file mode 100644 index 0000000..25eac9b --- /dev/null +++ b/src/domains/settings/types/directory-migration.ts @@ -0,0 +1,17 @@ +import { Schema } from "effect"; + +export type RigDirectoryMigrationResult = Readonly<{ + promptId: string; + status: "migrated" | "manual"; + legacyDir: string; + currentDir: string; + configUpdated: boolean; + reason?: string; +}>; + +const DirectoryMigrationOperation = Schema.Literals(["hasPrompted", "markPrompted", "migrate"]); + +export class DirectoryMigrationError extends Schema.TaggedErrorClass()( + "DirectoryMigrationError", + { operation: DirectoryMigrationOperation, cause: Schema.Defect() }, +) {} diff --git a/src/domains/settings/types/index.ts b/src/domains/settings/types/index.ts deleted file mode 100644 index e282c67..0000000 --- a/src/domains/settings/types/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./config-schema.ts"; diff --git a/src/agents/instructions.ts b/src/domains/tools/config/agent-instructions.ts similarity index 100% rename from src/agents/instructions.ts rename to src/domains/tools/config/agent-instructions.ts diff --git a/src/domains/tools/config/runtime-support-sdk-source.ts b/src/domains/tools/config/runtime-support-sdk-source.ts new file mode 100644 index 0000000..34778f0 --- /dev/null +++ b/src/domains/tools/config/runtime-support-sdk-source.ts @@ -0,0 +1,185 @@ +export const RuntimeSupportSdkSourceTemplate = `// Generated by Rig. Safe to overwrite. +// @ts-nocheck +import { spawn } from "node:child_process"; +import { existsSync, statSync } from "node:fs"; +import { mkdir } from "node:fs/promises"; +import { homedir } from "node:os"; +import { dirname, isAbsolute, join, resolve } from "node:path"; +import type { z as zodNamespace } from "zod"; +import type { RigToolDefinition, RigToolFactory, RigToolKit } from "./types"; +// @ts-ignore file URL imports are emitted so generated tools can load Rig's bundled Zod. +import * as zodModule from __RIG_ZOD_IMPORT__; + +export const z: typeof zodNamespace = (zodModule as any).z ?? zodModule; + +function makeRigPathRuntime() { + return { + home: () => homedir(), + resolve(cwd, pathValue) { + if (pathValue === "~") return homedir(); + if (pathValue.startsWith("~/")) return join(homedir(), pathValue.slice(2)); + return isAbsolute(pathValue) ? pathValue : resolve(cwd, pathValue); + }, + async ensureParent(pathValue) { await mkdir(dirname(pathValue), { recursive: true }); }, + size: (pathValue) => existsSync(pathValue) ? statSync(pathValue).size : 0, + }; +} + +function makeRigArgsRuntime() { + const args = []; + const builder = { + raw(...values) { args.push(...values); return builder; }, + flag(name, enabled = true) { if (enabled) args.push(name); return builder; }, + value(name, value) { if (value !== undefined && value !== null) args.push(name, String(value)); return builder; }, + values(name, values) { for (const value of values ?? []) args.push(name, value); return builder; }, + toArray: () => [...args], + }; + return builder; +} + +function makeRigBoundedOutputCollector(maxBytes) { + const chunks = []; + let capturedBytes = 0; + let truncated = false; + const decodeUtf8Prefix = (buffer) => { + const strictDecoder = new TextDecoder("utf-8", { fatal: true }); + for (let removed = 0; removed <= Math.min(3, buffer.byteLength); removed++) { + try { return strictDecoder.decode(buffer.subarray(0, buffer.byteLength - removed)); } + catch {} + } + return new TextDecoder().decode(buffer); + }; + return { + capture(buffer) { + const remaining = Math.max(0, maxBytes - capturedBytes); + if (remaining > 0) { + const captured = buffer.subarray(0, remaining); + chunks.push(captured); + capturedBytes += captured.byteLength; + } + if (buffer.byteLength > remaining) truncated = true; + }, + result() { + const content = decodeUtf8Prefix(Buffer.concat(chunks, capturedBytes)); + return truncated ? content + "\\n[rig: output truncated]" : content; + }, + }; +} + +function makeRigManagedProcess(child, command, timeoutMs) { + let timedOut = false; + let timeout; + let escalation; + let spawnError; + const signal = (signalName) => { + try { + if (process.platform !== "win32" && child.pid) process.kill(-child.pid, signalName); + else child.kill(signalName); + } catch {} + }; + const terminate = () => { + timedOut = true; + signal("SIGTERM"); + escalation = setTimeout(() => signal("SIGKILL"), 100); + }; + return { + async wait() { + child.once("error", (error) => { spawnError = error; }); + timeout = setTimeout(terminate, timeoutMs); + try { + const exitCode = await new Promise((resolve) => child.once("close", (code) => resolve(code ?? 1))); + if (spawnError) throw new Error("Command could not start: " + command[0] + ": " + spawnError.message); + if (timedOut) throw new Error("Command timed out after " + timeoutMs + "ms."); + return exitCode; + } finally { + if (timeout) clearTimeout(timeout); + if (escalation) clearTimeout(escalation); + } + }, + }; +} + +function shellQuote(value) { + if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(value)) return value; + return "'" + value.replaceAll("'", "'\\''") + "'"; +} + +function makeRigShellRuntime() { + const renderTemplateValue = (value) => Array.isArray(value) + ? value.map((item) => shellQuote(String(item))).join(" ") + : shellQuote(String(value)); + const renderTemplateCommand = (strings, values) => strings.reduce( + (command, part, index) => command + part + (index < values.length ? renderTemplateValue(values[index]) : ""), + "", + ); + const runProcess = async (args, options = {}, reportedCommand = args) => { + const timeoutMs = options.timeoutMs ?? 30000; + const maxOutputBytes = options.maxOutputBytes ?? 1048576; + const child = spawn(args[0], args.slice(1), { + cwd: options.cwd ?? process.cwd(), + env: { ...process.env, ...options.env }, + stdio: ["ignore", "pipe", "pipe"], + detached: process.platform !== "win32", + }); + const stdout = makeRigBoundedOutputCollector(maxOutputBytes); + const stderr = makeRigBoundedOutputCollector(maxOutputBytes); + child.stdout.on("data", (chunk) => stdout.capture(chunk)); + child.stderr.on("data", (chunk) => stderr.capture(chunk)); + const exitCode = await makeRigManagedProcess(child, reportedCommand, timeoutMs).wait(); + return { command: reportedCommand, stdout: stdout.result(), stderr: stderr.result(), exitCode }; + }; + const exec = async (args, options = {}) => { + if (!Array.isArray(args) || args.length === 0 || args.some((arg) => typeof arg !== "string" || arg.length === 0)) { + throw new Error("shell.exec expects a non-empty array of command arguments."); + } + return runProcess(args, options); + }; + const bash = (command, options = {}) => runProcess(["bash", "-lc", command], options, [command]); + return { + exec, + bash, + $: (strings, ...values) => bash(renderTemplateCommand(strings, values)), + async json(args, options = {}) { + const result = await exec(args, options); + if (result.exitCode !== 0) throw new Error("Command failed before JSON could be parsed."); + return JSON.parse(result.stdout); + }, + }; +} + +const rigShell = makeRigShellRuntime(); + +async function runRigCommand(options) { + const id = options.tool ? options.tool + "." + options.command : options.command; + if (!/^[A-Za-z0-9_-]+[.][A-Za-z0-9_-]+$/.test(id)) throw new Error("Command id must use .: " + id); + if (options.input !== undefined && options.args?.length) throw new Error("Use input or args, not both."); + const args = ["rig", "run", id]; + if (options.input !== undefined) args.push("--input", JSON.stringify(options.input)); + if (options.dryRun) args.push("--dry-run"); + args.push(...(options.args ?? [])); + const result = await rigShell.exec(args); + if (result.exitCode !== 0) throw new Error("Rig tool command failed: " + id + "\\n" + result.stderr); + const envelope = JSON.parse(result.stdout); + if (envelope.errors?.length) throw new Error("Rig tool command failed: " + id + "\\n" + JSON.stringify(envelope.errors, null, 2)); + return envelope.data; +} + +export const rig: RigToolKit = { + z, + defineTool: (definition) => typeof definition.commands === "function" + ? { ...definition, commands: definition.commands((command) => command) } + : definition, + defineCommand: (definition) => definition, + run: runRigCommand, + $: (strings, ...values) => rigShell.$(strings, ...values), + args: makeRigArgsRuntime, + paths: makeRigPathRuntime(), + shell: rigShell, +} as unknown as RigToolKit; + +function defineToolRuntime(value: T): T { + return typeof value === "function" ? value : rig.defineTool(value as RigToolDefinition) as T; +} + +export const RigTool = { define: defineToolRuntime }; +`; diff --git a/src/domains/tools/config/runtime-support-types-source.ts b/src/domains/tools/config/runtime-support-types-source.ts new file mode 100644 index 0000000..0f8becd --- /dev/null +++ b/src/domains/tools/config/runtime-support-types-source.ts @@ -0,0 +1,224 @@ +export const RuntimeTypesSource = `// Generated by Rig. Safe to overwrite. +import type { Database } from "bun:sqlite"; +import type { z } from "zod"; + +export type RigSchema = z.ZodTypeAny; + +export type RigCollectionDefinition = z.ZodObject> = { + schema?: T; + generateId?: (data: z.input) => string; +}; + +export type RigCollectionEntry> = { + id: string; + data: T; + body: string; + createdAt: string; + updatedAt: string; +}; + +export type RigCollectionHandle> = { + readonly name: string; + readonly path: string; + create(entry: { id?: string; data: T; body?: string }): Promise>; + getEntry(id: string): Promise | null>; + update(id: string, patch: { data?: Partial; body?: string }): Promise>; + upsert(entry: { id: string; data: T; body?: string }): Promise<{ id: string; created: boolean }>; + remove(id: string): Promise; + list(options?: { where?: Record; sort?: string; limit?: number; offset?: number }): Promise<{ entries: RigCollectionEntry[]; total: number }>; + search(query: string, options?: { limit?: number }): Promise<{ entries: Array & { snippet: string; rank: number }> }>; + count(where?: Record): Promise; + getCollection(filter?: (entry: RigCollectionEntry) => boolean): Promise[]>; + clear(): Promise; +}; + +export type RigToolCollectionDefinitions = Record; + +export type RigToolCollectionHandles = { + [Name in keyof Collections]: Collections[Name] extends RigCollectionDefinition + ? RigCollectionHandle> + : RigCollectionHandle; +}; + +export type RigShellOptions = { + cwd?: string; + env?: Record; + timeoutMs?: number; + maxOutputBytes?: number; +}; + +export type RigShellResult = { + command: string[]; + stdout: string; + stderr: string; + exitCode: number; +}; + +export type RigShell = { + $(strings: TemplateStringsArray, ...values: unknown[]): Promise; + exec(args: string[], options?: RigShellOptions): Promise; + bash(command: string, options?: RigShellOptions): Promise; + json(args: string[], options?: RigShellOptions): Promise; +}; + +export type RigPathHelper = { + home(): string; + resolve(cwd: string, pathValue: string): string; + ensureParent(pathValue: string): Promise; + size(pathValue: string): number; +}; + +export type RigArgBuilder = { + raw(...values: string[]): RigArgBuilder; + flag(name: string, enabled?: boolean): RigArgBuilder; + value(name: string, value: string | number | boolean | undefined | null): RigArgBuilder; + values(name: string, values: string[] | undefined): RigArgBuilder; + toArray(): string[]; +}; + +export type RigRunOptions = { + tool?: string; + command: string; + input?: unknown; + args?: string[]; + dryRun?: boolean; +}; + +export type RigToolDatabase = Database & { + readonly path: string; + migrate(version: number, name: string, sql: string): void; +}; + +export type RigToolKvStore = { + readonly path: string; + get(key: string): T | undefined; + set(key: string, value: unknown): void; +}; + +export type RigCacheKey = readonly unknown[]; + +export type RigCacheQueryOptions = { + queryKey: RigCacheKey; + queryFn: () => T | Promise; + /** Cached values younger than this duration return immediately; missing or stale values refresh before returning. */ + staleTime?: number; +}; + +export type RigToolCache = { + readonly path: string; + query(options: RigCacheQueryOptions): Promise; + peek(queryKey: RigCacheKey): T | undefined; + set(queryKey: RigCacheKey, value: T): void; + invalidate(queryKey: RigCacheKey): void; + remove(queryKey: RigCacheKey): void; + clear(): void; +}; + +export type RigLogMethod = { + (message: string): void; + (bindings: Record, message?: string): void; + (error: Error, message?: string): void; +}; + +export type RigToolLogger = { + trace: RigLogMethod; + debug: RigLogMethod; + info: RigLogMethod; + warn: RigLogMethod; + error: RigLogMethod; + fatal: RigLogMethod; + child(bindings: Record): RigToolLogger; +}; + +export type RigToolRunContext< + Input, + Env = unknown, + Collections = Record, +> = { + input: Input; + env: Env; + processEnv: NodeJS.ProcessEnv; + cwd: string; + db: RigToolDatabase; + kv: RigToolKvStore; + cache: RigToolCache; + log: RigToolLogger; + rig: RigToolKit; + collections: Collections; +}; + +export type RigToolExample = { + title: string; + text: string; + input?: Input; + output?: Output; +}; + +export type RigCommandDefinition< + Input extends RigSchema = RigSchema, + Output extends RigSchema = RigSchema, + Env = unknown, + Collections = Record, +> = { + description: string; + input: Input; + output: Output; + examples?: RigToolExample, z.output>[]; + run: (ctx: RigToolRunContext, Env, Collections>) => z.input | Promise>; +}; + +export type RigToolCommandBuilder< + Env, + Collections, +> = ( + definition: RigCommandDefinition, +) => RigCommandDefinition; + +export type RigToolCommandMap = Record>; + +export type RigToolCommandsFactory< + Env, + Collections, + Commands extends RigToolCommandMap, +> = (command: RigToolCommandBuilder) => Commands; + +export type RigToolDefinition< + Env extends RigSchema = RigSchema, + Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, + Commands extends RigToolCommandMap = RigToolCommandMap, +> = { + name?: string; + description: string; + env?: Env; + setupDb?: (db: RigToolDatabase) => void | Promise; + collections?: Collections; + commands: + | Commands + | RigToolCommandsFactory, RigToolCollectionHandles, Commands>; +}; + +export type RigNormalizedToolDefinition< + Env extends RigSchema = RigSchema, + Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, + Commands extends RigToolCommandMap = RigToolCommandMap, +> = Omit, "commands"> & { + commands: Commands; +}; + +export type RigToolKit = { + z: typeof z; + defineTool< + Env extends RigSchema = RigSchema, + Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, + const Commands extends RigToolCommandMap = RigToolCommandMap, + >(definition: RigToolDefinition): RigNormalizedToolDefinition; + defineCommand(definition: RigCommandDefinition): RigCommandDefinition; + run(options: RigRunOptions): Promise; + $(strings: TemplateStringsArray, ...values: unknown[]): Promise; + args(): RigArgBuilder; + paths: RigPathHelper; + shell: RigShell; +}; + +export type RigToolFactory = (rig: RigToolKit) => RigToolDefinition | Promise>; +`; diff --git a/src/tools/domain/tool-api.ts b/src/domains/tools/config/tool-api.ts similarity index 100% rename from src/tools/domain/tool-api.ts rename to src/domains/tools/config/tool-api.ts diff --git a/src/domains/tools/repo/tool-cache.ts b/src/domains/tools/repo/tool-cache.ts new file mode 100644 index 0000000..5cd52e1 --- /dev/null +++ b/src/domains/tools/repo/tool-cache.ts @@ -0,0 +1,382 @@ +import type { Database } from "bun:sqlite"; +import { createHash } from "node:crypto"; +import { mkdirSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Scope } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { + LoadedTool, + RigCacheKey, + RigCacheQueryOptions, + RigToolCache, + RigToolLogger, +} from "../types/tool-types"; +import { + type DatabaseConstructor, + SqlitePlatformService, +} from "../../../providers/database/sqlite"; + +type CacheIdentity = { hash: string; json: string }; +type CacheEntry = { data: Value; updatedAt: number; invalidatedAt: number | null }; +type CacheRow = { + key_json: string; + value_json: string; + data_updated_at: number; + invalidated_at: number | null; +}; +type CacheStorage = { + readonly read: (identity: CacheIdentity) => CacheEntry | undefined; + readonly write: (identity: CacheIdentity, value: unknown) => void; + readonly invalidate: (identity: CacheIdentity) => void; + readonly remove: (identity: CacheIdentity) => void; + readonly clear: () => void; + readonly close: () => void; +}; +type CacheDatabaseResource = { + readonly get: () => Database; + readonly close: () => void; +}; + +export type ManagedRigToolCache = RigToolCache & { close(): void }; + +class ToolCacheDatabaseService extends Context.Service< + ToolCacheDatabaseService, + { + readonly create: (path: string, database: CacheDatabaseResource) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolCacheDatabaseService", { + make: Effect.gen(function* () { + const create = Effect.fn("ToolCacheDatabaseService.create")(function* ( + path: string, + database: CacheDatabaseResource, + ) { + const read = (identity: CacheIdentity) => { + const row = database + .get() + .query( + "select key_json, value_json, data_updated_at, invalidated_at from _rig_cache where key_hash = ?", + ) + .get(identity.hash) as CacheRow | null; + if (!row) return undefined; + if (row.key_json !== identity.json) { + throw new RigError({ + code: "TOOL_INVALID", + message: "context.cache detected a query key hash collision.", + details: { path }, + }); + } + database + .get() + .query( + "update _rig_cache set last_accessed_at = $lastAccessedAt where key_hash = $keyHash", + ) + .run({ keyHash: identity.hash, lastAccessedAt: Date.now() }); + return { + data: JSON.parse(row.value_json) as Value, + updatedAt: row.data_updated_at, + invalidatedAt: row.invalidated_at, + }; + }; + const write = (identity: CacheIdentity, value: unknown) => { + const valueJson = JSON.stringify(value); + if (valueJson === undefined) { + throw new RigError({ code: "INPUT_ERROR", message: "Cache cannot store undefined." }); + } + const time = Date.now(); + database + .get() + .query( + `insert into _rig_cache + (key_hash, key_json, value_json, data_updated_at, invalidated_at, last_accessed_at) + values ($keyHash, $keyJson, $valueJson, $dataUpdatedAt, null, $lastAccessedAt) + on conflict(key_hash) do update set key_json = excluded.key_json, + value_json = excluded.value_json, data_updated_at = excluded.data_updated_at, + invalidated_at = null, last_accessed_at = excluded.last_accessed_at`, + ) + .run({ + keyHash: identity.hash, + keyJson: identity.json, + valueJson, + dataUpdatedAt: time, + lastAccessedAt: time, + }); + }; + const invalidate = (identity: CacheIdentity) => + void database + .get() + .query("update _rig_cache set invalidated_at = $invalidatedAt where key_hash = $keyHash") + .run({ keyHash: identity.hash, invalidatedAt: Date.now() }); + const remove = (identity: CacheIdentity) => + void database.get().query("delete from _rig_cache where key_hash = ?").run(identity.hash); + const clear = () => database.get().run("delete from _rig_cache"); + return { read, write, invalidate, remove, clear, close: database.close } as const; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCacheDatabaseService, ToolCacheDatabaseService.make); +} + +class ToolCacheStorageService extends Context.Service< + ToolCacheStorageService, + { + readonly open: ( + path: string, + Database: DatabaseConstructor, + ) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolCacheStorageService", { + make: Effect.gen(function* () { + const open = Effect.fn("ToolCacheStorageService.open")(function* ( + path: string, + Database: DatabaseConstructor, + ) { + let database: Database | undefined; + const get = (): Database => { + if (database) return database; + mkdirSync(dirname(path), { recursive: true }); + const opened = new Database(path, { create: true, strict: true }); + try { + opened.run("PRAGMA journal_mode = WAL;"); + opened.run(`create table if not exists _rig_cache ( + key_hash text primary key, key_json text not null, value_json text not null, + data_updated_at integer not null, invalidated_at integer, + last_accessed_at integer not null + );`); + database = opened; + return database; + } catch (error) { + opened.close(false); + throw error; + } + }; + const close = () => database?.close(false); + return { get, close } as const; + }); + return { open } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCacheStorageService, ToolCacheStorageService.make); +} + +class ToolCacheHandleService extends Context.Service< + ToolCacheHandleService, + { + readonly create: (params: { + path: string; + storage: CacheStorage; + identity: (queryKey: RigCacheKey) => CacheIdentity; + staleTime: (value: number | undefined) => number; + }) => Effect.Effect; + readonly unavailable: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolCacheHandleService", { + make: Effect.gen(function* () { + const create = Effect.fn("ToolCacheHandleService.create")(function* (params: { + path: string; + storage: CacheStorage; + identity: (queryKey: RigCacheKey) => CacheIdentity; + staleTime: (value: number | undefined) => number; + }) { + const inFlight = new Map>(); + const refresh = (key: CacheIdentity, query: () => Value | Promise) => { + const existing = inFlight.get(key.hash); + if (existing) return existing as Promise; + const promise = Promise.resolve(query()).then((value) => { + params.storage.write(key, value); + return value; + }); + inFlight.set(key.hash, promise); + void promise.then( + () => inFlight.delete(key.hash), + () => inFlight.delete(key.hash), + ); + return promise; + }; + const query = async (options: RigCacheQueryOptions) => { + const key = params.identity(options.queryKey); + const freshnessWindow = params.staleTime(options.staleTime); + const entry = params.storage.read(key); + if (!entry) return await refresh(key, options.queryFn); + const fresh = + entry.invalidatedAt === null && Date.now() - entry.updatedAt < freshnessWindow; + return fresh ? entry.data : await refresh(key, options.queryFn); + }; + return { + path: params.path, + query, + peek: (key: RigCacheKey) => + params.storage.read(params.identity(key))?.data, + set: (key: RigCacheKey, value: Value) => + params.storage.write(params.identity(key), value), + invalidate: (key: RigCacheKey) => params.storage.invalidate(params.identity(key)), + remove: (key: RigCacheKey) => params.storage.remove(params.identity(key)), + clear: params.storage.clear, + close: params.storage.close, + }; + }); + const unavailable = Effect.fn("ToolCacheHandleService.unavailable")(function* (path: string) { + const fail = (): never => { + throw new RigError({ + code: "TOOL_INVALID", + message: "context.cache requires the Bun SQLite runtime.", + details: { path }, + }); + }; + return { + path, + query: fail, + peek: fail, + set: fail, + invalidate: fail, + remove: fail, + clear: fail, + close: () => undefined, + }; + }); + return { create, unavailable } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCacheHandleService, ToolCacheHandleService.make); +} + +class ToolCacheResourceService extends Context.Service< + ToolCacheResourceService, + { + readonly open: ( + path: string, + Database: DatabaseConstructor | undefined, + ) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolCacheResourceService", { + make: Effect.gen(function* () { + const storageService = yield* ToolCacheStorageService; + const databaseService = yield* ToolCacheDatabaseService; + const handles = yield* ToolCacheHandleService; + const invalidKeyMessage = "context.cache query keys must contain only JSON-compatible values."; + const invalidKey = () => new RigError({ code: "INPUT_ERROR", message: invalidKeyMessage }); + const normalizeKey = (value: unknown, parents: Set, inArray: boolean): unknown => { + const isPrimitiveCacheValue = + value === null || typeof value === "string" || typeof value === "boolean"; + if (isPrimitiveCacheValue) return value; + if (typeof value === "number") { + if (Number.isFinite(value)) return value; + throw invalidKey(); + } + if (typeof value !== "object") { + const isOmittableUndefined = value === undefined && !inArray; + if (isOmittableUndefined) return undefined; + throw invalidKey(); + } + if (parents.has(value)) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Cache query keys cannot contain cycles.", + }); + } + parents.add(value); + try { + if (Array.isArray(value)) return value.map((item) => normalizeKey(item, parents, true)); + const prototype = Object.getPrototypeOf(value); + const hasUnsupportedPrototype = prototype !== Object.prototype && prototype !== null; + if (hasUnsupportedPrototype) throw invalidKey(); + const normalized: Record = {}; + for (const key of Object.keys(value).toSorted()) { + const item = normalizeKey((value as Record)[key], parents, false); + if (item !== undefined) normalized[key] = item; + } + return normalized; + } finally { + parents.delete(value); + } + }; + const identity = (queryKey: RigCacheKey): CacheIdentity => { + const isInvalidQueryKey = !Array.isArray(queryKey) || queryKey.length === 0; + if (isInvalidQueryKey) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Cache query keys must be non-empty arrays.", + }); + } + const json = JSON.stringify(normalizeKey(queryKey, new Set(), true)); + return { hash: createHash("sha256").update(json).digest("hex"), json }; + }; + const staleTime = (value: number | undefined): number => { + const normalized = value ?? 0; + const isInvalidStaleTime = + typeof normalized !== "number" || Number.isNaN(normalized) || normalized < 0; + if (isInvalidStaleTime) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Cache staleTime must be a non-negative number.", + }); + } + return normalized; + }; + const open = Effect.fn("ToolCacheResourceService.open")(function* ( + path: string, + Database: DatabaseConstructor | undefined, + ) { + if (!Database) return yield* handles.unavailable(path); + const database = yield* storageService.open(path, Database); + const storage = yield* databaseService.create(path, database); + return yield* handles.create({ + path, + storage, + identity, + staleTime, + }); + }); + return { open } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCacheResourceService, ToolCacheResourceService.make); +} + +export class ToolCacheService extends Context.Service< + ToolCacheService, + { + readonly pathForToolPath: (toolPath: string) => Effect.Effect; + readonly acquire: ( + tool: LoadedTool, + log: RigToolLogger, + ) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolCacheService", { + make: Effect.gen(function* () { + const databases = yield* SqlitePlatformService; + const resources = yield* ToolCacheResourceService; + const pathForToolPath = Effect.fn("ToolCacheService.pathForToolPath")(function* ( + toolPath: string, + ) { + return join(dirname(toolPath), "cache.sqlite"); + }); + const acquire = Effect.fn("ToolCacheService.acquire")(function* ( + tool: LoadedTool, + log: RigToolLogger, + ) { + void log; + const path = yield* pathForToolPath(tool.path); + const Database = (yield* databases.available) ? yield* databases.load : undefined; + return yield* Effect.acquireRelease(resources.open(path, Database), (resource) => + Effect.sync(resource.close), + ); + }); + return { pathForToolPath, acquire } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCacheService, ToolCacheService.make); +} + +const toolCacheResourceLayer = ToolCacheResourceService.layer.pipe( + Layer.provide( + Layer.mergeAll( + ToolCacheStorageService.layer, + ToolCacheDatabaseService.layer, + ToolCacheHandleService.layer, + ), + ), +); +export const toolCacheLayer = ToolCacheService.layer.pipe( + Layer.provide(Layer.merge(SqlitePlatformService.layer, toolCacheResourceLayer)), +); diff --git a/src/domains/tools/repo/tool-database.ts b/src/domains/tools/repo/tool-database.ts new file mode 100644 index 0000000..d532b96 --- /dev/null +++ b/src/domains/tools/repo/tool-database.ts @@ -0,0 +1,173 @@ +import type { Database } from "bun:sqlite"; +import { createHash } from "node:crypto"; +import { mkdir } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Scope } from "effect"; +import { SqlitePlatformService } from "../../../providers/database/sqlite"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { LoadedTool, RigToolDatabase } from "../types/tool-types"; + +type MigrationRow = { name: string; checksum: string }; + +type DatabaseMigrator = { + readonly ensureMetadata: () => void; + readonly migrate: (version: number, name: string, sql: string) => void; +}; + +class DatabaseMigrationService extends Context.Service< + DatabaseMigrationService, + { + readonly create: (database: Database, nowIso: () => string) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/DatabaseMigrationService", { + make: Effect.gen(function* () { + const checksum = (version: number, name: string, sql: string) => + createHash("sha256").update(`${version}\0${name}\0${sql}`).digest("hex"); + const create = Effect.fn("DatabaseMigrationService.create")(function* ( + database: Database, + nowIso: () => string, + ) { + let lastVersion = 0; + const ensureMetadata = () => { + database.run(` + create table if not exists _rig_migrations ( + version integer primary key, + name text not null, + checksum text not null, + applied_at text not null + ); + `); + }; + const migrate = (version: number, name: string, sql: string) => { + const isInvalidVersion = !Number.isInteger(version) || version <= 0; + if (isInvalidVersion) { + throw new RigError({ + code: "TOOL_INVALID", + message: `Migration version must be a positive integer: ${version}`, + }); + } + if (version <= lastVersion) { + throw new RigError({ + code: "TOOL_INVALID", + message: `Migration versions must be declared in ascending order: ${version} after ${lastVersion}`, + }); + } + const hasMissingContent = !name.trim() || !sql.trim(); + if (hasMissingContent) { + throw new RigError({ + code: "TOOL_INVALID", + message: !name.trim() + ? "Migration name must not be empty." + : `Migration ${version} SQL must not be empty.`, + }); + } + lastVersion = version; + const actual = { name, checksum: checksum(version, name, sql) }; + const existing = database + .query("select name, checksum from _rig_migrations where version = ?") + .get(version) as MigrationRow | null; + if (existing) { + const isUnchangedMigration = + existing.name === actual.name && existing.checksum === actual.checksum; + if (isUnchangedMigration) return; + throw new RigError({ + code: "TOOL_INVALID", + message: `Migration ${version} has changed since it was applied.`, + details: { version, expected: existing, actual }, + }); + } + database.transaction(() => { + database.run(sql); + database + .query( + `insert into _rig_migrations (version, name, checksum, applied_at) + values ($version, $name, $checksum, $appliedAt)`, + ) + .run({ version, name, checksum: actual.checksum, appliedAt: nowIso() }); + })(); + }; + return { ensureMetadata, migrate } as const; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(DatabaseMigrationService, DatabaseMigrationService.make); +} + +export class ToolDatabaseService extends Context.Service< + ToolDatabaseService, + { + readonly pathForToolPath: (toolPath: string) => Effect.Effect; + readonly acquire: ( + tool: LoadedTool, + ) => Effect.Effect; + readonly unavailable: (toolName: string) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolDatabaseService", { + make: Effect.gen(function* () { + const sqlite = yield* SqlitePlatformService; + const migrations = yield* DatabaseMigrationService; + const toRigError = (error: unknown) => { + if (error instanceof RigError) return error; + return new RigError({ + code: "INTERNAL_ERROR", + message: error instanceof Error ? error.message : String(error), + }); + }; + const pathForToolPath = Effect.fn("ToolDatabaseService.pathForToolPath")(function* ( + toolPath: string, + ) { + return join(dirname(toolPath), "index.sqlite"); + }); + const acquire = Effect.fn("ToolDatabaseService.acquire")(function* (tool: LoadedTool) { + if (!tool.definition.setupDb) return undefined; + const databasePath = yield* pathForToolPath(tool.path); + yield* Effect.tryPromise({ + try: () => mkdir(dirname(databasePath), { recursive: true }), + catch: toRigError, + }); + const Database = yield* sqlite.load; + const database = yield* Effect.acquireRelease( + Effect.try({ + try: () => new Database(databasePath, { create: true, strict: true }) as RigToolDatabase, + catch: toRigError, + }), + (resource) => Effect.sync(() => resource.close(false)).pipe(Effect.ignore), + ); + const migrator = yield* migrations.create(database, () => new Date().toISOString()); + yield* Effect.tryPromise({ + try: async () => { + Object.defineProperty(database, "path", { value: databasePath, enumerable: true }); + Object.defineProperty(database, "migrate", { + value: (version: number, name: string, sql: string) => + migrator.migrate(version, name, sql), + enumerable: true, + }); + database.run("PRAGMA journal_mode = WAL;"); + migrator.ensureMetadata(); + await tool.definition.setupDb?.(database); + }, + catch: toRigError, + }); + return database; + }); + const unavailable = Effect.fn("ToolDatabaseService.unavailable")(function* (toolName: string) { + return new Proxy({} as RigToolDatabase, { + get: () => { + throw new RigError({ + code: "TOOL_INVALID", + message: `Tool ${toolName} must define setupDb before using context.db.`, + details: { tool: toolName }, + }); + }, + }); + }); + return { pathForToolPath, acquire, unavailable } as const; + }), +}) { + static readonly layer = Layer.effect(ToolDatabaseService, ToolDatabaseService.make); +} + +export const toolDatabaseLayer = ToolDatabaseService.layer.pipe( + Layer.provide(Layer.merge(SqlitePlatformService.layer, DatabaseMigrationService.layer)), +); diff --git a/src/domains/tools/repo/tool-kv.ts b/src/domains/tools/repo/tool-kv.ts new file mode 100644 index 0000000..09d1e0a --- /dev/null +++ b/src/domains/tools/repo/tool-kv.ts @@ -0,0 +1,140 @@ +import type { Database } from "bun:sqlite"; +import { mkdirSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Scope } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { LoadedTool, RigToolKvStore } from "../types/tool-types"; +import { + type DatabaseConstructor, + SqlitePlatformService, +} from "../../../providers/database/sqlite"; + +type KvRow = { value_json: string }; +export type ManagedRigToolKvStore = RigToolKvStore & { close(): void }; + +class ToolKvResourceService extends Context.Service< + ToolKvResourceService, + { + readonly open: ( + path: string, + Database: DatabaseConstructor | undefined, + ) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolKvResourceService", { + make: Effect.gen(function* () { + const validateKey = (key: string) => { + const isInvalidKey = typeof key !== "string" || key.length === 0; + if (isInvalidKey) { + throw new RigError({ + code: "INPUT_ERROR", + message: "context.kv keys must be non-empty strings.", + details: { key }, + }); + } + }; + const create = (database: Database, path: string): ManagedRigToolKvStore => { + const get = (key: string) => { + validateKey(key); + const row = database + .query("select value_json from _rig_kv where key = ?") + .get(key) as KvRow | null; + return row ? (JSON.parse(row.value_json) as Value) : undefined; + }; + const set = (key: string, value: unknown) => { + validateKey(key); + const valueJson = JSON.stringify(value); + if (valueJson === undefined) { + throw new RigError({ + code: "INPUT_ERROR", + message: "context.kv cannot store undefined.", + }); + } + database + .query( + `insert into _rig_kv (key, value_json, updated_at) + values ($key, $valueJson, $updatedAt) + on conflict(key) do update set value_json = excluded.value_json, + updated_at = excluded.updated_at`, + ) + .run({ key, valueJson, updatedAt: new Date().toISOString() }); + }; + const close = () => database.close(false); + return { path, get, set, close } as const; + }; + const unavailable = (path: string): ManagedRigToolKvStore => { + const fail = (): never => { + throw new RigError({ + code: "TOOL_INVALID", + message: "context.kv requires the Bun SQLite runtime.", + details: { path }, + }); + }; + return { path, get: fail, set: fail, close: () => undefined }; + }; + const openResource = (path: string, Database: DatabaseConstructor) => { + mkdirSync(dirname(path), { recursive: true }); + const database = new Database(path, { create: true, strict: true }); + try { + database.run("PRAGMA journal_mode = WAL;"); + database.run(`create table if not exists _rig_kv ( + key text primary key, value_json text not null, updated_at text not null + );`); + return create(database, path); + } catch (error) { + database.close(false); + throw error; + } + }; + const open = Effect.fn("ToolKvResourceService.open")(function* ( + path: string, + Database: DatabaseConstructor | undefined, + ) { + if (!Database) return unavailable(path); + let store: ManagedRigToolKvStore | undefined; + const resource = () => (store ??= openResource(path, Database)); + return { + path, + get: (key: string) => resource().get(key), + set: (key: string, value: unknown) => resource().set(key, value), + close: () => store?.close(), + }; + }); + return { open } as const; + }), +}) { + static readonly layer = Layer.effect(ToolKvResourceService, ToolKvResourceService.make); +} + +export class ToolKvService extends Context.Service< + ToolKvService, + { + readonly pathForToolPath: (toolPath: string) => Effect.Effect; + readonly acquire: ( + tool: LoadedTool, + ) => Effect.Effect; + } +>()("@rendotdev/rig/persistence/ToolKvService", { + make: Effect.gen(function* () { + const databases = yield* SqlitePlatformService; + const resources = yield* ToolKvResourceService; + const pathForToolPath = Effect.fn("ToolKvService.pathForToolPath")(function* ( + toolPath: string, + ) { + return join(dirname(toolPath), "kv.sqlite"); + }); + const acquire = Effect.fn("ToolKvService.acquire")(function* (tool: LoadedTool) { + const path = yield* pathForToolPath(tool.path); + const Database = (yield* databases.available) ? yield* databases.load : undefined; + return yield* Effect.acquireRelease(resources.open(path, Database), (resource) => + Effect.sync(resource.close), + ); + }); + return { pathForToolPath, acquire } as const; + }), +}) { + static readonly layer = Layer.effect(ToolKvService, ToolKvService.make); +} + +export const toolKvLayer = ToolKvService.layer.pipe( + Layer.provide(Layer.merge(SqlitePlatformService.layer, ToolKvResourceService.layer)), +); diff --git a/src/domains/tools/runtime/agent-instruction-sync.ts b/src/domains/tools/runtime/agent-instruction-sync.ts new file mode 100644 index 0000000..f0c8e25 --- /dev/null +++ b/src/domains/tools/runtime/agent-instruction-sync.ts @@ -0,0 +1,292 @@ +import { join } from "node:path"; +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigAgentInstructions } from "../config/agent-instructions"; +import { atomicFileWriterLayer } from "../../../providers/filesystem/atomic-file-writer"; +import { + RigPathsConfigService, + RigPathsService, + rigPathsConfigLayer, + rigPathsLayer, +} from "../../../providers/paths/rig-paths"; +import { runtimeSupportRendererLayer } from "./runtime-support"; +import { RigError } from "../../../providers/errors/rig-error"; +import { registryLayer } from "../../registry/service/registry"; +import { toolDiscoveryLayer } from "../../registry/service/tool-discovery"; +import { rigShellLayer } from "../../../providers/process/rig-shell-service"; +import { toolIdentifierLayer } from "../service/tool-identifier"; +import { schemaRendererLayer } from "../service/schema-renderer"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { toolEnvironmentLayer } from "./tool-environment"; +import { toolLoaderLayer } from "./tool-loader"; +import { ToolApiMigrationService, toolApiMigrationLayer } from "./tool-api-migration"; +import { ToolListService, toolListLayer, toolMetadataCacheLayer } from "./tool-list"; +import { rigToolKitLayer } from "./tool-sdk"; +import { + AgentInstructionDocumentsService, + agentInstructionDocumentsLayer, +} from "../service/agent-instruction-documents"; +import { + AgentInstructionEndMarker, + AgentInstructionStartMarker, + type AgentInstructionSyncResult, + type AgentInstructionTarget, +} from "../types/agent-instruction"; +import { + AgentInstructionTargetsConfigService, + AgentInstructionTargetsService, + agentInstructionTargetsLayer, +} from "./agent-instruction-targets"; +import { + AgentSyncFingerprintService, + agentSyncFingerprintLayer, +} from "../service/agent-sync-fingerprint"; + +type AgentSyncStamp = { + sourceFingerprint: string; + targetFingerprints: Record; +}; + +class AgentInstructionSyncStateService extends Context.Service< + AgentInstructionSyncStateService, + { + readonly canSkip: (targets: AgentInstructionTarget[]) => Effect.Effect; + readonly projectHasRegistry: Effect.Effect; + readonly writeStamp: (targets: AgentInstructionTarget[]) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionSyncStateService", { + make: Effect.gen(function* () { + const documents = yield* AgentInstructionDocumentsService; + const targets = yield* AgentInstructionTargetsService; + const fingerprints = yield* AgentSyncFingerprintService; + const pathConfig = yield* RigPathsConfigService; + const paths = yield* RigPathsService; + const syncStampPath = join(pathConfig.rigDir, ".agent-sync-stamp"); + const canSkip = Effect.fn("AgentInstructionSyncStateService.canSkip")(function* ( + instructionTargets: AgentInstructionTarget[], + ) { + return yield* Effect.gen(function* () { + const stamp = JSON.parse(yield* documents.readText(syncStampPath)) as AgentSyncStamp; + const [sourceFingerprint, targetFingerprints] = yield* Effect.all([ + fingerprints.source, + fingerprints.targets(instructionTargets), + ]); + return Boolean( + sourceFingerprint && + targetFingerprints && + stamp.sourceFingerprint === sourceFingerprint && + JSON.stringify(stamp.targetFingerprints) === JSON.stringify(targetFingerprints), + ); + }).pipe(Effect.orElseSucceed(() => false)); + }); + const writeStamp = Effect.fn("AgentInstructionSyncStateService.writeStamp")(function* ( + instructionTargets: AgentInstructionTarget[], + ) { + yield* Effect.gen(function* () { + const [sourceFingerprint, targetFingerprints] = yield* Effect.all([ + fingerprints.source, + fingerprints.targets(instructionTargets.map((target) => ({ ...target, existed: true }))), + ]); + const hasIncompleteFingerprints = !sourceFingerprint || !targetFingerprints; + if (hasIncompleteFingerprints) return; + yield* documents.writeText( + syncStampPath, + `${JSON.stringify({ sourceFingerprint, targetFingerprints } satisfies AgentSyncStamp)}\n`, + ); + }).pipe(Effect.ignore); + }); + const projectHasRegistry = Effect.gen(function* () { + const configSource = yield* documents.readText(pathConfig.configPath); + const parsed = yield* Effect.try({ + try: () => JSON.parse(configSource) as unknown, + catch: (cause) => + new RigError({ + code: "CONFIG_INVALID", + message: `Config is not valid JSON at ${pathConfig.configPath}.`, + details: { cause }, + }), + }); + if (!Predicate.isObject(parsed)) return false; + const root = yield* documents.safeRealPath(yield* targets.projectRoot); + const custom = Array.isArray(parsed.customRegistries) + ? parsed.customRegistries.filter((value): value is string => typeof value === "string") + : []; + const registries = yield* Effect.all([ + paths.resolve( + typeof parsed.baseRegistryDir === "string" + ? parsed.baseRegistryDir + : pathConfig.defaultBaseRegistryDir, + ), + ...custom.map(paths.resolve), + ]); + const resolved = yield* Effect.all(registries.map(documents.safeRealPath)); + return resolved.some((path) => path === root || path.startsWith(`${root}/`)); + }).pipe(Effect.withSpan("AgentInstructionSyncStateService.projectHasRegistry")); + return { canSkip, projectHasRegistry, writeStamp } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionSyncStateService, + AgentInstructionSyncStateService.make, + ); +} + +export class AgentInstructionSyncService extends Context.Service< + AgentInstructionSyncService, + { + readonly sync: Effect.Effect; + readonly discoverTargets: Effect.Effect; + readonly renderBlock: ( + toolList: string, + migrationInstructions?: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionSyncService", { + make: Effect.gen(function* () { + const documents = yield* AgentInstructionDocumentsService; + const targets = yield* AgentInstructionTargetsService; + const state = yield* AgentInstructionSyncStateService; + const toolList = yield* ToolListService; + const migrations = yield* ToolApiMigrationService; + const renderBlock = Effect.fn("AgentInstructionSyncService.renderBlock")(function* ( + list: string, + migrationInstructions = "", + ) { + return `${AgentInstructionStartMarker} + +## Rig local tools + +${RigAgentInstructions}${migrationInstructions ? `${migrationInstructions}\n\n` : ""} +### Available Rig tools + +\`\`\`text +${list} +\`\`\` + +${AgentInstructionEndMarker}`; + }); + const discoverTargets = targets.discover.pipe( + Effect.withSpan("AgentInstructionSyncService.discoverTargets"), + ); + const sync = Effect.gen(function* () { + if (process.env.RIG_AGENT_SYNC === "0") return { skipped: true, targets: [] }; + const instructionTargets = yield* targets.discover; + if (instructionTargets.length === 0) return { skipped: false, targets: [] }; + if (yield* state.canSkip(instructionTargets)) { + return { + skipped: false, + targets: instructionTargets.map((target) => ({ ...target, changed: false })), + }; + } + const migrationInstructions = yield* migrations.renderAgentInstructions( + yield* migrations.inspect(), + ); + const hasProjectRegistry = yield* state.projectHasRegistry; + const updates = yield* Effect.all( + instructionTargets.map((target) => + Effect.gen(function* () { + const shouldRemoveInvisibleRegistryBlock = + target.scope === "visible" && !hasProjectRegistry; + if (shouldRemoveInvisibleRegistryBlock) { + return { ...target, changed: yield* documents.removeManagedBlock(target) }; + } + const data = yield* toolList.list( + target.scope === "visible" ? { visibleFromPath: target.path } : {}, + ); + const block = yield* renderBlock( + yield* toolList.renderPlain(data), + migrationInstructions, + ); + return { ...target, changed: yield* documents.upsertManagedBlock(target, block) }; + }), + ), + { concurrency: "unbounded" }, + ); + yield* state.writeStamp(instructionTargets); + return { skipped: false, targets: updates }; + }).pipe(Effect.withSpan("AgentInstructionSyncService.sync")); + return { sync, discoverTargets, renderBlock } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionSyncService, + AgentInstructionSyncService.make, + ); +} + +const runtimeRendererLayer = runtimeSupportRendererLayer.pipe(Layer.provide(rigPathsConfigLayer)); +const registryForAgentLayer = registryLayer.pipe( + Layer.provide(Layer.merge(atomicFileWriterLayer, runtimeRendererLayer)), +); +const discoveryForAgentLayer = toolDiscoveryLayer.pipe( + Layer.provide(Layer.merge(registryForAgentLayer, rigPathsLayer)), +); +const toolkitForAgentLayer = rigToolKitLayer.pipe( + Layer.provide(Layer.mergeAll(rigShellLayer, rigPathsConfigLayer, rigPathsLayer)), +); +const loaderForAgentLayer = toolLoaderLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolEnvironmentLayer, + discoveryForAgentLayer, + toolDefinitionLayer, + toolIdentifierLayer, + toolkitForAgentLayer, + ), + ), +); +const metadataForAgentLayer = toolMetadataCacheLayer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + loaderForAgentLayer, + atomicFileWriterLayer, + schemaRendererLayer, + toolIdentifierLayer, + ), + ), +); +const toolListForAgentLayer = toolListLayer.pipe( + Layer.provide(Layer.mergeAll(discoveryForAgentLayer, metadataForAgentLayer, toolIdentifierLayer)), +); +const migrationForAgentLayer = toolApiMigrationLayer.pipe(Layer.provide(discoveryForAgentLayer)); +const documentsForAgentLayer = agentInstructionDocumentsLayer.pipe( + Layer.provide(atomicFileWriterLayer), +); +const targetsForAgentLayer = agentInstructionTargetsLayer.pipe( + Layer.provide( + Layer.mergeAll( + documentsForAgentLayer, + rigPathsConfigLayer, + rigPathsLayer, + AgentInstructionTargetsConfigService.layer, + ), + ), +); +const fingerprintsForAgentLayer = agentSyncFingerprintLayer.pipe( + Layer.provide(Layer.merge(rigPathsConfigLayer, rigPathsLayer)), +); +const stateForAgentLayer = AgentInstructionSyncStateService.layer.pipe( + Layer.provide( + Layer.mergeAll( + documentsForAgentLayer, + targetsForAgentLayer, + fingerprintsForAgentLayer, + rigPathsConfigLayer, + rigPathsLayer, + ), + ), +); + +export const agentInstructionSyncLayer = AgentInstructionSyncService.layer.pipe( + Layer.provide( + Layer.mergeAll( + documentsForAgentLayer, + targetsForAgentLayer, + stateForAgentLayer, + toolListForAgentLayer, + migrationForAgentLayer, + rigPathsConfigLayer, + rigPathsLayer, + ), + ), +); diff --git a/src/domains/tools/runtime/agent-instruction-targets.ts b/src/domains/tools/runtime/agent-instruction-targets.ts new file mode 100644 index 0000000..9e273d8 --- /dev/null +++ b/src/domains/tools/runtime/agent-instruction-targets.ts @@ -0,0 +1,247 @@ +import { existsSync } from "node:fs"; +import { dirname, isAbsolute, join, resolve } from "node:path"; +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigPathsConfigService, RigPathsService } from "../../../providers/paths/rig-paths"; +import { AgentInstructionDocumentsService } from "../service/agent-instruction-documents"; +import { + AgentInstructionSyncLocations, + type AgentInstructionTarget, +} from "../types/agent-instruction"; + +export class AgentInstructionTargetsConfigService extends Context.Service< + AgentInstructionTargetsConfigService, + { readonly cwd: string } +>()("@rendotdev/rig/generated/agent/AgentInstructionTargetsConfigService", { + make: Effect.sync(() => ({ cwd: process.cwd() })), +}) { + static readonly layer = Layer.effect( + AgentInstructionTargetsConfigService, + AgentInstructionTargetsConfigService.make, + ); +} + +class AgentInstructionProjectPathsService extends Context.Service< + AgentInstructionProjectPathsService, + { + readonly instructionScope: (path: string) => Effect.Effect; + readonly projectDirectories: Effect.Effect; + readonly projectRoot: Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionProjectPathsService", { + make: Effect.gen(function* () { + const pathConfig = yield* RigPathsConfigService; + const { cwd } = yield* AgentInstructionTargetsConfigService; + const isGitRoot = (directory: string): boolean => existsSync(join(directory, ".git")); + const isPackageRoot = (directory: string): boolean => + existsSync(join(directory, "package.json")); + const findProjectRoot = (directory: string): string => { + let current = directory; + let packageRoot: string | undefined; + while (true) { + if (isGitRoot(current)) return current; + const foundFirstPackageRoot = !packageRoot && isPackageRoot(current); + if (foundFirstPackageRoot) packageRoot = current; + const parent = dirname(current); + if (parent === current) return packageRoot ?? directory; + current = parent; + } + }; + const projectRoot = Effect.sync(() => findProjectRoot(resolve(cwd))).pipe( + Effect.withSpan("AgentInstructionProjectPathsService.projectRoot"), + ); + const projectDirectories = Effect.sync(() => { + const root = findProjectRoot(resolve(cwd)); + const directories: string[] = []; + let current = resolve(cwd); + while (true) { + directories.push(current); + if (current === root) return directories; + current = dirname(current); + } + }).pipe(Effect.withSpan("AgentInstructionProjectPathsService.projectDirectories")); + const instructionScope = Effect.fn("AgentInstructionProjectPathsService.instructionScope")( + function* (path: string) { + let current = dirname(path); + const home = resolve(pathConfig.homeDir); + while (true) { + const isProjectBoundary = isGitRoot(current) || isPackageRoot(current); + if (isProjectBoundary) return "visible"; + if (current === home) return "all"; + const parent = dirname(current); + if (parent === current) return "visible"; + current = parent; + } + }, + ); + return { instructionScope, projectDirectories, projectRoot } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionProjectPathsService, + AgentInstructionProjectPathsService.make, + ); +} + +class AgentInstructionTargetCollectorService extends Context.Service< + AgentInstructionTargetCollectorService, + { + readonly addClaudeDirectory: ( + targets: Map, + directory: string, + scope: AgentInstructionTarget["scope"], + ) => Effect.Effect; + readonly addExistingFile: ( + targets: Map, + path: string, + scope: AgentInstructionTarget["scope"], + ) => Effect.Effect; + readonly addOpenCodeConfig: ( + targets: Map, + configPath: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionTargetCollectorService", { + make: Effect.gen(function* () { + const documents = yield* AgentInstructionDocumentsService; + const paths = yield* RigPathsService; + const projectPaths = yield* AgentInstructionProjectPathsService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const setTarget = Effect.fn("AgentInstructionTargetCollectorService.setTarget")(function* ( + targets: Map, + target: AgentInstructionTarget, + ) { + const key = target.existed ? yield* documents.safeRealPath(target.path) : target.path; + const current = targets.get(key); + const scope = current?.scope === "all" || target.scope === "all" ? "all" : "visible"; + const shouldReplaceCurrent = !current || target.path < current.path; + if (shouldReplaceCurrent) targets.set(key, { ...target, scope }); + else current.scope = scope; + }); + const addExistingFile = Effect.fn("AgentInstructionTargetCollectorService.addExistingFile")( + function* ( + targets: Map, + path: string, + scope: AgentInstructionTarget["scope"], + ) { + const isEligibleFile = + (yield* documents.isFile(path)) && !(yield* documents.isIgnored(path)); + if (isEligibleFile) { + yield* setTarget(targets, { path, existed: true, scope }); + } + }, + ); + const addClaudeDirectory = Effect.fn( + "AgentInstructionTargetCollectorService.addClaudeDirectory", + )(function* ( + targets: Map, + directory: string, + scope: AgentInstructionTarget["scope"], + ) { + const path = join(directory, "CLAUDE.md"); + const existed = yield* documents.isFile(path); + const isIgnoredExistingFile = existed && (yield* documents.isIgnored(path)); + if (isIgnoredExistingFile) return; + const directoryExists = existed ? false : yield* documents.isDirectory(directory); + const canCreateTarget = existed || directoryExists; + if (canCreateTarget) yield* setTarget(targets, { path, existed, scope }); + }); + const addOpenCodeConfig = Effect.fn("AgentInstructionTargetCollectorService.addOpenCodeConfig")( + function* (targets: Map, configPath: string) { + const parsed = yield* documents.readText(configPath).pipe( + Effect.map((source) => JSON.parse(source) as unknown), + Effect.orElseSucceed(() => undefined), + ); + const hasNoInstructionArray = !isRecord(parsed) || !Array.isArray(parsed.instructions); + if (hasNoInstructionArray) return; + const instructions = (parsed as { instructions: unknown[] }).instructions; + yield* Effect.all( + instructions.map((instruction) => { + if (typeof instruction !== "string") return Effect.void; + return Effect.gen(function* () { + const targetPath = + instruction === "~" || instruction.startsWith("~/") + ? yield* paths.resolve(instruction) + : isAbsolute(instruction) + ? instruction + : resolve(dirname(configPath), instruction); + yield* addExistingFile( + targets, + targetPath, + yield* projectPaths.instructionScope(targetPath), + ); + }); + }), + { concurrency: "unbounded" }, + ); + }, + ); + return { addClaudeDirectory, addExistingFile, addOpenCodeConfig } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionTargetCollectorService, + AgentInstructionTargetCollectorService.make, + ); +} + +export class AgentInstructionTargetsService extends Context.Service< + AgentInstructionTargetsService, + { + readonly discover: Effect.Effect; + readonly projectRoot: Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionTargetsService", { + make: Effect.gen(function* () { + const pathConfig = yield* RigPathsConfigService; + const projectPaths = yield* AgentInstructionProjectPathsService; + const collector = yield* AgentInstructionTargetCollectorService; + const projectRoot = projectPaths.projectRoot.pipe( + Effect.withSpan("AgentInstructionTargetsService.projectRoot"), + ); + const discover = Effect.gen(function* () { + const targets = new Map(); + yield* Effect.all( + (yield* projectPaths.projectDirectories).flatMap((directory) => [ + ...AgentInstructionSyncLocations.projectFiles.map((file) => + collector.addExistingFile(targets, join(directory, file), "visible"), + ), + ...AgentInstructionSyncLocations.projectClaudeDirectories.map((name) => + collector.addClaudeDirectory(targets, join(directory, name), "visible"), + ), + ]), + { concurrency: "unbounded" }, + ); + yield* Effect.all( + [ + ...AgentInstructionSyncLocations.homeClaudeDirectories.map((path) => + collector.addClaudeDirectory(targets, join(pathConfig.homeDir, ...path), "all"), + ), + ...AgentInstructionSyncLocations.homeFiles.map((path) => + collector.addExistingFile(targets, join(pathConfig.homeDir, ...path), "all"), + ), + ...AgentInstructionSyncLocations.openCodeConfigFiles.map((path) => + collector.addOpenCodeConfig(targets, join(pathConfig.homeDir, ...path)), + ), + ], + { concurrency: "unbounded" }, + ); + return [...targets.values()].toSorted((left, right) => left.path.localeCompare(right.path)); + }).pipe(Effect.withSpan("AgentInstructionTargetsService.discover")); + return { discover, projectRoot } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionTargetsService, + AgentInstructionTargetsService.make, + ); +} + +const agentInstructionTargetCollectorLayer = AgentInstructionTargetCollectorService.layer.pipe( + Layer.provide(AgentInstructionProjectPathsService.layer), +); +export const agentInstructionTargetsLayer = AgentInstructionTargetsService.layer.pipe( + Layer.provide( + Layer.merge(AgentInstructionProjectPathsService.layer, agentInstructionTargetCollectorLayer), + ), +); diff --git a/src/domains/tools/runtime/run-test-harness-service.ts b/src/domains/tools/runtime/run-test-harness-service.ts new file mode 100644 index 0000000..95dd3fd --- /dev/null +++ b/src/domains/tools/runtime/run-test-harness-service.ts @@ -0,0 +1,96 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { CommandDefinition } from "../types/tool-types"; +import type { RunCommandOptions, RunCommandResult } from "../types/tool-runner"; +import { toolInputParserLayer } from "../service/tool-input-parser"; +import { RunInputService, ToolRunnerService, runInputLayer } from "./tool-runner"; +import { ToolFilesService, type ToolFileResult } from "./tool-files"; +import { ToolTypecheckService, type ToolTypecheckResult } from "./tool-typecheck"; +import { ToolHelpService } from "./tool-help"; +import { ToolInspectorService } from "./tool-inspector"; +import { ToolListService, type ToolListData } from "./tool-list"; +import { toolServicesLayer } from "./tool-services-layer"; +import { RuntimeSupportService } from "./runtime-support"; + +export class RunToolTestService extends Context.Service< + RunToolTestService, + { + readonly createTool: ( + name: string, + ) => Effect.Effect; + readonly renderHelp: ( + toolName: string, + commandName?: string, + ) => Effect.Effect; + readonly inspect: (toolName: string, commandName?: string) => Effect.Effect; + readonly typecheck: (toolName?: string) => Effect.Effect; + readonly list: Effect.Effect; + readonly renderList: (data: ToolListData) => Effect.Effect; + readonly runInput: ( + command: CommandDefinition, + options: RunCommandOptions, + ) => Effect.Effect<{ value: unknown; source: string }, RigError>; + readonly run: ( + tool: string, + command: string, + options?: RunCommandOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/testing/RunToolTestService", { + make: Effect.gen(function* () { + const files = yield* ToolFilesService; + const help = yield* ToolHelpService; + const inspector = yield* ToolInspectorService; + const typechecker = yield* ToolTypecheckService; + const lists = yield* ToolListService; + const inputs = yield* RunInputService; + const runner = yield* ToolRunnerService; + const runtimeSupport = yield* RuntimeSupportService; + const createTool = Effect.fn("RunToolTestService.createTool")(function* (name: string) { + yield* runtimeSupport.ensure(); + return yield* files.create(name); + }); + const renderHelp = Effect.fn("RunToolTestService.renderHelp")(function* ( + toolName: string, + commandName?: string, + ) { + return yield* help.render(toolName, commandName); + }); + const inspect = Effect.fn("RunToolTestService.inspect")(function* ( + toolName: string, + commandName?: string, + ) { + return yield* inspector.inspect(toolName, commandName); + }); + const typecheck = Effect.fn("RunToolTestService.typecheck")(function* (toolName?: string) { + yield* runtimeSupport.ensure(); + return yield* typechecker.typecheck(toolName); + }); + const list = lists.list().pipe(Effect.withSpan("RunToolTestService.list")); + const renderList = Effect.fn("RunToolTestService.renderList")(function* (data: ToolListData) { + return yield* lists.renderPlain(data); + }); + const runInput = Effect.fn("RunToolTestService.runInput")(function* ( + command: CommandDefinition, + options: RunCommandOptions, + ) { + return yield* inputs.read(command, options); + }); + const run = Effect.fn("RunToolTestService.run")(function* ( + tool: string, + command: string, + options: RunCommandOptions = {}, + ) { + yield* runtimeSupport.ensure(); + return yield* runner.run(tool, command, options); + }); + return { createTool, renderHelp, inspect, typecheck, list, renderList, runInput, run } as const; + }), +}) { + static readonly layer = Layer.effect(RunToolTestService, RunToolTestService.make); +} + +const inputLayer = runInputLayer.pipe(Layer.provide(toolInputParserLayer)); +export const runTestHarnessLayer = RunToolTestService.layer.pipe( + Layer.provide(Layer.merge(toolServicesLayer, inputLayer)), +); diff --git a/src/domains/tools/runtime/runtime-support.test.ts b/src/domains/tools/runtime/runtime-support.test.ts new file mode 100644 index 0000000..cc52d8d --- /dev/null +++ b/src/domains/tools/runtime/runtime-support.test.ts @@ -0,0 +1,128 @@ +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, expect, test } from "vite-plus/test"; +import { atomicFileWriterLayer } from "../../../providers/filesystem/atomic-file-writer"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, + rigPathsConfigLayer, +} from "../../../providers/paths/rig-paths"; +import { RuntimeSupportService, runtimeSupportRendererLayer } from "./runtime-support"; +import { + ToolRuntimeInstructionRendererService, + ToolRuntimeInstructionSyncService, +} from "./tool-runtime-instruction-sync"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; + +const directories: string[] = []; + +const makeDirectory = async (prefix: string): Promise => { + const directory = await mkdtemp(join(tmpdir(), prefix)); + directories.push(directory); + return directory; +}; + +const makePathsConfigLayer = (homeDir: string) => + rigPathsConfigLayer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(RigPathsOptionsConfigService, { homeDir }), + RigPathsPlatformService.layer, + ), + ), + ); + +const makeRuntimeSupportLayer = (homeDir: string) => { + const pathsLayer = makePathsConfigLayer(homeDir); + const rendererLayer = runtimeSupportRendererLayer.pipe(Layer.provide(pathsLayer)); + return RuntimeSupportService.layer.pipe( + Layer.provide(Layer.mergeAll(pathsLayer, atomicFileWriterLayer, rendererLayer)), + ); +}; + +afterEach(async () => { + await Promise.all( + directories.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); +}); + +test("renders the managed tool runtime prefix through its service layer", async () => { + const result = await Effect.gen(function* () { + const renderer = yield* ToolRuntimeInstructionRendererService; + const current = yield* renderer.renderPrefix(); + const withShebang = yield* renderer.upsertPrefix("#!/usr/bin/env bun\nconsole.log(1);\n"); + const legacy = yield* renderer.upsertPrefix("// rig:tool-api-version 0\nconsole.log(1);\n"); + return { current, withShebang, legacy }; + }).pipe(Effect.provide(ToolRuntimeInstructionRendererService.layer), Effect.runPromise); + + expect(result.current).toContain("// rig:tool-api-version 2"); + expect(result.current).toContain("context.cache.query"); + expect(result.withShebang).toMatch(/^#!\/usr\/bin\/env bun\n\/\/ rig:runtime-reference:start/); + expect(result.withShebang).toContain("// rig:tool-api-version 1"); + expect(result.legacy).toContain("// rig:tool-api-version 1"); +}); + +test("syncs runtime instructions idempotently through service dependencies", async () => { + const home = await makeDirectory("rig-runtime-sync-"); + const toolDir = join(home, "rig", "tools", "sample"); + const toolPath = join(toolDir, "index.rig.ts"); + await mkdir(toolDir, { recursive: true }); + await writeFile(toolPath, "export default {};\n", "utf8"); + const discovered = { + name: "sample", + registryKind: "base" as const, + registryPath: join(home, "rig", "tools"), + toolDir, + toolPath, + }; + const discoveryLayer = Layer.succeed(ToolDiscoveryService, { + discover: () => Effect.succeed([discovered]), + discoverRegistry: () => Effect.succeed([discovered]), + find: () => Effect.succeed(discovered), + projectRootFor: () => Effect.succeed(home), + }); + const syncLayer = ToolRuntimeInstructionSyncService.layer.pipe( + Layer.provide( + Layer.mergeAll( + discoveryLayer, + ToolRuntimeInstructionRendererService.layer, + atomicFileWriterLayer, + ), + ), + ); + + const updates = await Effect.gen(function* () { + const sync = yield* ToolRuntimeInstructionSyncService; + return [yield* sync.sync, yield* sync.sync] as const; + }).pipe(Effect.provide(syncLayer), Effect.runPromise); + + expect(updates[0].tools).toEqual([{ name: "sample", path: toolPath, changed: true }]); + expect(updates[1].tools).toEqual([{ name: "sample", path: toolPath, changed: false }]); + expect(await readFile(toolPath, "utf8")).toMatch(/^\/\/ rig:runtime-reference:start/); +}); + +test("generates runtime support without replacing a user registry config", async () => { + const home = await makeDirectory("rig-runtime-support-"); + const registry = join(home, "custom-registry"); + const layer = makeRuntimeSupportLayer(home); + + await Effect.gen(function* () { + const support = yield* RuntimeSupportService; + yield* support.ensure([registry]); + }).pipe(Effect.provide(layer), Effect.runPromise); + + const sdkPath = join(home, "rig", "runtime", "sdk.ts"); + const typesPath = join(home, "rig", "runtime", "types.d.ts"); + const registryConfigPath = join(registry, "tsconfig.json"); + expect(await readFile(sdkPath, "utf8")).toContain("function makeRigManagedProcess"); + expect(await readFile(typesPath, "utf8")).toContain("shell: RigShell"); + expect(await readFile(registryConfigPath, "utf8")).toContain("Generated by Rig"); + + await writeFile(registryConfigPath, "{}\n", "utf8"); + await Effect.gen(function* () { + yield* (yield* RuntimeSupportService).ensure([registry]); + }).pipe(Effect.provide(layer), Effect.runPromise); + expect(await readFile(registryConfigPath, "utf8")).toBe("{}\n"); +}); diff --git a/src/domains/tools/runtime/runtime-support.ts b/src/domains/tools/runtime/runtime-support.ts new file mode 100644 index 0000000..fb3bd7c --- /dev/null +++ b/src/domains/tools/runtime/runtime-support.ts @@ -0,0 +1,226 @@ +import { mkdir, readFile } from "node:fs/promises"; +import { createRequire } from "node:module"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { + AtomicFileWriterService, + atomicFileWriterLayer, +} from "../../../providers/filesystem/atomic-file-writer"; +import { RigPathsConfigService, rigPathsConfigLayer } from "../../../providers/paths/rig-paths"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RuntimeTypesSource } from "../config/runtime-support-types-source"; +import { RuntimeSupportSdkSourceTemplate } from "../config/runtime-support-sdk-source"; + +export class RuntimeSupportService extends Context.Service< + RuntimeSupportService, + { readonly ensure: (registryDirs?: string[]) => Effect.Effect } +>()("@rendotdev/rig/generated/RuntimeSupportService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const writer = yield* AtomicFileWriterService; + const renderer = yield* RuntimeSupportRendererService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const readTextIfExists = async (path: string): Promise => { + try { + return typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(path).text() + : await readFile(path, "utf8"); + } catch { + return undefined; + } + }; + const writeTextIfChanged = Effect.fn("RuntimeSupportService.writeTextIfChanged")(function* ( + path: string, + content: string, + ) { + if ((yield* Effect.promise(() => readTextIfExists(path))) === content) return; + yield* writer.write(path, content).pipe(Effect.mapError((error) => toError(error.cause))); + }); + const ensureRegistryTsconfig = Effect.fn("RuntimeSupportService.ensureRegistryTsconfig")( + function* (registryDir: string) { + yield* Effect.tryPromise({ + try: () => mkdir(registryDir, { recursive: true }), + catch: toError, + }); + const tsconfigPath = join(registryDir, "tsconfig.json"); + const existing = yield* Effect.promise(() => readTextIfExists(tsconfigPath)); + const hasCustomTsconfig = + existing !== undefined && !existing.startsWith("// Generated by Rig."); + if (hasCustomTsconfig) return; + yield* writeTextIfChanged(tsconfigPath, yield* renderer.registryTsconfig); + }, + ); + const ensure = Effect.fn("RuntimeSupportService.ensure")(function* ( + registryDirs: string[] = [], + ) { + yield* Effect.tryPromise({ + try: () => mkdir(paths.runtimeDir, { recursive: true }), + catch: toError, + }); + yield* Effect.all([ + writeTextIfChanged(paths.runtimeSdkPath, yield* renderer.sdk), + writeTextIfChanged(paths.runtimeTypesPath, RuntimeTypesSource), + writeTextIfChanged(paths.runtimeGlobalsPath, yield* renderer.globals), + ]); + yield* Effect.all(registryDirs.map(ensureRegistryTsconfig), { concurrency: "unbounded" }); + }); + return { ensure } as const; + }), +}) { + static readonly layer = Layer.effect(RuntimeSupportService, RuntimeSupportService.make); +} + +class RuntimeSupportSdkRendererService extends Context.Service< + RuntimeSupportSdkRendererService, + { readonly sdk: Effect.Effect } +>()("@rendotdev/rig/generated/RuntimeSupportSdkRendererService", { + make: Effect.gen(function* () { + const resolveImport = (specifier: string): string => { + const meta = import.meta as ImportMeta & { resolve?: (id: string) => string }; + return meta.resolve ? meta.resolve(specifier) : specifier; + }; + const sdk = Effect.sync(() => + RuntimeSupportSdkSourceTemplate.replace( + "__RIG_ZOD_IMPORT__", + JSON.stringify(resolveImport("zod")), + ), + ).pipe(Effect.withSpan("RuntimeSupportSdkRendererService.sdk")); + return { sdk } as const; + }), +}) { + static readonly layer = Layer.effect( + RuntimeSupportSdkRendererService, + RuntimeSupportSdkRendererService.make, + ); +} + +class RuntimeSupportGlobalsRendererService extends Context.Service< + RuntimeSupportGlobalsRendererService, + { readonly globals: Effect.Effect } +>()("@rendotdev/rig/generated/RuntimeSupportGlobalsRendererService", { + make: Effect.gen(function* () { + const runtimeGlobalsSource = (): string => `// Generated by Rig. Safe to overwrite. +import type { RigToolFactory as RigToolFactoryType, RigToolKit as RigToolKitType } from "./types"; + +declare global { + type RigToolFactory = RigToolFactoryType; + type RigToolKit = RigToolKitType; +} + +export {}; +`; + const globals = Effect.sync(runtimeGlobalsSource).pipe( + Effect.withSpan("RuntimeSupportGlobalsRendererService.globals"), + ); + return { globals } as const; + }), +}) { + static readonly layer = Layer.effect( + RuntimeSupportGlobalsRendererService, + RuntimeSupportGlobalsRendererService.make, + ); +} + +class RuntimeSupportRegistryTsconfigRendererService extends Context.Service< + RuntimeSupportRegistryTsconfigRendererService, + { readonly registryTsconfig: Effect.Effect } +>()("@rendotdev/rig/generated/RuntimeSupportRegistryTsconfigRendererService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const requireFromRuntimeSupport = createRequire(import.meta.url); + const resolvePackage = (specifier: string): string => + requireFromRuntimeSupport.resolve(`${specifier}/package.json`); + const runtimeRegistryTsconfigSource = (params: { + dirname: typeof dirname; + resolvePackage: (specifier: string) => string; + }): string => `// Generated by Rig. Safe to overwrite. +${JSON.stringify( + { + compilerOptions: { + target: "ES2022", + module: "ESNext", + moduleResolution: "Bundler", + strict: true, + noEmit: true, + skipLibCheck: true, + allowImportingTsExtensions: true, + jsx: "preserve", + lib: ["ES2023", "DOM"], + types: ["bun"], + typeRoots: [params.dirname(params.dirname(params.resolvePackage("@types/bun")))], + paths: { + zod: [join(params.dirname(params.resolvePackage("zod")), "index.d.ts")], + }, + }, + files: [paths.runtimeGlobalsPath, paths.runtimeTypesPath], + include: ["./*/index.rig.ts", "./*/index.rig.tsx"], + }, + null, + 2, +)} +`; + const registryTsconfig = Effect.sync(() => + runtimeRegistryTsconfigSource({ dirname, resolvePackage }), + ).pipe(Effect.withSpan("RuntimeSupportRegistryTsconfigRendererService.registryTsconfig")); + return { registryTsconfig } as const; + }), +}) { + static readonly layer = Layer.effect( + RuntimeSupportRegistryTsconfigRendererService, + RuntimeSupportRegistryTsconfigRendererService.make, + ); +} + +class RuntimeSupportRendererService extends Context.Service< + RuntimeSupportRendererService, + { + readonly sdk: Effect.Effect; + readonly globals: Effect.Effect; + readonly registryTsconfig: Effect.Effect; + } +>()("@rendotdev/rig/generated/RuntimeSupportRendererService", { + make: Effect.gen(function* () { + const sdkRenderer = yield* RuntimeSupportSdkRendererService; + const globalsRenderer = yield* RuntimeSupportGlobalsRendererService; + const registryRenderer = yield* RuntimeSupportRegistryTsconfigRendererService; + const sdk = sdkRenderer.sdk.pipe(Effect.withSpan("RuntimeSupportRendererService.sdk")); + const globals = globalsRenderer.globals.pipe( + Effect.withSpan("RuntimeSupportRendererService.globals"), + ); + const registryTsconfig = registryRenderer.registryTsconfig.pipe( + Effect.withSpan("RuntimeSupportRendererService.registryTsconfig"), + ); + return { sdk, globals, registryTsconfig } as const; + }), +}) { + static readonly layer = Layer.effect( + RuntimeSupportRendererService, + RuntimeSupportRendererService.make, + ); +} + +export const runtimeSupportRendererLayer = RuntimeSupportRendererService.layer.pipe( + Layer.provide( + Layer.mergeAll( + RuntimeSupportSdkRendererService.layer, + RuntimeSupportGlobalsRendererService.layer, + RuntimeSupportRegistryTsconfigRendererService.layer, + ), + ), +); + +export const runtimeSupportLayer = RuntimeSupportService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + atomicFileWriterLayer, + runtimeSupportRendererLayer.pipe(Layer.provide(rigPathsConfigLayer)), + ), + ), +); diff --git a/src/domains/tools/runtime/tool-api-migration.test.ts b/src/domains/tools/runtime/tool-api-migration.test.ts new file mode 100644 index 0000000..5a698d0 --- /dev/null +++ b/src/domains/tools/runtime/tool-api-migration.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it, vi } from "vite-plus/test"; +import { Effect, Layer } from "effect"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import { ToolApiMigrationService, toolApiMigrationLayer } from "./tool-api-migration"; + +describe("Tool API migration", () => { + it("composes the discovery dependency through the service layer", async () => { + const discover = vi.fn(() => Effect.succeed([])); + const discoveryLayer = Layer.succeed(ToolDiscoveryService, { + discover, + discoverRegistry: () => Effect.succeed([]), + find: () => Effect.die(new Error("unused")), + projectRootFor: () => Effect.succeed("/project"), + }); + + await expect( + Effect.runPromise( + ToolApiMigrationService.use((service) => service.inspect()).pipe( + Effect.provide(toolApiMigrationLayer.pipe(Layer.provide(discoveryLayer))), + ), + ), + ).resolves.toEqual({ + currentVersion: 2, + ready: true, + migrations: [], + unsupported: [], + }); + expect(discover).toHaveBeenCalledWith({}); + }); +}); diff --git a/src/domains/tools/runtime/tool-api-migration.ts b/src/domains/tools/runtime/tool-api-migration.ts new file mode 100644 index 0000000..b78fc63 --- /dev/null +++ b/src/domains/tools/runtime/tool-api-migration.ts @@ -0,0 +1,149 @@ +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { CurrentRigToolApiVersion } from "../config/tool-api"; + +type ToolApiMigrationEntry = { + name: string; + path: string; + fromVersion: number; + toVersion: number; +}; + +export type ToolApiMigrationReport = { + currentVersion: number; + ready: boolean; + migrations: ToolApiMigrationEntry[]; + unsupported: ToolApiMigrationEntry[]; +}; + +class ToolApiMigrationSourceService extends Context.Service< + ToolApiMigrationSourceService, + { readonly read: (path: string) => Effect.Effect } +>()("@rendotdev/rig/tools/migration/ToolApiMigrationSourceService", { + make: Effect.gen(function* () { + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const read = Effect.fn("ToolApiMigrationSourceService.read")(function* (path: string) { + return yield* Effect.tryPromise({ + /* v8 ignore next 4 -- Bun path is covered by distribution integration */ + try: () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? Bun.file(path).text() + : readFile(path, "utf8"), + catch: toError, + }); + }); + return { read } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolApiMigrationSourceService, + ToolApiMigrationSourceService.make, + ); +} + +export class ToolApiMigrationService extends Context.Service< + ToolApiMigrationService, + { + readonly inspect: (options?: { + visibleFromPath?: string; + }) => Effect.Effect; + readonly renderAgentInstructions: (report: ToolApiMigrationReport) => Effect.Effect; + readonly renderCli: (report: ToolApiMigrationReport) => Effect.Effect; + } +>()("@rendotdev/rig/tools/migration/ToolApiMigrationService", { + make: Effect.gen(function* () { + const discovery = yield* ToolDiscoveryService; + const source = yield* ToolApiMigrationSourceService; + const sourceVersion = (contents: string): number => { + const match = /^\/\/ rig:tool-api-version (\d+)\s*$/m.exec(contents); + if (!match) return 1; + const version = Number.parseInt(match[1]!, 10); + return Number.isSafeInteger(version) && version > 0 ? version : 1; + }; + const renderAgentInstructions = Effect.fn("ToolApiMigrationService.renderAgentInstructions")( + function* (report: ToolApiMigrationReport) { + if (report.ready) return ""; + const lines = ["### Rig tool migration required", ""]; + if (report.migrations.length > 0) { + lines.push( + `Migrate these tools to Rig tool API v${report.currentVersion}:`, + "", + ...report.migrations.map( + (entry) => + `- ${entry.name}: v${entry.fromVersion} to v${entry.toVersion} (${entry.path})`, + ), + "", + "For each tool:", + "", + "1. Export the factory directly as `(rig: RigToolKit) => rig.defineTool({ ... })`.", + "2. Remove the redundant `name`; Rig derives it from the folder.", + "3. Define `commands` as `(command) => ({ ... })` and replace each inline `rig.defineCommand({ ... })` with `command({ ... })`.", + "4. Move predeclared commands into the `commands` callback. Declare reusable command factories inside the callback or pass `command` into them.", + `5. Set the header to \`// rig:tool-api-version ${report.currentVersion}\`.`, + "6. Run `rig typecheck ` and execute the command examples before considering the migration complete.", + ); + } + if (report.unsupported.length > 0) { + lines.push( + "", + "These tools declare a newer API than this Rig installation supports:", + "", + ...report.unsupported.map( + (entry) => `- ${entry.name}: v${entry.fromVersion} (${entry.path})`, + ), + "", + "Update Rig before editing or running these tools.", + ); + } + return lines.join("\n"); + }, + ); + const renderCli = Effect.fn("ToolApiMigrationService.renderCli")(function* ( + report: ToolApiMigrationReport, + ) { + if (report.ready) return `All tools use Rig tool API v${report.currentVersion}.`; + return (yield* renderAgentInstructions(report)).replace(/^### /, ""); + }); + const inspectTool = Effect.fn("ToolApiMigrationService.inspectTool")(function* ( + tool: DiscoveredTool, + ) { + const contents = yield* source.read(tool.toolPath); + return { + name: tool.name, + path: tool.toolPath, + fromVersion: sourceVersion(contents), + toVersion: CurrentRigToolApiVersion, + }; + }); + const inspect = Effect.fn("ToolApiMigrationService.inspect")(function* ( + options: { visibleFromPath?: string } = {}, + ) { + const tools = yield* discovery.discover(options); + const entries = yield* Effect.all(tools.map(inspectTool), { concurrency: "unbounded" }); + const migrations = entries.filter((entry) => entry.fromVersion < entry.toVersion); + const unsupported = entries.filter((entry) => entry.fromVersion > entry.toVersion); + return { + currentVersion: CurrentRigToolApiVersion, + ready: migrations.length === 0 && unsupported.length === 0, + migrations, + unsupported, + }; + }); + return { inspect, renderAgentInstructions, renderCli } as const; + }), +}) { + static readonly layer = Layer.effect(ToolApiMigrationService, ToolApiMigrationService.make); +} + +export const toolApiMigrationLayer = ToolApiMigrationService.layer.pipe( + Layer.provide(ToolApiMigrationSourceService.layer), +); diff --git a/src/domains/tools/runtime/tool-collections.test.ts b/src/domains/tools/runtime/tool-collections.test.ts new file mode 100644 index 0000000..09dcfe1 --- /dev/null +++ b/src/domains/tools/runtime/tool-collections.test.ts @@ -0,0 +1,178 @@ +import { describe, expect, it } from "vite-plus/test"; +import { Effect, Layer } from "effect"; +import { + CollectionHandleFactoryService, + LazyCollectionHandleService, +} from "../../collections/runtime/collection-handle-factory"; +import type { + CollectionHandle, + ManagedCollectionHandle, +} from "../../collections/types/tool-collection"; +import { ToolCollectionsService } from "./tool-collections"; +import { toolIdentifierLayer } from "../service/tool-identifier"; + +class FaultInjectingCollectionHandle implements ManagedCollectionHandle { + readonly path = "/tmp/test"; + readonly calls: string[] = []; + initialized = false; + closeRuns = 0; + + constructor( + readonly name: string, + private readonly failure: Error | undefined, + ) {} + + async init(): Promise { + if (this.failure) throw this.failure; + this.initialized = true; + } + + close(): void { + this.closeRuns++; + } + + async create(): Promise { + this.calls.push("create"); + return { id: "created" } as never; + } + async getEntry(): Promise { + this.calls.push("getEntry"); + return null as never; + } + async update(): Promise { + this.calls.push("update"); + return { id: "updated" } as never; + } + async upsert(): Promise { + this.calls.push("upsert"); + return { id: "upserted", created: true } as never; + } + async remove(): Promise { + this.calls.push("remove"); + return true; + } + async list(): Promise { + this.calls.push("list"); + return { entries: [], total: 0 } as never; + } + async search(): Promise { + this.calls.push("search"); + return { entries: [] } as never; + } + async count(): Promise { + this.calls.push("count"); + return 1; + } + async getCollection(): Promise { + this.calls.push("getCollection"); + return [] as never; + } + async clear(): Promise { + this.calls.push("clear"); + } +} + +class FaultInjectingCollectionHandleFactory { + readonly handles: FaultInjectingCollectionHandle[] = []; + + create(name: string): FaultInjectingCollectionHandle { + const handle = new FaultInjectingCollectionHandle( + name, + name === "broken" ? new Error("collection initialization failed") : undefined, + ); + this.handles.push(handle); + return handle; + } +} + +describe("ToolCollections cleanup", () => { + it("initializes collections on first use and closes failures", async () => { + const factory = new FaultInjectingCollectionHandleFactory(); + const factoryLayer = Layer.effect( + CollectionHandleFactoryService, + Effect.gen(function* () { + const lazyHandles = yield* LazyCollectionHandleService; + return { + createLazy: >(name: string) => + lazyHandles.create(factory.create(name)), + acquire: >(name: string) => + Effect.succeed(factory.create(name) as CollectionHandle), + } as const; + }), + ).pipe(Layer.provide(LazyCollectionHandleService.layer)); + const serviceLayer = ToolCollectionsService.layer.pipe( + Layer.provide(Layer.merge(factoryLayer, toolIdentifierLayer)), + ); + const handles = await Effect.runPromise( + ToolCollectionsService.use((service) => + service.setup({ + path: "/tmp/tool/index.rig.ts", + definition: { collections: { ready: {}, broken: {}, unused: undefined } }, + } as never), + ).pipe(Effect.provide(serviceLayer)), + ); + + expect(factory.handles.every((handle) => !handle.initialized)).toBe(true); + await expect(handles?.broken?.count()).rejects.toThrow("collection initialization failed"); + expect(factory.handles.find((handle) => handle.name === "broken")?.closeRuns).toBe(1); + + const ready = handles!.ready!; + await ready.create({ data: {} }); + await ready.getEntry("created"); + await ready.update("created", { body: "updated" }); + await ready.upsert({ id: "upserted", data: {} }); + await ready.remove("created"); + await ready.list({ limit: 1 }); + await ready.search("query", { limit: 1 }); + expect(await ready.count({ status: "ready" })).toBe(1); + await ready.getCollection(() => true); + await ready.clear(); + + expect(factory.handles.find((handle) => handle.name === "ready")?.calls).toEqual([ + "create", + "getEntry", + "update", + "upsert", + "remove", + "list", + "search", + "count", + "getCollection", + "clear", + ]); + await Effect.runPromise( + ToolCollectionsService.use((service) => service.close(handles)).pipe( + Effect.provide(serviceLayer), + ), + ); + expect( + factory.handles.map((handle) => [handle.name, handle.initialized, handle.closeRuns]), + ).toEqual([ + ["ready", true, 1], + ["broken", false, 1], + ["unused", false, 0], + ]); + }); + + it("returns undefined for tools without collections", async () => { + const factoryLayer = Layer.succeed(CollectionHandleFactoryService, { + createLazy: () => Effect.die("unexpected collection"), + acquire: () => Effect.die("unexpected collection"), + }); + const serviceLayer = ToolCollectionsService.layer.pipe( + Layer.provide(Layer.merge(factoryLayer, toolIdentifierLayer)), + ); + expect( + await Effect.runPromise( + ToolCollectionsService.use((service) => + service.setup({ path: "/tmp/tool/index.rig.ts", definition: {} } as never), + ).pipe(Effect.provide(serviceLayer)), + ), + ).toBeUndefined(); + await Effect.runPromise( + ToolCollectionsService.use((service) => service.close(undefined)).pipe( + Effect.provide(serviceLayer), + ), + ); + }); +}); diff --git a/src/domains/tools/runtime/tool-collections.ts b/src/domains/tools/runtime/tool-collections.ts new file mode 100644 index 0000000..19c1545 --- /dev/null +++ b/src/domains/tools/runtime/tool-collections.ts @@ -0,0 +1,76 @@ +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Scope } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolIdentifierService, toolIdentifierLayer } from "../service/tool-identifier"; +import type { LoadedTool } from "../types/tool-types"; +import { + CollectionHandleFactoryService, + collectionHandleFactoryLayer, +} from "../../collections/runtime/collection-handle-factory"; +import type { + CollectionDefinition, + CollectionHandle, +} from "../../collections/types/tool-collection"; + +type ToolDefinitionWithCollections = { + collections?: Record; +}; + +export class ToolCollectionsService extends Context.Service< + ToolCollectionsService, + { + readonly setup: ( + tool: LoadedTool, + ) => Effect.Effect> | undefined, RigError>; + readonly acquire: ( + tool: LoadedTool, + ) => Effect.Effect> | undefined, RigError, Scope.Scope>; + readonly close: ( + handles: Record> | undefined, + ) => Effect.Effect; + } +>()("@rendotdev/rig/collections/ToolCollectionsService", { + make: Effect.gen(function* () { + const handleFactory = yield* CollectionHandleFactoryService; + const identifiers = yield* ToolIdentifierService; + const definitionsFor = (tool: LoadedTool) => + (tool.definition as ToolDefinitionWithCollections).collections; + const close = Effect.fn("ToolCollectionsService.close")(function* ( + handles: Record> | undefined, + ) { + if (!handles) return; + for (const handle of Object.values(handles)) { + (handle as CollectionHandle & { close?: () => void }).close?.(); + } + }); + const setup = Effect.fn("ToolCollectionsService.setup")(function* (tool: LoadedTool) { + const definitions = definitionsFor(tool); + const hasNoCollections = !definitions || Object.keys(definitions).length === 0; + if (hasNoCollections) return undefined; + const toolDirectory = dirname(tool.path); + const entries = yield* Effect.forEach(Object.entries(definitions), ([name, definition]) => + Effect.gen(function* () { + const collectionName = (yield* identifiers.parseCollectionName(name)).value; + const handle = yield* handleFactory.createLazy( + collectionName, + join(toolDirectory, collectionName), + definition ?? {}, + ); + return [name, handle] as const; + }), + ); + return Object.fromEntries(entries); + }); + const acquire = Effect.fn("ToolCollectionsService.acquire")(function* (tool: LoadedTool) { + return yield* Effect.acquireRelease(setup(tool), close); + }); + + return { setup, acquire, close } as const; + }), +}) { + static readonly layer = Layer.effect(ToolCollectionsService, ToolCollectionsService.make); +} + +export const toolCollectionsLayer = ToolCollectionsService.layer.pipe( + Layer.provide(Layer.merge(collectionHandleFactoryLayer, toolIdentifierLayer)), +); diff --git a/src/tools/env.test.ts b/src/domains/tools/runtime/tool-env.test.ts similarity index 54% rename from src/tools/env.test.ts rename to src/domains/tools/runtime/tool-env.test.ts index 552debb..06121bf 100644 --- a/src/tools/env.test.ts +++ b/src/domains/tools/runtime/tool-env.test.ts @@ -2,7 +2,16 @@ import { afterEach, describe, expect, test } from "vite-plus/test"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { ToolEnvServiceClass } from "./env"; +import { Effect, Layer } from "effect"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { toolIdentifierLayer } from "../service/tool-identifier"; +import { schemaRendererLayer } from "../service/schema-renderer"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { toolEnvironmentLayer } from "./tool-environment"; +import { toolLoaderLayer } from "./tool-loader"; +import { ToolEnvService, toolEnvLayer } from "./tool-env"; +import { createRigToolKit, RigToolKitService } from "./tool-sdk"; class EnvWorkspaceStore { private readonly homes: string[] = []; @@ -33,8 +42,145 @@ class EnvToolWriter { } } +const needsTokenToolSource = `export default (rig) => rig.defineTool({ + name: "needs-token", + description: "Env validation test tool.", + env: rig.z.object({ TOKEN: rig.z.string() }), + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`; + +const badEnvFileToolSource = `export default (rig) => rig.defineTool({ + name: "bad-env-file", + description: "Bad env file test tool.", + env: rig.z.object({ TOKEN: rig.z.string() }), + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`; + +const noEnvSchemaToolSource = `export default (rig) => rig.defineTool({ + name: "no-env-schema", + description: "No env schema test tool.", + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`; + +const scalarEnvToolSource = `export default (rig) => rig.defineTool({ + name: "scalar-env", + description: "Scalar env schema test tool.", + env: rig.z.string(), + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`; + const workspaces = new EnvWorkspaceStore(); +const envService = (home: string) => { + const discoveryLayer = Layer.succeed(ToolDiscoveryService, { + discover: () => Effect.succeed([]), + discoverRegistry: () => Effect.succeed([]), + find: (name: string) => + Effect.succeed({ + name, + registryKind: "base", + registryPath: join(home, "rig", "tools"), + toolDir: join(home, "rig", "tools", name), + toolPath: join(home, "rig", "tools", name, "index.rig.ts"), + } satisfies DiscoveredTool), + projectRootFor: () => Effect.succeed(home), + }); + const toolkitLayer = Layer.succeed(RigToolKitService, { + create: () => Effect.succeed(createRigToolKit({ homeDir: home })), + }); + const loaderLayer = toolLoaderLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolEnvironmentLayer, + discoveryLayer, + toolDefinitionLayer, + toolIdentifierLayer, + toolkitLayer, + ), + ), + ); + const layer = toolEnvLayer.pipe(Layer.provide(Layer.merge(loaderLayer, schemaRendererLayer))); + return { + configure: (target: string, assignments?: string[]) => + Effect.runPromise( + ToolEnvService.use((service) => service.configure(target, assignments)).pipe( + Effect.provide(layer), + ), + ), + }; +}; + +const writeValidationToolFixtures = async (writer: EnvToolWriter) => { + await writer.write("needs-token", needsTokenToolSource); + await writer.write("bad-env-file", badEnvFileToolSource, "EMPTY=\nTOKEN=abc\nnot an env line\n"); + await writer.write("no-env-schema", noEnvSchemaToolSource); + await writer.write("scalar-env", scalarEnvToolSource); +}; + +const expectEnvValidationFailures = async (service: ReturnType) => { + await expect(service.configure("needs-token.read.extra")).rejects.toThrow( + "Env target must use ", + ); + await expect(service.configure("needs-token.missing")).rejects.toThrow( + "Env target must use ", + ); + await expect(service.configure("no-env-schema")).rejects.toThrow( + "Tool no-env-schema does not define an env schema.", + ); + await expect(service.configure("needs-token", ["bad"])).rejects.toThrow( + "Env assignment must use KEY=VALUE: bad", + ); + await expect(service.configure("needs-token", ["BAD-KEY=value"])).rejects.toThrow( + "Env assignment must use KEY=VALUE: BAD-KEY=value", + ); + await expect(service.configure("needs-token", ["OTHER=value"])).rejects.toThrow( + "Tool needs-token env would be invalid.", + ); + await expect(service.configure("needs-token", ["remove"])).rejects.toThrow( + "Env remove expects at least one KEY.", + ); + await expect(service.configure("needs-token", ["remove", "BAD-KEY"])).rejects.toThrow( + "Env key must be a valid shell variable name: BAD-KEY", + ); + await expect(service.configure("needs-token", ["remove", "TOKEN"])).rejects.toThrow( + "Tool needs-token env would be invalid.", + ); + await expect(service.configure("bad-env-file")).rejects.toThrow("Invalid .env line."); + await expect(service.configure("scalar-env")).resolves.toMatchObject({ entries: [] }); +}; + afterEach(async () => { await workspaces.cleanup(); }); @@ -68,7 +214,7 @@ MODE='dev' `, ); - const service = new ToolEnvServiceClass({ homeDir: home }); + const service = envService(home); const listed = await service.configure("needs-env"); const updated = await service.configure("needs-env", [ "TOKEN=new secret", @@ -117,106 +263,10 @@ MODE='dev' }); test("reports env command validation errors", async () => { + expect.assertions(11); const home = await workspaces.create(); const writer = new EnvToolWriter(home); - await writer.write( - "needs-token", - `export default (rig) => rig.defineTool({ - name: "needs-token", - description: "Env validation test tool.", - env: rig.z.object({ TOKEN: rig.z.string() }), - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - ); - await writer.write( - "bad-env-file", - `export default (rig) => rig.defineTool({ - name: "bad-env-file", - description: "Bad env file test tool.", - env: rig.z.object({ TOKEN: rig.z.string() }), - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - "EMPTY=\nTOKEN=abc\nnot an env line\n", - ); - await writer.write( - "no-env-schema", - `export default (rig) => rig.defineTool({ - name: "no-env-schema", - description: "No env schema test tool.", - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - ); - await writer.write( - "scalar-env", - `export default (rig) => rig.defineTool({ - name: "scalar-env", - description: "Scalar env schema test tool.", - env: rig.z.string(), - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - ); - - const service = new ToolEnvServiceClass({ homeDir: home }); - await expect(service.configure("needs-token.read.extra")).rejects.toThrow( - "Env target must use ", - ); - await expect(service.configure("needs-token.missing")).rejects.toThrow( - "Env target must use ", - ); - await expect(service.configure("no-env-schema")).rejects.toThrow( - "Tool no-env-schema does not define an env schema.", - ); - await expect(service.configure("needs-token", ["bad"])).rejects.toThrow( - "Env assignment must use KEY=VALUE: bad", - ); - await expect(service.configure("needs-token", ["BAD-KEY=value"])).rejects.toThrow( - "Env assignment must use KEY=VALUE: BAD-KEY=value", - ); - await expect(service.configure("needs-token", ["OTHER=value"])).rejects.toThrow( - "Tool needs-token env would be invalid.", - ); - await expect(service.configure("needs-token", ["remove"])).rejects.toThrow( - "Env remove expects at least one KEY.", - ); - await expect(service.configure("needs-token", ["remove", "BAD-KEY"])).rejects.toThrow( - "Env key must be a valid shell variable name: BAD-KEY", - ); - await expect(service.configure("needs-token", ["remove", "TOKEN"])).rejects.toThrow( - "Tool needs-token env would be invalid.", - ); - await expect(service.configure("bad-env-file")).rejects.toThrow("Invalid .env line."); - await expect(service.configure("scalar-env")).resolves.toMatchObject({ entries: [] }); + await writeValidationToolFixtures(writer); + await expectEnvValidationFailures(envService(home)); }); }); diff --git a/src/domains/tools/runtime/tool-env.ts b/src/domains/tools/runtime/tool-env.ts new file mode 100644 index 0000000..5681cde --- /dev/null +++ b/src/domains/tools/runtime/tool-env.ts @@ -0,0 +1,338 @@ +import { existsSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Predicate } from "effect"; +import { + AtomicFileWriterService, + atomicFileWriterLayer, +} from "../../../providers/filesystem/atomic-file-writer"; +import { RigError } from "../../../providers/errors/rig-error"; +import { SchemaRendererService } from "../service/schema-renderer"; +import { ToolLoaderService } from "./tool-loader"; + +type ToolEnvEntry = { + key: string; + required: boolean; + set: boolean; +}; + +export type ToolEnvResult = { + tool: string; + envPath: string; + updated: boolean; + entries: ToolEnvEntry[]; + updatedKeys: string[]; + removedKeys: string[]; +}; + +type ToolEnvTarget = { tool: string }; +type ToolEnvSchema = { + safeParse(value: unknown): { success: boolean; error?: { flatten(): unknown } }; +}; +type ToolEnvResultParameters = { + target: ToolEnvTarget; + envPath: string; + updated: boolean; + values: Record; + schema: unknown; + updatedKeys: string[]; + removedKeys: string[]; +}; + +class ToolEnvInputService extends Context.Service< + ToolEnvInputService, + { + readonly parseTarget: (value: string) => Effect.Effect; + readonly parseKeys: (keys: string[]) => Effect.Effect; + readonly parseAssignments: ( + assignments: string[], + ) => Effect.Effect, RigError>; + } +>()("@rendotdev/rig/tools/management/ToolEnvInputService", { + make: Effect.gen(function* () { + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const parseKey = (key: string): string => { + if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) return key; + throw new RigError({ + code: "INPUT_ERROR", + message: `Env key must be a valid shell variable name: ${key}`, + }); + }; + const parseAssignment = (assignment: string): [string, string] => { + const separator = assignment.indexOf("="); + const key = assignment.slice(0, separator); + const isValidAssignment = separator > 0 && /^[A-Za-z_][A-Za-z0-9_]*$/.test(key); + if (isValidAssignment) { + return [key, assignment.slice(separator + 1)]; + } + throw new RigError({ + code: "INPUT_ERROR", + message: `Env assignment must use KEY=VALUE: ${assignment}`, + }); + }; + const parseTarget = Effect.fn("ToolEnvInputService.parseTarget")(function* (value: string) { + return yield* Effect.try({ + try: () => { + const isValidTarget = value && !value.includes("."); + if (isValidTarget) return { tool: value }; + throw new RigError({ + code: "INPUT_ERROR", + message: `Env target must use : ${value}`, + }); + }, + catch: toError, + }); + }); + const parseKeys = Effect.fn("ToolEnvInputService.parseKeys")(function* (keys: string[]) { + return yield* Effect.try({ + try: () => { + if (keys.length > 0) return keys.map(parseKey); + throw new RigError({ + code: "INPUT_ERROR", + message: "Env remove expects at least one KEY.", + }); + }, + catch: toError, + }); + }); + const parseAssignments = Effect.fn("ToolEnvInputService.parseAssignments")(function* ( + assignments: string[], + ) { + return yield* Effect.try({ + try: () => Object.fromEntries(assignments.map(parseAssignment)), + catch: toError, + }); + }); + return { parseTarget, parseKeys, parseAssignments } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEnvInputService, ToolEnvInputService.make); +} + +class ToolEnvDocumentService extends Context.Service< + ToolEnvDocumentService, + { + readonly read: (path: string) => Effect.Effect, RigError>; + readonly write: (path: string, values: Record) => Effect.Effect; + } +>()("@rendotdev/rig/tools/management/ToolEnvDocumentService", { + make: Effect.gen(function* () { + const writer = yield* AtomicFileWriterService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const parseValue = (value: string): string => { + const trimmed = value.trim(); + if (trimmed.length < 2) return trimmed; + const isDoubleQuoted = trimmed[0] === '"' && trimmed.at(-1) === '"'; + if (isDoubleQuoted) return JSON.parse(trimmed) as string; + const isSingleQuoted = trimmed[0] === "'" && trimmed.at(-1) === "'"; + if (isSingleQuoted) return trimmed.slice(1, -1); + return trimmed; + }; + const parseLine = (line: string, path: string, lineNumber: number) => { + const trimmed = line.trim(); + const isBlankOrComment = !trimmed || trimmed.startsWith("#"); + if (isBlankOrComment) return undefined; + const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(trimmed); + if (match) return { key: match[1]!, value: parseValue(match[2]!) }; + throw new RigError({ + code: "TOOL_INVALID", + message: "Invalid .env line.", + details: { path, line: lineNumber }, + }); + }; + const parseDocument = (source: string, path: string): Record => { + const values: Record = {}; + for (const [index, line] of source.split(/\r?\n/).entries()) { + const parsed = parseLine(line, path, index + 1); + if (parsed) values[parsed.key] = parsed.value; + } + return values; + }; + const renderValue = (value: string): string => + /^[A-Za-z0-9_./:=@%+,-]*$/.test(value) ? value : JSON.stringify(value); + const serialize = (values: Record): string => { + const lines = Object.entries(values) + .toSorted(([left], [right]) => left.localeCompare(right)) + .map(([key, value]) => `${key}=${renderValue(value)}`); + return `${lines.join("\n")}\n`; + }; + const read = Effect.fn("ToolEnvDocumentService.read")(function* (path: string) { + return yield* Effect.tryPromise({ + try: async () => + existsSync(path) ? parseDocument(await readFile(path, "utf8"), path) : {}, + catch: toError, + }); + }); + const write = Effect.fn("ToolEnvDocumentService.write")(function* ( + path: string, + values: Record, + ) { + yield* writer + .write(path, serialize(values)) + .pipe(Effect.mapError((error) => toError(error.cause))); + }); + return { read, write } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEnvDocumentService, ToolEnvDocumentService.make); +} + +class ToolEnvResultService extends Context.Service< + ToolEnvResultService, + { + readonly validate: (params: { + target: ToolEnvTarget; + envPath: string; + schema: ToolEnvSchema; + values: Record; + }) => Effect.Effect; + readonly create: (params: ToolEnvResultParameters) => Effect.Effect; + } +>()("@rendotdev/rig/tools/management/ToolEnvResultService", { + make: Effect.gen(function* () { + const schemas = yield* SchemaRendererService; + const schemaEntries = Effect.fn("ToolEnvResultService.schemaEntries")(function* ( + schema: unknown, + values: Record, + ) { + const jsonSchema = yield* schemas.renderJson(schema); + const hasNoObjectProperties = + !Predicate.isObject(jsonSchema) || !Predicate.isObject(jsonSchema.properties); + if (hasNoObjectProperties) return []; + const schemaRecord = jsonSchema as Record; + const properties = schemaRecord.properties as Record; + /* v8 ignore next */ + const required = Array.isArray(schemaRecord.required) ? schemaRecord.required : []; + return Object.keys(properties) + .toSorted() + .map((key) => ({ key, required: required.includes(key), set: values[key] !== undefined })); + }); + const validate = Effect.fn("ToolEnvResultService.validate")(function* (params: { + target: ToolEnvTarget; + envPath: string; + schema: ToolEnvSchema; + values: Record; + }) { + const validation = params.schema.safeParse(params.values); + if (!validation.success) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Tool ${params.target.tool} env would be invalid.`, + details: { path: params.envPath, errors: validation.error?.flatten() }, + }); + } + return undefined; + }); + const create = Effect.fn("ToolEnvResultService.create")(function* ( + params: ToolEnvResultParameters, + ) { + return { + tool: params.target.tool, + envPath: params.envPath, + updated: params.updated, + entries: yield* schemaEntries(params.schema, params.values), + updatedKeys: params.updatedKeys.toSorted(), + removedKeys: params.removedKeys.toSorted(), + }; + }); + return { validate, create } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEnvResultService, ToolEnvResultService.make); +} + +export class ToolEnvService extends Context.Service< + ToolEnvService, + { + readonly configure: ( + target: string, + assignments?: string[], + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/management/ToolEnvService", { + make: Effect.gen(function* () { + const loader = yield* ToolLoaderService; + const input = yield* ToolEnvInputService; + const documents = yield* ToolEnvDocumentService; + const results = yield* ToolEnvResultService; + const configure = Effect.fn("ToolEnvService.configure")(function* ( + targetValue: string, + assignments: string[] = [], + ) { + const target = yield* input.parseTarget(targetValue); + const loaded = yield* loader.loadDefinition(target.tool); + const schema = loaded.definition.env; + if (!schema) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Tool ${target.tool} does not define an env schema.`, + details: { tool: target.tool }, + }); + } + const envPath = join(dirname(loaded.path), ".env"); + const existing = yield* documents.read(envPath); + if (assignments.length === 0) { + return yield* results.create({ + target, + envPath, + updated: false, + values: existing, + schema, + updatedKeys: [], + removedKeys: [], + }); + } + if (assignments[0] === "remove") { + const keys = yield* input.parseKeys(assignments.slice(1)); + const nextValues = { ...existing }; + const removedKeys = keys.filter((key) => key in nextValues); + for (const key of keys) delete nextValues[key]; + yield* results.validate({ target, envPath, schema, values: nextValues }); + if (removedKeys.length > 0) yield* documents.write(envPath, nextValues); + return yield* results.create({ + target, + envPath, + updated: removedKeys.length > 0, + values: nextValues, + schema, + updatedKeys: [], + removedKeys, + }); + } + const updates = yield* input.parseAssignments(assignments); + const nextValues = { ...existing, ...updates }; + yield* results.validate({ target, envPath, schema, values: nextValues }); + yield* documents.write(envPath, nextValues); + return yield* results.create({ + target, + envPath, + updated: true, + values: nextValues, + schema, + updatedKeys: Object.keys(updates), + removedKeys: [], + }); + }); + return { configure } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEnvService, ToolEnvService.make); +} + +const documentLayer = ToolEnvDocumentService.layer.pipe(Layer.provide(atomicFileWriterLayer)); +const resultLayer = ToolEnvResultService.layer; +export const toolEnvLayer = ToolEnvService.layer.pipe( + Layer.provide(Layer.mergeAll(ToolEnvInputService.layer, documentLayer, resultLayer)), +); diff --git a/src/domains/tools/runtime/tool-environment.ts b/src/domains/tools/runtime/tool-environment.ts new file mode 100644 index 0000000..421f670 --- /dev/null +++ b/src/domains/tools/runtime/tool-environment.ts @@ -0,0 +1,148 @@ +import { existsSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import type { ToolDefinition } from "../types/tool-types"; + +class ToolEnvironmentParserService extends Context.Service< + ToolEnvironmentParserService, + { + readonly parse: ( + source: string, + path: string, + ) => Effect.Effect, RigError>; + } +>()("@rendotdev/rig/tools/loading/ToolEnvironmentParserService", { + make: Effect.gen(function* () { + const parseValue = (value: string): string => { + const trimmed = value.trim(); + if (trimmed.length < 2) return trimmed; + const quote = trimmed[0]; + const isUnwrapped = (quote !== '"' && quote !== "'") || trimmed.at(-1) !== quote; + if (isUnwrapped) return trimmed; + const inner = trimmed.slice(1, -1); + if (quote === "'") return inner; + return inner + .replaceAll("\\n", "\n") + .replaceAll("\\r", "\r") + .replaceAll("\\t", "\t") + .replaceAll('\\"', '"') + .replaceAll("\\\\", "\\"); + }; + const parse = Effect.fn("ToolEnvironmentParserService.parse")(function* ( + source: string, + path: string, + ) { + const env: Record = {}; + for (const [index, line] of source.split(/\r?\n/).entries()) { + const trimmed = line.trim(); + const isBlankOrComment = !trimmed || trimmed.startsWith("#"); + if (isBlankOrComment) continue; + const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(trimmed); + if (!match) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: "Invalid .env line.", + details: { path, line: index + 1 }, + }); + } + env[match[1]!] = parseValue(match[2]!); + } + return env; + }); + return { parse } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolEnvironmentParserService, + ToolEnvironmentParserService.make, + ); +} + +export class ToolEnvironmentService extends Context.Service< + ToolEnvironmentService, + { + readonly parse: ( + source: string, + path: string, + ) => Effect.Effect, RigError>; + readonly load: ( + tool: DiscoveredTool, + definition: ToolDefinition, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolEnvironmentService", { + make: Effect.gen(function* () { + const parser = yield* ToolEnvironmentParserService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const parse = Effect.fn("ToolEnvironmentService.parse")(function* ( + source: string, + path: string, + ) { + return yield* parser.parse(source, path); + }); + const bunFile = (): + | ((path: string) => { exists(): Promise; text(): Promise }) + | undefined => { + const candidate = (globalThis as typeof globalThis & { Bun?: { file?: unknown } }).Bun?.file; + /* v8 ignore next -- Node consumers use the filesystem fallback */ + return typeof candidate === "function" ? (candidate as never) : undefined; + }; + + const load = Effect.fn("ToolEnvironmentService.load")(function* ( + tool: DiscoveredTool, + definition: ToolDefinition, + ) { + const envPath = join(dirname(tool.toolPath), ".env"); + const file = bunFile(); + const fileExists = yield* Effect.tryPromise({ + /* v8 ignore next -- Node consumers use the filesystem fallback */ + try: () => (file ? file(envPath).exists() : Promise.resolve(existsSync(envPath))), + catch: toError, + }); + if (!definition.env) { + if (fileExists) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Tool ${definition.name} has .env but no env schema.`, + details: { path: envPath }, + }); + } + return {}; + } + const source = fileExists + ? yield* Effect.tryPromise({ + /* v8 ignore next -- Node consumers use the filesystem fallback */ + try: () => (file ? file(envPath).text() : readFile(envPath, "utf8")), + catch: toError, + }) + : ""; + const rawEnv = yield* parse(source, envPath); + const result = definition.env.safeParse(rawEnv); + if (!result.success) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Tool ${definition.name} env is invalid.`, + details: { path: envPath, errors: result.error.flatten() }, + }); + } + return result.data; + }); + + return { parse, load } as const; + }), +}) { + static readonly layer = Layer.effect(ToolEnvironmentService, ToolEnvironmentService.make); +} + +export const toolEnvironmentLayer = ToolEnvironmentService.layer.pipe( + Layer.provide(ToolEnvironmentParserService.layer), +); diff --git a/src/domains/tools/runtime/tool-execution-resources.test.ts b/src/domains/tools/runtime/tool-execution-resources.test.ts new file mode 100644 index 0000000..31723bd --- /dev/null +++ b/src/domains/tools/runtime/tool-execution-resources.test.ts @@ -0,0 +1,134 @@ +/** @effect-diagnostics strictEffectProvide:skip-file */ +/* oxlint-disable vitest/no-standalone-expect -- @effect/vitest owns these Effect-aware tests. */ +import { it } from "@effect/vitest"; +import { Effect, Layer, Result, Scope } from "effect"; +import { expect } from "vite-plus/test"; +import { ToolCollectionsService } from "./tool-collections"; +import type { CollectionHandle } from "../../collections/types/tool-collection"; +import { + ToolPersistenceService, + type ToolPersistenceResourceSet, +} from "../service/tool-persistence"; +import { RigError } from "../../../providers/errors/rig-error"; +import type { LoadedTool, RigToolDatabase, RigToolLogger } from "../types/tool-types"; +import { ToolExecutionError, type ToolExecutionOperation } from "../types/tool-execution"; +import { ToolExecutionResourcesService } from "./tool-execution-resources"; + +class ResourceHarness { + readonly events: string[] = []; + readonly cause = new RigError({ code: "INTERNAL_ERROR", message: "resource setup failed" }); + readonly tool = {} as LoadedTool; + readonly log = {} as RigToolLogger; + readonly db = {} as RigToolDatabase; + readonly kv = {} as ToolPersistenceResourceSet["kv"]; + readonly cache = {} as ToolPersistenceResourceSet["cache"]; + readonly collections = {} as Record>; + + constructor( + private readonly setupFailure?: ToolExecutionOperation, + private readonly closeFailure?: ToolExecutionOperation, + ) {} + + layer() { + const acquireDatabase = () => this.acquire("database", this.db); + const acquireKv = () => this.acquire("kv", this.kv); + const acquireCache = () => this.acquire("cache", this.cache); + const persistence = Layer.succeed(ToolPersistenceService, { + acquire: () => + Effect.gen(function* () { + const db = yield* acquireDatabase().pipe( + Effect.mapError((cause) => new ToolExecutionError({ operation: "database", cause })), + ); + const kv = yield* acquireKv().pipe( + Effect.mapError((cause) => new ToolExecutionError({ operation: "kv", cause })), + ); + const cache = yield* acquireCache().pipe( + Effect.mapError((cause) => new ToolExecutionError({ operation: "cache", cause })), + ); + return { db, kv, cache } as const; + }), + }); + const collections = Layer.succeed(ToolCollectionsService, { + setup: () => Effect.succeed(this.collections), + acquire: () => this.acquire("collections", this.collections), + close: () => Effect.void, + }); + return ToolExecutionResourcesService.layer.pipe( + Layer.provide(Layer.merge(persistence, collections)), + ); + } + + private acquire( + operation: ToolExecutionOperation, + resource: Resource, + ): Effect.Effect { + return Effect.acquireRelease( + Effect.try({ + try: () => { + this.events.push(`open:${operation}`); + if (operation === this.setupFailure) throw this.cause; + return resource; + }, + catch: (cause) => + cause instanceof RigError + ? cause + : new RigError({ code: "INTERNAL_ERROR", message: String(cause), details: cause }), + }), + () => + Effect.sync(() => { + this.events.push(`close:${operation}`); + if (operation === this.closeFailure) this.events.push(`warn:${operation}`); + }), + ); + } +} + +it.effect("releases every tool resource in reverse order", () => + Effect.gen(function* () { + const harness = new ResourceHarness(undefined, "collections"); + const resources = yield* Effect.scoped( + ToolExecutionResourcesService.use((service) => + service.acquire({ tool: harness.tool, log: harness.log }), + ).pipe(Effect.provide(harness.layer())), + ); + expect(resources).toEqual({ + db: harness.db, + kv: harness.kv, + cache: harness.cache, + collections: harness.collections, + }); + expect(harness.events).toEqual([ + "open:database", + "open:kv", + "open:cache", + "open:collections", + "close:collections", + "warn:collections", + "close:cache", + "close:kv", + "close:database", + ]); + }), +); + +it.effect("releases partial setup and reports the failed operation", () => + Effect.gen(function* () { + const harness = new ResourceHarness("cache"); + const result = yield* Effect.scoped( + ToolExecutionResourcesService.use((service) => + service.acquire({ tool: harness.tool, log: harness.log }), + ).pipe(Effect.provide(harness.layer()), Effect.result), + ); + expect(Result.isFailure(result)).toBe(true); + if (Result.isSuccess(result)) return; + expect(result.failure).toBeInstanceOf(ToolExecutionError); + expect(result.failure).toMatchObject({ operation: "cache", cause: harness.cause }); + expect(harness.events).toEqual([ + "open:database", + "open:kv", + "open:cache", + "close:kv", + "close:database", + ]); + }), +); diff --git a/src/domains/tools/runtime/tool-execution-resources.ts b/src/domains/tools/runtime/tool-execution-resources.ts new file mode 100644 index 0000000..37ad833 --- /dev/null +++ b/src/domains/tools/runtime/tool-execution-resources.ts @@ -0,0 +1,54 @@ +import { Context, Effect, Layer, Scope } from "effect"; +import { ToolCollectionsService, toolCollectionsLayer } from "./tool-collections"; +import type { CollectionHandle } from "../../collections/types/tool-collection"; +import { + ToolPersistenceService, + toolPersistenceLayer, + type ToolPersistenceResourceSet, +} from "../service/tool-persistence"; +import { ToolExecutionError } from "../types/tool-execution"; +import type { LoadedTool, RigToolDatabase, RigToolLogger } from "../types/tool-types"; + +export type ToolExecutionResourceSet = Readonly<{ + db: RigToolDatabase; + kv: ToolPersistenceResourceSet["kv"]; + cache: ToolPersistenceResourceSet["cache"]; + collections: Record> | undefined; +}>; + +export class ToolExecutionResourcesService extends Context.Service< + ToolExecutionResourcesService, + { + readonly acquire: (params: { + tool: LoadedTool; + log: RigToolLogger; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolExecutionResourcesService", { + make: Effect.gen(function* () { + const persistence = yield* ToolPersistenceService; + const collections = yield* ToolCollectionsService; + const acquire = Effect.fn("ToolExecutionResourcesService.acquire")(function* (params: { + tool: LoadedTool; + log: RigToolLogger; + }) { + const { db, kv, cache } = yield* persistence.acquire(params); + const collectionHandles = yield* collections + .acquire(params.tool) + .pipe( + Effect.mapError((cause) => new ToolExecutionError({ operation: "collections", cause })), + ); + return { db, kv, cache, collections: collectionHandles }; + }); + return { acquire } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolExecutionResourcesService, + ToolExecutionResourcesService.make, + ); +} + +export const toolExecutionResourcesLayer = ToolExecutionResourcesService.layer.pipe( + Layer.provide(Layer.merge(toolPersistenceLayer, toolCollectionsLayer)), +); diff --git a/src/domains/tools/runtime/tool-files.ts b/src/domains/tools/runtime/tool-files.ts new file mode 100644 index 0000000..802ba49 --- /dev/null +++ b/src/domains/tools/runtime/tool-files.ts @@ -0,0 +1,118 @@ +import { existsSync } from "node:fs"; +import { mkdir, rm } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import { AtomicFileWriterService } from "../../../providers/filesystem/atomic-file-writer"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import { RigToolEntryFiles } from "../../registry/types/tool-discovery"; +import { CurrentRigToolApiVersion } from "../config/tool-api"; +import { ToolDefinitionService } from "../service/tool-definition"; + +export type ToolFileResult = { + name: string; + toolDir: string; + toolPath: string; +}; + +export class ToolFilesService extends Context.Service< + ToolFilesService, + { + readonly renderTemplate: Effect.Effect; + readonly create: ( + name: string, + ) => Effect.Effect; + readonly path: (name: string) => Effect.Effect; + readonly remove: (name: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/management/ToolFilesService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsService; + const config = yield* RigConfigStoreService; + const discovery = yield* ToolDiscoveryService; + const definitions = yield* ToolDefinitionService; + const writer = yield* AtomicFileWriterService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const renderTemplate = Effect.succeed(`// rig:tool-api-version ${CurrentRigToolApiVersion} +export default (rig: RigToolKit) => rig.defineTool({ + description: "Describe what this tool does.", + commands: (command) => ({ + example: command({ + description: "Example command. Replace this with a real command.", + input: rig.z.object({ text: rig.z.string().default("example") }), + output: rig.z.object({ text: rig.z.string() }), + examples: [{ + title: "Run the example command", + text: "Use this to verify Rig can run a local command.", + input: { text: "example" }, + output: { text: "example" }, + }], + run: async ({ input }) => ({ text: input.text }), + }), + }), +}); +`).pipe(Effect.withSpan("ToolFilesService.renderTemplate")); + + const path = Effect.fn("ToolFilesService.path")(function* (name: string) { + yield* definitions.validateToolName(name); + const tool = yield* discovery.find(name); + return { name: tool.name, toolDir: tool.toolDir, toolPath: tool.toolPath }; + }); + + const create = Effect.fn("ToolFilesService.create")(function* (name: string) { + yield* definitions.validateToolName(name); + const current = yield* config.ensure.pipe( + /* v8 ignore next -- config errors are already typed before this boundary */ + Effect.mapError((error) => toError(error.cause)), + ); + const toolDir = join(yield* paths.resolve(current.baseRegistryDir), name); + const toolPath = join(toolDir, RigToolEntryFiles[0]); + if (existsSync(toolDir)) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Tool already exists: ${name}`, + details: { path: toolDir }, + }); + } + const template = yield* renderTemplate; + yield* Effect.tryPromise({ + try: () => mkdir(toolDir, { recursive: true }), + catch: toError, + }); + yield* writer.write(toolPath, template).pipe( + Effect.mapError((error) => toError(error.cause)), + /* v8 ignore next -- cleanup after a platform write failure is defensive */ + Effect.tapError(() => Effect.promise(() => rm(toolDir, { recursive: true, force: true }))), + ); + return { + name, + command: "example", + id: `${name}.example`, + toolDir, + toolPath, + files: [toolPath], + }; + }); + + const remove = Effect.fn("ToolFilesService.remove")(function* (name: string) { + const tool = yield* path(name); + yield* Effect.tryPromise({ + try: () => rm(tool.toolDir, { recursive: true, force: false }), + catch: toError, + }); + return tool; + }); + + return { renderTemplate, create, path, remove } as const; + }), +}) { + static readonly layer = Layer.effect(ToolFilesService, ToolFilesService.make); +} diff --git a/src/domains/tools/runtime/tool-find.test.ts b/src/domains/tools/runtime/tool-find.test.ts new file mode 100644 index 0000000..6ce819b --- /dev/null +++ b/src/domains/tools/runtime/tool-find.test.ts @@ -0,0 +1,238 @@ +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { Effect, Layer } from "effect"; +import { atomicFileWriterLayer } from "../../../providers/filesystem/atomic-file-writer"; +import { RigPathsConfigService } from "../../../providers/paths/rig-paths"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { toolIdentifierLayer } from "../service/tool-identifier"; +import { schemaRendererLayer } from "../service/schema-renderer"; +import { toolSearchLayer } from "../service/tool-search"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { toolEnvironmentLayer } from "./tool-environment"; +import { toolLoaderLayer } from "./tool-loader"; +import { createRigToolKit, RigToolKitService } from "./tool-sdk"; +import { + ToolFindService, + toolFindLayer, + type ToolFindData, + type ToolFindOptions, +} from "./tool-find"; +import { toolMetadataCacheLayer } from "./tool-list"; + +class ToolFindTestHome { + private readonly paths: string[] = []; + + public async create(): Promise { + const home = await mkdtemp(join(tmpdir(), "rig-find-test-")); + this.paths.push(home); + await this.writeTool({ + home, + name: "languagetool", + source: `export default (rig) => rig.defineTool({ + name: "languagetool", + description: "Check grammar, spelling, and writing style.", + commands: { + "check-file": rig.defineCommand({ + description: "Check grammar and spelling in a local Markdown file.", + input: rig.z.object({ input: rig.z.string().describe("Markdown file path") }), + output: rig.z.object({ issues: rig.z.number() }), + examples: [{ + title: "Proofread notes", + text: "Check a Markdown document for writing problems.", + input: { input: "notes.md" }, + output: { issues: 0 }, + }], + run: async () => ({ issues: 0 }), + }), + "check-text": rig.defineCommand({ + description: "Check a text string.", + input: rig.z.object({ + text: rig.z.string(), + categories: rig.z.array(rig.z.enum(["grammar", "style"]).describe("Rule category")), + strict: rig.z.boolean().optional(), + maxIssues: rig.z.number().optional(), + }), + output: rig.z.object({ issues: rig.z.number() }), + examples: [ + { title: "Default check", text: "Check without explicit input." }, + { + title: "Configured check", + text: "Check selected categories.", + input: { text: "Hello", categories: ["grammar"], strict: true, maxIssues: 2, extra: null }, + output: { issues: 0 }, + }, + ], + run: async () => ({ issues: 0 }), + }), + }, +});`, + }); + await this.writeTool({ + home, + name: "image", + source: `export default (rig) => rig.defineTool({ + name: "image", + description: "Resize and convert images.", + commands: { + convert: rig.defineCommand({ + description: "Resize an image and convert its format.", + input: rig.z.object({ input: rig.z.string(), width: rig.z.number().optional() }), + output: rig.z.object({ output: rig.z.string() }), + run: async () => ({ output: "image.webp" }), + }), + }, +});`, + }); + return home; + } + + public async cleanup(): Promise { + await Promise.all( + this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); + } + + private async writeTool(params: { home: string; name: string; source: string }): Promise { + const directory = join(params.home, "rig", "tools", params.name); + await mkdir(directory, { recursive: true }); + await writeFile(join(directory, "index.rig.ts"), params.source, "utf8"); + } +} + +const findService = (home: string) => { + const tools = ["languagetool", "image"].map( + (name) => + ({ + name, + registryKind: "base" as const, + registryPath: join(home, "rig", "tools"), + toolDir: join(home, "rig", "tools", name), + toolPath: join(home, "rig", "tools", name, "index.rig.ts"), + }) satisfies DiscoveredTool, + ); + const discoveryLayer = Layer.succeed(ToolDiscoveryService, { + discover: () => Effect.succeed(tools), + discoverRegistry: () => Effect.succeed(tools), + find: (name: string) => Effect.succeed(tools.find((tool) => tool.name === name)!), + projectRootFor: () => Effect.succeed(home), + }); + const toolkitLayer = Layer.succeed(RigToolKitService, { + create: () => Effect.succeed(createRigToolKit({ homeDir: home })), + }); + const loaderLayer = toolLoaderLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolEnvironmentLayer, + discoveryLayer, + toolDefinitionLayer, + toolIdentifierLayer, + toolkitLayer, + ), + ), + ); + const pathLayer = Layer.succeed(RigPathsConfigService, { + homeDir: home, + rigDir: join(home, "rig"), + legacyRigDir: join(home, ".rig"), + configPath: join(home, "rig", "rig.json"), + runtimeDir: join(home, "rig", "runtime"), + runtimeSdkPath: join(home, "rig", "runtime", "sdk.ts"), + runtimeTypesPath: join(home, "rig", "runtime", "types.d.ts"), + runtimeGlobalsPath: join(home, "rig", "runtime", "globals.d.ts"), + runtimeToolTsconfigPath: join(home, "rig", "runtime", "tsconfig.tools.json"), + cronDir: join(home, "rig", "cron"), + logsDir: join(home, "rig", ".logs"), + updateCheckCachePath: join(home, "rig", "update-check.json"), + toolMetadataCachePath: join(home, "rig", "tool-metadata.json"), + migrationPromptStatePath: join(home, "rig", "migration-prompts.json"), + defaultBaseRegistryDir: "~/rig/tools", + legacyDefaultBaseRegistryDir: "~/.rig/tools", + }); + const metadataLayer = toolMetadataCacheLayer.pipe( + Layer.provide( + Layer.mergeAll( + loaderLayer, + atomicFileWriterLayer, + schemaRendererLayer, + toolIdentifierLayer, + pathLayer, + ), + ), + ); + const layer = toolFindLayer.pipe( + Layer.provide( + Layer.mergeAll(discoveryLayer, metadataLayer, toolIdentifierLayer, toolSearchLayer), + ), + ); + return { + find: (query: string, options?: ToolFindOptions) => + Effect.runPromise( + ToolFindService.use((service) => service.find(query, options)).pipe(Effect.provide(layer)), + ), + render: (data: ToolFindData) => + Effect.runPromise( + ToolFindService.use((service) => service.render(data)).pipe(Effect.provide(layer)), + ), + }; +}; + +describe("ToolFind", () => { + const homes = new ToolFindTestHome(); + + afterEach(async () => { + await homes.cleanup(); + }); + + test("finds commands through descriptions, examples, fields, and typos", async () => { + const homeDir = await homes.create(); + const service = findService(homeDir); + const result = await service.find("grammer chek markdown"); + + expect(result.results[0]).toMatchObject({ + rank: 1, + id: "languagetool.check-file", + tool: "languagetool", + command: "check-file", + }); + expect(result.results[0]?.runExample).toContain("notes.md"); + expect(result.results[0]?.matches.length).toBeGreaterThan(0); + expect(await service.render(result)).toContain("1. languagetool.check-file"); + }); + + test("supports tool filters, limits, input-field discovery, and no-result output", async () => { + const homeDir = await homes.create(); + const service = findService(homeDir); + const filtered = await service.find("width resize", { tool: "image", limit: "1" }); + + expect(filtered).toMatchObject({ tool: "image", limit: 1 }); + expect(filtered.results).toMatchObject([{ id: "image.convert" }]); + + const empty = await service.find("quantum payroll"); + expect(await service.render(empty)).toBe('No Rig commands found for "quantum payroll".'); + const scopedEmpty = await service.find("quantum payroll", { tool: "image" }); + expect(await service.render(scopedEmpty)).toBe( + 'No Rig commands found for "quantum payroll" in tool image.', + ); + expect( + await service.render({ + ...filtered, + results: filtered.results.map((result) => ({ + ...result, + runExample: "rig run image.convert input=a\r\nb\rc\td", + })), + }), + ).toContain("input=a\\nb\\rc\\td"); + }); + + test("rejects invalid queries, limits, and tool filters", async () => { + const homeDir = await homes.create(); + const service = findService(homeDir); + await expect(service.find(" ")).rejects.toThrow("Find query cannot be empty"); + await expect(service.find("image", { limit: 0 })).rejects.toThrow("between 1 and 50"); + await expect(service.find("image", { limit: "1.5" })).rejects.toThrow("between 1 and 50"); + await expect(service.find("image", { tool: "missing" })).rejects.toThrow("Tool not found"); + }); +}); diff --git a/src/domains/tools/runtime/tool-find.ts b/src/domains/tools/runtime/tool-find.ts new file mode 100644 index 0000000..5198143 --- /dev/null +++ b/src/domains/tools/runtime/tool-find.ts @@ -0,0 +1,311 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import { ToolIdentifierService } from "../service/tool-identifier"; +import { ToolSearchService, type ToolSearchDocument } from "../service/tool-search"; +import type { ToolExample } from "../types/tool-types"; +import { type ToolCommandMetadata, type ToolMetadata, ToolMetadataCacheService } from "./tool-list"; + +export type ToolFindOptions = { limit?: number | string; tool?: string }; +type ToolFindMatch = { field: string; value: string; score: number }; +type ToolFindResult = { + rank: number; + score: number; + id: string; + tool: string; + command: string; + description: string; + runExample: string; + matches: ToolFindMatch[]; +}; +export type ToolFindData = { + query: string; + tool?: string; + limit: number; + results: ToolFindResult[]; +}; + +type SearchableCommand = { + id: string; + tool: string; + command: string; + description: string; + runExample: string; + document: ToolSearchDocument; +}; + +type ParsedToolFindInput = { query: string; limit: number; tool?: string }; + +class ToolFindInputService extends Context.Service< + ToolFindInputService, + { + readonly parse: ( + query: string, + options?: ToolFindOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolFindInputService", { + make: Effect.gen(function* () { + const normalizeError = (cause: unknown): RigError => { + if (cause instanceof RigError) return cause; + return new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + }; + const parseLimit = (value: number | string | undefined): number => { + const parsed = value === undefined ? 5 : Number(value); + const isInvalidLimit = !Number.isInteger(parsed) || parsed < 1 || parsed > 50; + if (isInvalidLimit) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Find limit must be an integer between 1 and 50.", + details: { value }, + }); + } + return parsed; + }; + const parse = Effect.fn("ToolFindInputService.parse")(function* ( + queryValue: string, + options: ToolFindOptions = {}, + ) { + return yield* Effect.try({ + try: () => { + const query = queryValue.trim(); + if (!query) { + throw new RigError({ code: "INPUT_ERROR", message: "Find query cannot be empty." }); + } + return { query, limit: parseLimit(options.limit), tool: options.tool }; + }, + catch: normalizeError, + }); + }); + return { parse } as const; + }), +}) { + static readonly layer = Layer.effect(ToolFindInputService, ToolFindInputService.make); +} + +class ToolFindSchemaFieldsService extends Context.Service< + ToolFindSchemaFieldsService, + { readonly fields: (schema: unknown) => Effect.Effect } +>()("@rendotdev/rig/tools/presentation/ToolFindSchemaFieldsService", { + make: Effect.gen(function* () { + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const walk = (schema: unknown, path: string[]): ToolSearchDocument["fields"] => { + /* v8 ignore next -- schemaRenderer always returns a JSON Schema object */ + if (!isRecord(schema)) return []; + const fields: ToolSearchDocument["fields"] = []; + if (path.length > 0) fields.push({ name: "input.field", value: path.join("."), weight: 3 }); + if (typeof schema.description === "string") { + fields.push({ name: "input.description", value: schema.description, weight: 3 }); + } + if (Array.isArray(schema.enum)) { + fields.push({ name: "input.enum", value: schema.enum.join(" "), weight: 1.5 }); + } + if (isRecord(schema.properties)) { + for (const [name, child] of Object.entries(schema.properties)) { + fields.push(...walk(child, [...path, name])); + } + } + if (schema.items !== undefined) fields.push(...walk(schema.items, path)); + return fields; + }; + const fields = Effect.fn("ToolFindSchemaFieldsService.fields")(function* (schema: unknown) { + return walk(schema, []); + }); + return { fields } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolFindSchemaFieldsService, + ToolFindSchemaFieldsService.make, + ); +} + +class ToolFindCommandIndexService extends Context.Service< + ToolFindCommandIndexService, + { readonly build: (metadata: ToolMetadata[]) => Effect.Effect } +>()("@rendotdev/rig/tools/presentation/ToolFindCommandIndexService", { + make: Effect.gen(function* () { + const schemas = yield* ToolFindSchemaFieldsService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const searchableValue = (value: unknown): string => { + if (typeof value === "string") return value; + const isSearchableScalar = typeof value === "number" || typeof value === "boolean"; + if (isSearchableScalar) return String(value); + if (Array.isArray(value)) return value.map(searchableValue).join(" "); + if (isRecord(value)) { + return Object.entries(value) + .flatMap(([key, item]) => [key, searchableValue(item)]) + .join(" "); + } + return ""; + }; + const exampleFields = (examples: ToolExample[]): ToolSearchDocument["fields"] => + examples.flatMap((example) => [ + { name: "example.title", value: example.title, weight: 4 }, + { name: "example.text", value: example.text, weight: 4 }, + ...(example.input === undefined + ? [] + : [{ name: "example.input", value: searchableValue(example.input), weight: 2 }]), + ]); + const command = Effect.fn("ToolFindCommandIndexService.command")(function* ( + toolName: string, + toolDescription: string, + commandValue: ToolCommandMetadata, + ) { + const id = `${toolName}.${commandValue.name}`; + return { + id, + tool: toolName, + command: commandValue.name, + description: commandValue.description, + runExample: commandValue.runExample, + document: { + id, + fields: [ + { name: "command.id", value: id, weight: 12 }, + { name: "command.name", value: commandValue.name, weight: 10 }, + { name: "tool.name", value: toolName, weight: 8 }, + { name: "command.description", value: commandValue.description, weight: 7 }, + { name: "tool.description", value: toolDescription, weight: 5 }, + ...exampleFields(commandValue.examples), + ...(yield* schemas.fields(commandValue.inputSchema)), + ], + }, + }; + }); + const build = Effect.fn("ToolFindCommandIndexService.build")(function* ( + metadata: ToolMetadata[], + ) { + return yield* Effect.forEach(metadata, (tool) => + Effect.forEach(tool.commands, (value) => command(tool.name, tool.description, value)), + ).pipe(Effect.map((commands) => commands.flat())); + }); + return { build } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolFindCommandIndexService, + ToolFindCommandIndexService.make, + ); +} + +class ToolFindRenderService extends Context.Service< + ToolFindRenderService, + { readonly render: (data: ToolFindData) => Effect.Effect } +>()("@rendotdev/rig/tools/presentation/ToolFindRenderService", { + make: Effect.gen(function* () { + const oneLine = (value: string): string => + value + .replace(/\r\n/g, "\\n") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t"); + const render = Effect.fn("ToolFindRenderService.render")(function* (data: ToolFindData) { + if (data.results.length === 0) { + const scope = data.tool ? ` in tool ${data.tool}` : ""; + return `No Rig commands found for ${JSON.stringify(data.query)}${scope}.`; + } + return data.results + .map((result) => + [ + `${result.rank}. ${result.id}`, + ` ${result.description}`, + ` ${oneLine(result.runExample)}`, + ].join("\n"), + ) + .join("\n\n"); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolFindRenderService, ToolFindRenderService.make); +} + +export class ToolFindService extends Context.Service< + ToolFindService, + { + readonly find: ( + query: string, + options?: ToolFindOptions, + ) => Effect.Effect; + readonly render: (data: ToolFindData) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolFindService", { + make: Effect.gen(function* () { + const input = yield* ToolFindInputService; + const index = yield* ToolFindCommandIndexService; + const renderer = yield* ToolFindRenderService; + const discovery = yield* ToolDiscoveryService; + const metadata = yield* ToolMetadataCacheService; + const identifiers = yield* ToolIdentifierService; + const search = yield* ToolSearchService; + const render = Effect.fn("ToolFindService.render")(function* (data: ToolFindData) { + return yield* renderer.render(data); + }); + const find = Effect.fn("ToolFindService.find")(function* ( + queryValue: string, + options: ToolFindOptions = {}, + ) { + const { query, limit, tool } = yield* input.parse(queryValue, options); + if (tool) yield* identifiers.parseToolName(tool); + const discovered = yield* discovery.discover(); + const selected = tool ? discovered.filter((entry) => entry.name === tool) : discovered; + const isSelectedToolMissing = tool && selected.length === 0; + if (isSelectedToolMissing) { + return yield* new RigError({ + code: "TOOL_NOT_FOUND", + message: `Tool not found: ${tool}`, + details: { available: discovered.map((entry) => entry.name) }, + }); + } + const commands = yield* index.build( + yield* metadata.load(selected, { prune: tool === undefined }), + ); + const byId = new Map(commands.map((command) => [command.id, command])); + const ranked = yield* search.search({ + query, + documents: commands.map((command) => command.document), + limit, + }); + return { + query, + ...(tool ? { tool } : {}), + limit, + results: ranked.map((result, rank) => { + const command = byId.get(result.id)!; + return { + rank: rank + 1, + score: result.score, + id: command.id, + tool: command.tool, + command: command.command, + description: command.description, + runExample: command.runExample, + matches: result.matches, + }; + }), + }; + }); + return { find, render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolFindService, ToolFindService.make); +} + +const toolFindCommandIndexLayer = ToolFindCommandIndexService.layer.pipe( + Layer.provide(ToolFindSchemaFieldsService.layer), +); +export const toolFindLayer = ToolFindService.layer.pipe( + Layer.provide( + Layer.mergeAll( + ToolFindInputService.layer, + toolFindCommandIndexLayer, + ToolFindRenderService.layer, + ), + ), +); diff --git a/src/domains/tools/runtime/tool-help.ts b/src/domains/tools/runtime/tool-help.ts new file mode 100644 index 0000000..a0f6315 --- /dev/null +++ b/src/domains/tools/runtime/tool-help.ts @@ -0,0 +1,284 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import type { CollectionDefinition } from "../../collections/types/tool-collection"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolIdentifierService } from "../service/tool-identifier"; +import { SchemaRendererService } from "../service/schema-renderer"; +import type { CommandDefinition, ToolDefinition } from "../types/tool-types"; +import { ToolLoaderService } from "./tool-loader"; + +type ToolDefinitionWithCollections = { + collections?: Record; +}; + +class ToolHelpSchemaService extends Context.Service< + ToolHelpSchemaService, + { readonly renderFields: (schema: unknown) => Effect.Effect } +>()("@rendotdev/rig/tools/presentation/ToolHelpSchemaService", { + make: Effect.gen(function* () { + const schemas = yield* SchemaRendererService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const typeName = (schema: unknown): string => { + if (!isRecord(schema)) return "unknown"; + if (Array.isArray(schema.type)) return schema.type.join(" | "); + return typeof schema.type === "string" ? schema.type : "unknown"; + }; + const renderFields = Effect.fn("ToolHelpSchemaService.renderFields")(function* ( + schema: unknown, + ) { + const jsonSchema = yield* schemas.renderJson(schema); + const hasNoObjectProperties = !isRecord(jsonSchema) || !isRecord(jsonSchema.properties); + if (hasNoObjectProperties) return "- value: unknown"; + const schemaRecord = jsonSchema as Record; + const properties = schemaRecord.properties as Record; + const required = Array.isArray(schemaRecord.required) ? schemaRecord.required : []; + return Object.entries(properties) + .map(([name, property]) => { + const hasDefault = isRecord(property) && property.default !== undefined; + const requiredText = required.includes(name) && !hasDefault ? "required" : "optional"; + const defaultText = hasDefault ? `, default ${JSON.stringify(property.default)}` : ""; + return `- ${name}: ${typeName(property)} (${requiredText}${defaultText})`; + }) + .join("\n"); + }); + return { renderFields } as const; + }), +}) { + static readonly layer = Layer.effect(ToolHelpSchemaService, ToolHelpSchemaService.make); +} + +class ToolHelpExampleService extends Context.Service< + ToolHelpExampleService, + { + readonly render: ( + toolName: string, + commandName: string, + command: CommandDefinition, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolHelpExampleService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const shellArg = (value: string): string => + /^[A-Za-z0-9_./:=@+-]+$/.test(value) ? value : `'${value.replaceAll("'", "'\\''")}'`; + const renderArgs = (input: unknown): string => { + if (!isRecord(input)) return shellArg(String(input)); + const entries = Object.entries(input); + if (entries.length === 1) return shellArg(String(entries[0]?.[1])); + return entries.map(([key, value]) => `${key}=${shellArg(String(value))}`).join(" "); + }; + const render = Effect.fn("ToolHelpExampleService.render")(function* ( + toolName: string, + commandName: string, + command: CommandDefinition, + ) { + const examples = command.examples ?? []; + if (examples.length === 0) return "No examples declared."; + const id = yield* identifiers.commandId(toolName, commandName); + return examples + .map((example) => { + const args = example.input === undefined ? "" : ` ${renderArgs(example.input)}`; + return [`$ rig run ${id}${args}`, `# ${example.title}: ${example.text}`].join("\n"); + }) + .join("\n\n"); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolHelpExampleService, ToolHelpExampleService.make); +} + +class ToolHelpCommandService extends Context.Service< + ToolHelpCommandService, + { + readonly render: (params: { + toolName: string; + commandName: string; + command: CommandDefinition; + options?: { detailed?: boolean; headingLevel?: number }; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolHelpCommandService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const schemas = yield* ToolHelpSchemaService; + const examples = yield* ToolHelpExampleService; + const render = Effect.fn("ToolHelpCommandService.render")(function* (params: { + toolName: string; + commandName: string; + command: CommandDefinition; + options?: { detailed?: boolean; headingLevel?: number }; + }) { + const options = params.options ?? {}; + const id = yield* identifiers.commandId(params.toolName, params.commandName); + const heading = "#".repeat(options.headingLevel ?? 3); + const lines = options.detailed + ? [ + `Tool: ${params.toolName}`, + `Command: ${params.commandName}`, + `Run: rig run ${id} [args...]`, + "", + ] + : [`${heading} ${id}`, "", `Run: rig run ${id} [args...]`, ""]; + if (options.detailed) lines.push(`${heading} ${id}`, ""); + lines.push( + params.command.description, + "", + "Input:", + "", + yield* schemas.renderFields(params.command.input), + "", + "Output:", + "", + yield* schemas.renderFields(params.command.output), + "", + "Examples:", + "", + yield* examples.render(params.toolName, params.commandName, params.command), + ); + return lines.join("\n"); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolHelpCommandService, ToolHelpCommandService.make); +} + +class ToolHelpDocumentService extends Context.Service< + ToolHelpDocumentService, + { + readonly render: ( + definition: ToolDefinition, + selectedCommand?: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolHelpDocumentService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const schemas = yield* ToolHelpSchemaService; + const commands = yield* ToolHelpCommandService; + const renderCollection = Effect.fn("ToolHelpDocumentService.renderCollection")(function* ( + name: string, + definition: CollectionDefinition | undefined, + ) { + const lines = [ + `### ${name}`, + "", + `Directory: /${name}/`, + `Access: context.collections.${name}`, + ]; + if (definition?.schema) { + lines.push("", "Schema:", "", yield* schemas.renderFields(definition.schema)); + } else { + lines.push("", "Schema: (none, any frontmatter allowed)"); + } + return lines.join("\n"); + }); + const render = Effect.fn("ToolHelpDocumentService.render")(function* ( + definition: ToolDefinition, + selectedCommand?: string, + ) { + if (selectedCommand) { + const command = definition.commands[selectedCommand]; + if (!command) { + return yield* new RigError({ + code: "COMMAND_NOT_FOUND", + message: `Command not found: ${yield* identifiers.commandId(definition.name, selectedCommand)}`, + details: { available: Object.keys(definition.commands) }, + }); + } + return yield* commands.render({ + toolName: definition.name, + commandName: selectedCommand, + command, + options: { detailed: true, headingLevel: 1 }, + }); + } + const lines = [`# ${definition.name}`, "", definition.description, ""]; + const collections = (definition as ToolDefinitionWithCollections).collections; + const hasCollections = collections && Object.keys(collections).length > 0; + if (hasCollections) { + lines.push("## Collections", ""); + for (const [name, collection] of Object.entries(collections)) { + lines.push(yield* renderCollection(name, collection), ""); + } + } + lines.push("## Commands", ""); + for (const [commandName, command] of Object.entries(definition.commands)) { + lines.push( + yield* commands.render({ + toolName: definition.name, + commandName, + command, + options: { headingLevel: 3 }, + }), + "", + ); + } + return lines.join("\n").trimEnd(); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolHelpDocumentService, ToolHelpDocumentService.make); +} + +export class ToolHelpService extends Context.Service< + ToolHelpService, + { + readonly render: (toolName: string, commandName?: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolHelpService", { + make: Effect.gen(function* () { + const loader = yield* ToolLoaderService; + const identifiers = yield* ToolIdentifierService; + const documents = yield* ToolHelpDocumentService; + const helpTarget = Effect.fn("ToolHelpService.helpTarget")(function* ( + toolName: string, + commandName?: string, + ) { + if (commandName) { + const target = yield* identifiers.makeCommandTarget(toolName, commandName); + return { toolName: target.tool, commandName: target.command }; + } + if (!toolName.includes(".")) { + return { toolName: (yield* identifiers.parseToolName(toolName)).value }; + } + const target = yield* identifiers.parseCommandTarget(toolName); + return { toolName: target.tool, commandName: target.command }; + }); + const render = Effect.fn("ToolHelpService.render")(function* ( + toolName: string, + commandName?: string, + ) { + const target = yield* helpTarget(toolName, commandName); + const tool = yield* loader.loadDefinition(target.toolName); + return yield* documents.render(tool.definition, target.commandName); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolHelpService, ToolHelpService.make); +} + +const toolHelpSchemaLayer = ToolHelpSchemaService.layer.pipe( + Layer.provide(SchemaRendererService.layer), +); +const toolHelpExampleLayer = ToolHelpExampleService.layer.pipe( + Layer.provide(ToolIdentifierService.layer), +); +const toolHelpCommandLayer = ToolHelpCommandService.layer.pipe( + Layer.provide( + Layer.mergeAll(ToolIdentifierService.layer, toolHelpSchemaLayer, toolHelpExampleLayer), + ), +); +const toolHelpDocumentLayer = ToolHelpDocumentService.layer.pipe( + Layer.provide( + Layer.mergeAll(ToolIdentifierService.layer, toolHelpSchemaLayer, toolHelpCommandLayer), + ), +); +export const toolHelpLayer = ToolHelpService.layer.pipe( + Layer.provide(Layer.merge(ToolIdentifierService.layer, toolHelpDocumentLayer)), +); diff --git a/src/domains/tools/runtime/tool-identifier.test.ts b/src/domains/tools/runtime/tool-identifier.test.ts new file mode 100644 index 0000000..e2ffae3 --- /dev/null +++ b/src/domains/tools/runtime/tool-identifier.test.ts @@ -0,0 +1,126 @@ +import { existsSync } from "node:fs"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vite-plus/test"; +import { Context, Effect, Layer } from "effect"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import { AtomicFileWriterService } from "../../../providers/filesystem/atomic-file-writer"; +import { RigPathsService } from "../../../providers/paths/rig-paths"; +import { CollectionName, CommandName, CommandTarget, ToolName } from "../types/tool-identifier"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { ToolIdentifierService, toolIdentifierLayer } from "../service/tool-identifier"; +import { ToolFilesService } from "./tool-files"; + +describe("routed identifier schemas", () => { + const homes: string[] = []; + + afterEach(async () => { + await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); + }); + + const runIdentifier = ( + use: (service: Context.Service.Shape) => Effect.Effect, + ) => Effect.runPromise(ToolIdentifierService.use(use).pipe(Effect.provide(toolIdentifierLayer))); + + it("creates schema entities and command ids", async () => { + expect( + await runIdentifier((service) => service.parseToolName("clean-image-metadata")), + ).toBeInstanceOf(ToolName); + expect(await runIdentifier((service) => service.parseCommandName("get-status"))).toBeInstanceOf( + CommandName, + ); + expect( + await runIdentifier((service) => service.parseCollectionName("release-notes")), + ).toBeInstanceOf(CollectionName); + expect( + await runIdentifier((service) => + service.makeCommandTarget("clean-image-metadata", "get_metadata"), + ), + ).toEqual( + new CommandTarget({ + tool: "clean-image-metadata", + command: "get_metadata", + id: "clean-image-metadata.get_metadata", + }), + ); + expect( + await runIdentifier((service) => service.commandId("clean-image-metadata", "get_metadata")), + ).toBe("clean-image-metadata.get_metadata"); + }); + + it("rejects identifiers that cannot be routed safely", async () => { + await Promise.all( + ["", ".", "..", "../tool", "/tmp/tool", "folder/tool", "folder\\tool"].map((name) => + expect(runIdentifier((service) => service.parseToolName(name))).rejects.toThrow( + "Invalid tool name", + ), + ), + ); + await Promise.all( + ["", ".", "..", "nested.command", "folder/command"].map((name) => + expect(runIdentifier((service) => service.parseCommandName(name))).rejects.toThrow( + "Invalid command name", + ), + ), + ); + await expect( + runIdentifier((service) => service.parseCollectionName("../notes")), + ).rejects.toThrow("Invalid collection name"); + }); + + it("parses exactly one tool and command segment", async () => { + expect( + await runIdentifier((service) => service.parseCommandTarget("socials.post-message")), + ).toMatchObject({ + id: "socials.post-message", + tool: "socials", + command: "post-message", + }); + await Promise.all( + ["tool", ".command", "tool.", "tool.bad.command", "../tool.command"].map((id) => + expect(runIdentifier((service) => service.parseCommandTarget(id))).rejects.toThrow( + "Command id must use .", + ), + ), + ); + await expect(runIdentifier((service) => service.parseCommandTarget(undefined))).rejects.toThrow( + "Command id must use .", + ); + }); + + it("prevents tool creation outside the base registry", async () => { + const home = await mkdtemp(join(tmpdir(), "rig-identifier-test-")); + homes.push(home); + const filesLayer = ToolFilesService.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(RigPathsService, {} as Context.Service.Shape), + Layer.succeed( + RigConfigStoreService, + {} as Context.Service.Shape, + ), + Layer.succeed( + ToolDiscoveryService, + {} as Context.Service.Shape, + ), + Layer.succeed( + AtomicFileWriterService, + {} as Context.Service.Shape, + ), + toolDefinitionLayer, + ), + ), + ); + const create = (name: string) => + Effect.runPromise( + ToolFilesService.use((service) => service.create(name)).pipe(Effect.provide(filesLayer)), + ); + await expect(create("../escaped")).rejects.toThrow("Invalid tool name"); + await expect(create("/tmp/escaped")).rejects.toThrow("Invalid tool name"); + + expect(existsSync(join(home, "rig", "escaped"))).toBe(false); + expect(existsSync(join(home, "escaped"))).toBe(false); + }); +}); diff --git a/src/domains/tools/runtime/tool-inspector.ts b/src/domains/tools/runtime/tool-inspector.ts new file mode 100644 index 0000000..b79b72b --- /dev/null +++ b/src/domains/tools/runtime/tool-inspector.ts @@ -0,0 +1,90 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { SchemaRendererService } from "../service/schema-renderer"; +import { ToolIdentifierService } from "../service/tool-identifier"; +import type { CommandDefinition } from "../types/tool-types"; +import { ToolLoaderService } from "./tool-loader"; + +export class ToolInspectorService extends Context.Service< + ToolInspectorService, + { + readonly inspect: (toolName: string, commandName?: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolInspectorService", { + make: Effect.gen(function* () { + const loader = yield* ToolLoaderService; + const identifiers = yield* ToolIdentifierService; + const schemas = yield* SchemaRendererService; + const inspectTarget = Effect.fn("ToolInspectorService.inspectTarget")(function* ( + toolName: string, + commandName?: string, + ) { + if (commandName) { + const target = yield* identifiers.makeCommandTarget(toolName, commandName); + return { toolName: target.tool, commandName: target.command }; + } + if (!toolName.includes(".")) { + return { toolName: (yield* identifiers.parseToolName(toolName)).value }; + } + const target = yield* identifiers.parseCommandTarget(toolName); + return { toolName: target.tool, commandName: target.command }; + }); + const commandMetadata = Effect.fn("ToolInspectorService.commandMetadata")(function* ( + toolName: string, + name: string, + command: CommandDefinition, + ) { + const id = yield* identifiers.commandId(toolName, name); + return { + name, + id, + description: command.description, + inputSchema: yield* schemas.renderJson(command.input), + outputSchema: yield* schemas.renderJson(command.output), + run: `rig run ${id} [args...]`, + examples: command.examples ?? [], + }; + }); + const inspect = Effect.fn("ToolInspectorService.inspect")(function* ( + toolName: string, + commandName?: string, + ) { + const target = yield* inspectTarget(toolName, commandName); + const loaded = yield* loader.loadDefinition(target.toolName); + const definition = loaded.definition; + if (target.commandName) { + const command = definition.commands[target.commandName]; + if (!command) { + return yield* new RigError({ + code: "COMMAND_NOT_FOUND", + message: `Command not found: ${yield* identifiers.commandId(target.toolName, target.commandName)}`, + details: { + tool: target.toolName, + command: target.commandName, + available: Object.keys(definition.commands), + }, + }); + } + return { + tool: definition.name, + command: target.commandName, + path: loaded.path, + ...(yield* commandMetadata(definition.name, target.commandName, command)), + }; + } + return { + name: definition.name, + description: definition.description, + path: loaded.path, + commands: yield* Effect.all( + Object.entries(definition.commands).map(([name, command]) => + commandMetadata(definition.name, name, command), + ), + ), + }; + }); + return { inspect } as const; + }), +}) { + static readonly layer = Layer.effect(ToolInspectorService, ToolInspectorService.make); +} diff --git a/src/domains/tools/runtime/tool-list.ts b/src/domains/tools/runtime/tool-list.ts new file mode 100644 index 0000000..4565e55 --- /dev/null +++ b/src/domains/tools/runtime/tool-list.ts @@ -0,0 +1,406 @@ +import { readFile, stat } from "node:fs/promises"; +import { Context, Effect, Layer, Predicate } from "effect"; +import type { CollectionDefinition } from "../../collections/types/tool-collection"; +import { AtomicFileWriterService } from "../../../providers/filesystem/atomic-file-writer"; +import { RigPathsConfigService } from "../../../providers/paths/rig-paths"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { ToolIdentifierService } from "../service/tool-identifier"; +import { SchemaRendererService } from "../service/schema-renderer"; +import { CurrentRigToolApiVersion } from "../config/tool-api"; +import type { CommandDefinition, ToolExample } from "../types/tool-types"; +import { ToolLoaderService, type LoadedToolDefinition } from "./tool-loader"; + +export type ToolListOptions = { visibleFromPath?: string }; +type ListedCommand = { + name: string; + id: string; + description: string; + runExample: string; + helpExample: string; +}; +type ListedCollection = { name: string; hasSchema: boolean }; +type ListedTool = { + name: string; + description: string; + registryKind: string; + registryPath: string; + toolPath: string; + commands: ListedCommand[]; + collections: ListedCollection[]; +}; +export type ToolListData = { tools: ListedTool[]; visibleFromPath?: string }; +export type ToolCommandMetadata = { + name: string; + description: string; + inputSchema: unknown; + outputSchema: unknown; + examples: ToolExample[]; + runExample: string; +}; +export type ToolMetadata = { + name: string; + description: string; + commands: ToolCommandMetadata[]; + collections: ListedCollection[]; +}; +type ToolMetadataCacheTimes = { modifiedAtMs: number; changedAtMs: number }; +type ToolMetadataCacheEntry = ToolMetadataCacheTimes & { size: number; metadata: ToolMetadata }; +type MetadataCacheHeader = { version: 1; toolApiVersion: number }; +type ToolMetadataCache = MetadataCacheHeader & { entries: Record }; + +class ToolMetadataExampleService extends Context.Service< + ToolMetadataExampleService, + { + readonly render: ( + toolName: string, + commandName: string, + command: CommandDefinition, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolMetadataExampleService", { + make: Effect.gen(function* () { + const schemas = yield* SchemaRendererService; + const identifiers = yield* ToolIdentifierService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const shellArg = (value: string): string => + /^[A-Za-z0-9_./:=@%+,-]+$/.test(value) ? value : `'${value.replaceAll("'", "'\\''")}'`; + const placeholder = (schema: unknown): string => { + if (!isRecord(schema)) return "VALUE"; + const type = Array.isArray(schema.type) ? schema.type[0] : schema.type; + const isNumericType = type === "number" || type === "integer"; + if (isNumericType) return "0"; + if (type === "boolean") return "true"; + if (type === "array") return "[]"; + if (type === "object") return "{}"; + return "VALUE"; + }; + const renderRequiredInput = Effect.fn("ToolMetadataExampleService.renderRequiredInput")( + function* (schema: unknown) { + const jsonSchema = yield* schemas.renderJson(schema); + const hasNoObjectProperties = !isRecord(jsonSchema) || !isRecord(jsonSchema.properties); + if (hasNoObjectProperties) return ""; + const schemaRecord = jsonSchema as Record; + const properties = schemaRecord.properties as Record; + const required = Array.isArray(schemaRecord.required) ? schemaRecord.required : []; + const fields = Object.entries(properties) + .filter( + ([name, property]) => + required.includes(name) && !(isRecord(property) && property.default !== undefined), + ) + .map(([name, property]) => [name, placeholder(property)] as const); + if (fields.length === 0) return ""; + if (fields.length <= 3) { + return fields.map(([name, value]) => `${name}=${shellArg(value)}`).join(" "); + } + return `--input ${shellArg(JSON.stringify(Object.fromEntries(fields)))}`; + }, + ); + const inputScalar = (value: unknown): string => + typeof value === "string" ? value : (JSON.stringify(value) ?? String(value)); + const renderInput = (input: unknown): string => { + if (!isRecord(input)) return shellArg(String(input)); + return Object.entries(input) + .map(([key, value]) => `${key}=${shellArg(inputScalar(value))}`) + .join(" "); + }; + const render = Effect.fn("ToolMetadataExampleService.render")(function* ( + toolName: string, + commandName: string, + command: CommandDefinition, + ) { + const id = yield* identifiers.commandId(toolName, commandName); + const exampleInput = command.examples?.find((example) => example.input !== undefined)?.input; + if (exampleInput !== undefined) { + return `rig run ${id} ${renderInput(exampleInput)}`.trimEnd(); + } + const args = yield* renderRequiredInput(command.input); + return `rig run ${id}${args ? ` ${args}` : ""}`; + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ToolMetadataExampleService, ToolMetadataExampleService.make); +} + +class ToolMetadataBuilderService extends Context.Service< + ToolMetadataBuilderService, + { readonly build: (loaded: LoadedToolDefinition) => Effect.Effect } +>()("@rendotdev/rig/tools/presentation/ToolMetadataBuilderService", { + make: Effect.gen(function* () { + const schemas = yield* SchemaRendererService; + const examples = yield* ToolMetadataExampleService; + const listedCollections = (loaded: LoadedToolDefinition): ListedCollection[] => { + const collections = + ( + loaded.definition as LoadedToolDefinition["definition"] & { + collections?: Record; + } + ).collections ?? {}; + return Object.entries(collections).map(([name, value]) => ({ + name, + hasSchema: Boolean(value?.schema), + })); + }; + const buildCommand = Effect.fn("ToolMetadataBuilderService.buildCommand")(function* ( + toolName: string, + name: string, + command: CommandDefinition, + ) { + return { + name, + description: command.description, + inputSchema: yield* schemas.renderJson(command.input), + outputSchema: yield* schemas.renderJson(command.output), + examples: command.examples ?? [], + runExample: yield* examples.render(toolName, name, command), + }; + }); + const build = Effect.fn("ToolMetadataBuilderService.build")(function* ( + loaded: LoadedToolDefinition, + ) { + const commands = yield* Effect.all( + Object.entries(loaded.definition.commands).map(([name, command]) => + buildCommand(loaded.definition.name, name, command), + ), + ); + return { + name: loaded.definition.name, + description: loaded.definition.description, + commands, + collections: listedCollections(loaded), + }; + }); + return { build } as const; + }), +}) { + static readonly layer = Layer.effect(ToolMetadataBuilderService, ToolMetadataBuilderService.make); +} + +class ToolMetadataCacheFileService extends Context.Service< + ToolMetadataCacheFileService, + { + readonly read: Effect.Effect; + readonly write: (cache: ToolMetadataCache) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolMetadataCacheFileService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const writer = yield* AtomicFileWriterService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const emptyCache = (): ToolMetadataCache => ({ + version: 1, + toolApiVersion: CurrentRigToolApiVersion, + entries: {}, + }); + const read = Effect.tryPromise({ + try: async () => { + const value = + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(paths.toolMetadataCachePath).json() + : JSON.parse(await readFile(paths.toolMetadataCachePath, "utf8")); + const hasInvalidCacheEnvelope = !isRecord(value) || value.version !== 1; + if (hasInvalidCacheEnvelope) return emptyCache(); + const hasIncompatibleCacheContent = + value.toolApiVersion !== CurrentRigToolApiVersion || !isRecord(value.entries); + if (hasIncompatibleCacheContent) { + return emptyCache(); + } + return value as ToolMetadataCache; + }, + catch: () => emptyCache(), + }).pipe(Effect.orElseSucceed(emptyCache), Effect.withSpan("ToolMetadataCacheFileService.read")); + const write = Effect.fn("ToolMetadataCacheFileService.write")(function* ( + cache: ToolMetadataCache, + ) { + yield* writer + .write(paths.toolMetadataCachePath, `${JSON.stringify(cache, null, 2)}\n`) + .pipe(Effect.ignore); + }); + return { read, write } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolMetadataCacheFileService, + ToolMetadataCacheFileService.make, + ); +} + +export class ToolMetadataCacheService extends Context.Service< + ToolMetadataCacheService, + { + readonly load: ( + entries: DiscoveredTool[], + options?: { prune?: boolean }, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolMetadataCacheService", { + make: Effect.gen(function* () { + const loader = yield* ToolLoaderService; + const builders = yield* ToolMetadataBuilderService; + const files = yield* ToolMetadataCacheFileService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const isMetadata = (value: unknown): value is ToolMetadata => + Predicate.isObject(value) && + !Array.isArray(value) && + typeof value.name === "string" && + typeof value.description === "string" && + Array.isArray(value.commands) && + Array.isArray(value.collections); + const loadEntry = Effect.fn("ToolMetadataCacheService.loadEntry")(function* (params: { + entry: DiscoveredTool; + current: ToolMetadataCache; + nextEntries: Record; + changed: { value: boolean }; + }) { + const status = yield* Effect.tryPromise({ + try: () => stat(params.entry.toolPath), + catch: toError, + }); + const cached = params.current.entries[params.entry.toolPath]; + const isCurrentCacheEntry = + cached?.modifiedAtMs === status.mtimeMs && + cached.changedAtMs === status.ctimeMs && + cached.size === status.size && + isMetadata(cached.metadata); + if (isCurrentCacheEntry) { + params.nextEntries[params.entry.toolPath] = cached; + return cached.metadata; + } + params.changed.value = true; + const value = yield* builders.build(yield* loader.loadDefinitionDiscovered(params.entry)); + params.nextEntries[params.entry.toolPath] = { + modifiedAtMs: status.mtimeMs, + changedAtMs: status.ctimeMs, + size: status.size, + metadata: value, + }; + return value; + }); + const load = Effect.fn("ToolMetadataCacheService.load")(function* ( + entries: DiscoveredTool[], + options: { prune?: boolean } = {}, + ) { + const current = yield* files.read; + const nextEntries: Record = + options.prune === false ? { ...current.entries } : {}; + const changed = { value: false }; + const metadata = yield* Effect.all( + entries.map((entry) => loadEntry({ entry, current, nextEntries, changed })), + { concurrency: "unbounded" }, + ); + const pruned = + options.prune !== false && Object.keys(current.entries).some((path) => !nextEntries[path]); + const shouldWriteCache = changed.value || pruned; + if (shouldWriteCache) { + yield* files.write({ + version: 1, + toolApiVersion: CurrentRigToolApiVersion, + entries: nextEntries, + }); + } + return metadata; + }); + return { load } as const; + }), +}) { + static readonly layer = Layer.effect(ToolMetadataCacheService, ToolMetadataCacheService.make); +} + +export class ToolListService extends Context.Service< + ToolListService, + { + readonly list: (options?: ToolListOptions) => Effect.Effect; + readonly renderPlain: (data: ToolListData) => Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/ToolListService", { + make: Effect.gen(function* () { + const discovery = yield* ToolDiscoveryService; + const metadataCache = yield* ToolMetadataCacheService; + const identifiers = yield* ToolIdentifierService; + const plainDescription = (value: string): string => value.replace(/\s+/g, " ").trim(); + const plainExample = (value: string): string => + value + .replace(/\r\n/g, "\\n") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t") + .trim(); + const listedTool = Effect.fn("ToolListService.listedTool")(function* ( + entry: DiscoveredTool, + tool: ToolMetadata, + ) { + const commands = yield* Effect.all( + tool.commands.map((command) => + identifiers.commandId(tool.name, command.name).pipe( + Effect.map((id) => ({ + name: command.name, + id, + description: command.description, + runExample: command.runExample, + helpExample: `rig help ${id}`, + })), + ), + ), + ); + return { + name: tool.name, + description: tool.description, + registryKind: entry.registryKind, + registryPath: entry.registryPath, + toolPath: entry.toolPath, + commands, + collections: tool.collections, + }; + }); + const list = Effect.fn("ToolListService.list")(function* (options: ToolListOptions = {}) { + const discovered = yield* discovery.discover({ visibleFromPath: options.visibleFromPath }); + const metadata = yield* metadataCache.load(discovered); + return { + tools: yield* Effect.all( + discovered.map((entry, index) => listedTool(entry, metadata[index]!)), + ), + visibleFromPath: options.visibleFromPath, + }; + }); + const renderPlain = Effect.fn("ToolListService.renderPlain")(function* (data: ToolListData) { + if (data.tools.length === 0) return "No Rig tools found."; + return data.tools + .map((tool) => { + const collections = + tool.collections.length > 0 + ? ` [collections: ${tool.collections.map((item) => item.name).join(", ")}]` + : ""; + return [ + `${tool.name} # ${plainDescription(tool.description)}${collections}`, + ...tool.commands.map( + (command) => + ` ${plainExample(command.runExample)} # ${plainDescription(command.description)}`, + ), + ].join("\n"); + }) + .join("\n\n"); + }); + return { list, renderPlain } as const; + }), +}) { + static readonly layer = Layer.effect(ToolListService, ToolListService.make); +} + +const toolMetadataExampleLayer = ToolMetadataExampleService.layer; +const toolMetadataBuilderLayer = ToolMetadataBuilderService.layer.pipe( + Layer.provide(toolMetadataExampleLayer), +); +export const toolMetadataCacheLayer = ToolMetadataCacheService.layer.pipe( + Layer.provide(Layer.merge(toolMetadataBuilderLayer, ToolMetadataCacheFileService.layer)), +); +export const toolListLayer = ToolListService.layer; diff --git a/src/domains/tools/runtime/tool-loader.ts b/src/domains/tools/runtime/tool-loader.ts new file mode 100644 index 0000000..e327871 --- /dev/null +++ b/src/domains/tools/runtime/tool-loader.ts @@ -0,0 +1,191 @@ +import { stat } from "node:fs/promises"; +import { pathToFileURL } from "node:url"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { ToolIdentifierService } from "../service/tool-identifier"; +import type { CommandDefinition, LoadedTool, RigToolKit } from "../types/tool-types"; +import { RigToolKitService } from "./tool-sdk"; +import { ToolDefinitionService } from "../service/tool-definition"; +import { ToolEnvironmentService } from "./tool-environment"; + +export type LoadedToolDefinition = Omit; + +export type LoadedToolCommand = Readonly<{ + tool: LoadedTool; + commandName: string; + command: CommandDefinition; +}>; + +class ToolDiscoveryCacheService extends Context.Service< + ToolDiscoveryCacheService, + { readonly find: (name: string) => Effect.Effect } +>()("@rendotdev/rig/tools/loading/ToolDiscoveryCacheService", { + make: Effect.gen(function* () { + const discovery = yield* ToolDiscoveryService; + const discoveredTools = new Map(); + const find = Effect.fn("ToolDiscoveryCacheService.find")(function* (name: string) { + const cached = discoveredTools.get(name); + if (cached) return cached; + const discovered = yield* discovery.find(name); + discoveredTools.set(name, discovered); + return discovered; + }); + return { find } as const; + }), +}) { + static readonly layer = Layer.effect(ToolDiscoveryCacheService, ToolDiscoveryCacheService.make); +} + +class ToolDefinitionModuleService extends Context.Service< + ToolDefinitionModuleService, + { + readonly load: (tool: DiscoveredTool) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolDefinitionModuleService", { + make: Effect.gen(function* () { + const definitionsService = yield* ToolDefinitionService; + const toolkits = yield* RigToolKitService; + const definitions = new Map< + string, + { modifiedAtMs: number; size: number; value: LoadedToolDefinition } + >(); + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const evaluateDefault = Effect.fn("ToolDefinitionModuleService.evaluateDefault")(function* ( + value: unknown, + toolName: string, + ) { + const awaitedValue = yield* Effect.promise(() => Promise.resolve(value)); + if (typeof awaitedValue !== "function") return awaitedValue; + const toolkit = yield* toolkits.create(); + return yield* Effect.tryPromise({ + try: () => Promise.resolve((awaitedValue as (toolKit: RigToolKit) => unknown)(toolkit)), + catch: (error) => + new RigError({ + code: "TOOL_INVALID", + message: `Could not evaluate tool factory ${toolName}.`, + details: { tool: toolName, error }, + }), + }); + }); + const load = Effect.fn("ToolDefinitionModuleService.load")(function* (tool: DiscoveredTool) { + const metadata = yield* Effect.tryPromise({ + try: () => stat(tool.toolPath), + catch: toError, + }); + const cached = definitions.get(tool.toolPath); + const isCurrentDefinition = + cached?.modifiedAtMs === metadata.mtimeMs && cached.size === metadata.size; + if (isCurrentDefinition) { + return cached.value; + } + const url = `${pathToFileURL(tool.toolPath).href}?rig=${metadata.mtimeMs}-${metadata.size}`; + const moduleValue = yield* Effect.tryPromise({ + try: () => import(url) as Promise, + catch: (error) => + new RigError({ + code: "TOOL_INVALID", + message: `Could not load tool ${tool.name}.`, + details: { path: tool.toolPath, error }, + }), + }); + const definitionValue = yield* evaluateDefault( + (moduleValue as { default?: unknown }).default, + tool.name, + ); + const definition = yield* definitionsService.decode(definitionValue, tool.name); + const loaded = { name: definition.name, path: tool.toolPath, definition }; + definitions.set(tool.toolPath, { + modifiedAtMs: metadata.mtimeMs, + size: metadata.size, + value: loaded, + }); + return loaded; + }); + return { load } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolDefinitionModuleService, + ToolDefinitionModuleService.make, + ); +} + +export class ToolLoaderService extends Context.Service< + ToolLoaderService, + { + readonly loadDefinition: (name: string) => Effect.Effect; + readonly loadDefinitionDiscovered: ( + tool: DiscoveredTool, + ) => Effect.Effect; + readonly loadDiscovered: (tool: DiscoveredTool) => Effect.Effect; + readonly load: (name: string) => Effect.Effect; + readonly loadCommand: ( + toolName: string, + commandName: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolLoaderService", { + make: Effect.gen(function* () { + const environment = yield* ToolEnvironmentService; + const discovery = yield* ToolDiscoveryCacheService; + const definitions = yield* ToolDefinitionService; + const modules = yield* ToolDefinitionModuleService; + const identifiers = yield* ToolIdentifierService; + const loadDefinitionDiscovered = Effect.fn("ToolLoaderService.loadDefinitionDiscovered")( + function* (tool: DiscoveredTool) { + return yield* modules.load(tool); + }, + ); + const loadDefinition = Effect.fn("ToolLoaderService.loadDefinition")(function* (name: string) { + return yield* loadDefinitionDiscovered(yield* discovery.find(name)); + }); + const loadDiscovered = Effect.fn("ToolLoaderService.loadDiscovered")(function* ( + tool: DiscoveredTool, + ) { + const loaded = yield* loadDefinitionDiscovered(tool); + const env = yield* environment.load(tool, loaded.definition); + return { ...loaded, env }; + }); + const load = Effect.fn("ToolLoaderService.load")(function* (name: string) { + yield* definitions.validateToolName(name); + return yield* loadDiscovered(yield* discovery.find(name)); + }); + const loadCommand = Effect.fn("ToolLoaderService.loadCommand")(function* ( + toolName: string, + commandName: string, + ) { + const tool = yield* load(toolName); + yield* definitions.validateCommandName(commandName); + const command = tool.definition.commands[commandName]; + if (!command) { + return yield* new RigError({ + code: "COMMAND_NOT_FOUND", + message: `Command not found: ${yield* identifiers.commandId(toolName, commandName)}`, + details: { + tool: toolName, + command: commandName, + available: Object.keys(tool.definition.commands), + }, + }); + } + return { tool, commandName, command }; + }); + return { loadDefinition, loadDefinitionDiscovered, loadDiscovered, load, loadCommand } as const; + }), +}) { + static readonly layer = Layer.effect(ToolLoaderService, ToolLoaderService.make); +} + +const toolDiscoveryCacheLayer = ToolDiscoveryCacheService.layer; +const toolDefinitionModuleLayer = ToolDefinitionModuleService.layer; +export const toolLoaderLayer = ToolLoaderService.layer.pipe( + Layer.provide(Layer.merge(toolDiscoveryCacheLayer, toolDefinitionModuleLayer)), +); diff --git a/src/registry/registry.test.ts b/src/domains/tools/runtime/tool-registry.integration.test.ts similarity index 51% rename from src/registry/registry.test.ts rename to src/domains/tools/runtime/tool-registry.integration.test.ts index d2f1545..d6d27a4 100644 --- a/src/registry/registry.test.ts +++ b/src/domains/tools/runtime/tool-registry.integration.test.ts @@ -2,11 +2,157 @@ import { afterEach, describe, expect, test } from "vite-plus/test"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { RegistryConfigServiceClass } from "./registry"; -import { ToolDiscoveryServiceClass } from "./discover"; -import { ToolCreatorClass } from "../tools/create"; -import { CurrentRigToolApiVersion } from "../tools/domain/tool-api"; -import { ToolListServiceClass, ToolMetadataCacheClass } from "../tools/list"; +import { Effect, Layer } from "effect"; +import { rigConfigStoreLayer } from "../../settings/service/config-store"; +import { atomicFileWriterLayer } from "../../../providers/filesystem/atomic-file-writer"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + RigPathsService, +} from "../../../providers/paths/rig-paths"; +import { rigShellLayer } from "../../../providers/process/rig-shell-service"; +import { RegistryService, registryLayer } from "../../registry/service/registry"; +import { ToolDiscoveryService, toolDiscoveryLayer } from "../../registry/service/tool-discovery"; +import type { DiscoveredTool } from "../../registry/types/tool-discovery"; +import { CurrentRigToolApiVersion } from "../config/tool-api"; +import { schemaRendererLayer } from "../service/schema-renderer"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { toolIdentifierLayer } from "../service/tool-identifier"; +import { runtimeSupportRendererLayer } from "./runtime-support"; +import { ToolFilesService } from "./tool-files"; +import { toolEnvironmentLayer } from "./tool-environment"; +import { toolLoaderLayer } from "./tool-loader"; +import { rigToolKitLayer } from "./tool-sdk"; +import { + ToolListService, + ToolMetadataCacheService, + toolMetadataCacheLayer, + type ToolListData, +} from "./tool-list"; + +const pathLayers = (homeDir: string) => { + const options = Layer.succeed(RigPathsOptionsConfigService, { homeDir }); + const platform = RigPathsPlatformService.layer; + const inputs = Layer.merge(options, platform); + const config = RigPathsConfigService.layer.pipe(Layer.provide(inputs)); + const paths = RigPathsService.layer.pipe(Layer.provide(Layer.merge(config, platform))); + return { inputs, config, paths, services: Layer.merge(config, paths) } as const; +}; + +const registryService = (homeDir: string) => { + const paths = pathLayers(homeDir); + const infrastructure = Layer.merge( + atomicFileWriterLayer, + runtimeSupportRendererLayer.pipe(Layer.provide(paths.config)), + ); + const layer = registryLayer.pipe(Layer.provide(paths.inputs), Layer.provide(infrastructure)); + const run = (effect: Effect.Effect) => + Effect.runPromise(effect.pipe(Effect.provide(layer))); + return { + list: () => run(RegistryService.use((service) => service.list)), + add: (path: string) => run(RegistryService.use((service) => service.add(path))), + remove: (path: string) => run(RegistryService.use((service) => service.remove(path))), + } as const; +}; + +const discoveryService = (homeDir: string) => { + const paths = pathLayers(homeDir); + const infrastructure = Layer.merge( + atomicFileWriterLayer, + runtimeSupportRendererLayer.pipe(Layer.provide(paths.config)), + ); + const registry = registryLayer.pipe(Layer.provide(paths.inputs), Layer.provide(infrastructure)); + const layer = toolDiscoveryLayer.pipe(Layer.provide(Layer.merge(registry, paths.services))); + const run = (effect: Effect.Effect) => + Effect.runPromise(effect.pipe(Effect.provide(layer))); + return { + discover: () => run(ToolDiscoveryService.use((service) => service.discover())), + find: (name: string) => run(ToolDiscoveryService.use((service) => service.find(name))), + layer, + } as const; +}; + +const toolServices = (homeDir: string) => { + const paths = pathLayers(homeDir); + const infrastructure = Layer.merge( + atomicFileWriterLayer, + runtimeSupportRendererLayer.pipe(Layer.provide(paths.config)), + ); + const config = rigConfigStoreLayer.pipe( + Layer.provide(paths.inputs), + Layer.provide(infrastructure), + ); + const registry = registryLayer.pipe(Layer.provide(paths.inputs), Layer.provide(infrastructure)); + const discovery = toolDiscoveryLayer.pipe(Layer.provide(Layer.merge(registry, paths.services))); + const toolkit = rigToolKitLayer.pipe( + Layer.provide(Layer.mergeAll(rigShellLayer, paths.config, paths.services)), + ); + const loader = toolLoaderLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolEnvironmentLayer, + discovery, + toolDefinitionLayer, + toolIdentifierLayer, + toolkit, + ), + ), + ); + const metadata = toolMetadataCacheLayer.pipe( + Layer.provide( + Layer.mergeAll( + paths.config, + loader, + atomicFileWriterLayer, + schemaRendererLayer, + toolIdentifierLayer, + ), + ), + ); + const files = ToolFilesService.layer.pipe( + Layer.provide( + Layer.mergeAll(paths.services, config, discovery, atomicFileWriterLayer, toolDefinitionLayer), + ), + ); + const list = ToolListService.layer.pipe( + Layer.provide(Layer.mergeAll(discovery, metadata, toolIdentifierLayer)), + ); + return { discovery, files, list, metadata } as const; +}; + +const createTool = (homeDir: string, name: string) => + Effect.runPromise( + ToolFilesService.use((service) => service.create(name)).pipe( + Effect.provide(toolServices(homeDir).files), + ), + ); + +const toolListService = (homeDir: string) => { + const layer = toolServices(homeDir).list; + return { + list: (options?: { visibleFromPath?: string }) => + Effect.runPromise( + ToolListService.use((service) => service.list(options)).pipe(Effect.provide(layer)), + ), + renderPlain: (data: ToolListData) => + Effect.runPromise( + ToolListService.use((service) => service.renderPlain(data)).pipe(Effect.provide(layer)), + ), + } as const; +}; + +const toolMetadataCacheService = (homeDir: string) => { + const layer = toolServices(homeDir).metadata; + return { + load: (entries: DiscoveredTool[]) => + Effect.runPromise( + ToolMetadataCacheService.use((service) => service.load(entries)).pipe( + Effect.provide(layer), + ), + ), + } as const; +}; class TestHomeStore { private readonly homes: string[] = []; @@ -26,92 +172,7 @@ class TestHomeStore { const homes = new TestHomeStore(); -afterEach(async () => { - await homes.cleanup(); -}); - -describe("registries", () => { - test("adds and removes a custom registry", async () => { - const home = await homes.create(); - const registry = join(home, "project-tools"); - const service = new RegistryConfigServiceClass({ homeDir: home }); - const added = await service.add(registry); - expect(added.customRegistries).toContain(registry); - - const removed = await service.remove(registry); - expect(removed.customRegistries).not.toContain(registry); - }); - - test("preserves concurrent registry additions", async () => { - const home = await homes.create(); - const registries = Array.from({ length: 12 }, (_, index) => - join(home, `project-tools-${index}`), - ); - - await Promise.all( - registries.map((registry) => new RegistryConfigServiceClass({ homeDir: home }).add(registry)), - ); - - expect( - (await new RegistryConfigServiceClass({ homeDir: home }).list()).customRegistries.toSorted(), - ).toEqual(registries.toSorted()); - }); - - test("discovers index.rig.tsx entry files", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "view-tool"); - await mkdir(toolDir, { recursive: true }); - await writeFile(join(toolDir, "index.rig.tsx"), "export default {};\n", "utf8"); - - const discovery = new ToolDiscoveryServiceClass({ homeDir: home }); - const tools = await discovery.discover(); - - expect(tools).toMatchObject([ - { - name: "view-tool", - toolPath: join(toolDir, "index.rig.tsx"), - }, - ]); - expect(await discovery.find("view-tool")).toMatchObject({ - name: "view-tool", - toolPath: join(toolDir, "index.rig.tsx"), - }); - }); - - test("rejects legacy tool.ts entry files", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "legacy"); - await mkdir(toolDir, { recursive: true }); - await writeFile(join(toolDir, "tool.ts"), "export default {};\n", "utf8"); - - const discovery = new ToolDiscoveryServiceClass({ homeDir: home }); - await expect(discovery.discover()).rejects.toThrow( - "Tool legacy must use index.rig.ts or index.rig.tsx.", - ); - await expect(discovery.find("legacy")).rejects.toThrow( - "Tool legacy must use index.rig.ts or index.rig.tsx.", - ); - }); - - test("lists base tools and custom registries visible to a project path", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("base-visible"); - - const project = join(home, "project"); - const custom = join(project, "rig-tools"); - const outside = join(home, "outside-tools"); - const registry = new RegistryConfigServiceClass({ homeDir: home }); - await mkdir(join(project, ".git"), { recursive: true }); - await registry.add(outside); - await registry.add(custom); - await mkdir(join(outside, "local-only"), { recursive: true }); - await mkdir(join(custom, "project-tool"), { recursive: true }); - await mkdir(join(custom, "many-fields"), { recursive: true }); - await mkdir(join(custom, "scalar-example"), { recursive: true }); - await mkdir(join(custom, "scalar-required"), { recursive: true }); - await writeFile( - join(outside, "local-only", "index.rig.ts"), - `export default (rig) => rig.defineTool({ +const localOnlyToolSource = `export default (rig) => rig.defineTool({ name: "local-only", description: "Outside custom registry test tool.", commands: { @@ -123,12 +184,9 @@ describe("registries", () => { }), }, }); -`, - "utf8", - ); - await writeFile( - join(custom, "project-tool", "index.rig.ts"), - `export default (rig) => rig.defineTool({ +`; + +const projectToolSource = `export default (rig) => rig.defineTool({ name: "project-tool", description: "Project visible test tool.", commands: { @@ -146,12 +204,9 @@ describe("registries", () => { }), }, }); -`, - "utf8", - ); - await writeFile( - join(custom, "many-fields", "index.rig.ts"), - `export default (rig) => rig.defineTool({ +`; + +const manyFieldsToolSource = `export default (rig) => rig.defineTool({ name: "many-fields", description: "Many fields test tool.", commands: { @@ -170,12 +225,9 @@ describe("registries", () => { }), }, }); -`, - "utf8", - ); - await writeFile( - join(custom, "scalar-example", "index.rig.ts"), - `export default (rig) => rig.defineTool({ +`; + +const scalarExampleToolSource = `export default (rig) => rig.defineTool({ name: "scalar-example", description: "Scalar example test tool.", commands: { @@ -195,12 +247,9 @@ describe("registries", () => { }), }, }); -`, - "utf8", - ); - await writeFile( - join(custom, "scalar-required", "index.rig.ts"), - `export default (rig) => rig.defineTool({ +`; + +const scalarRequiredToolSource = `export default (rig) => rig.defineTool({ name: "scalar-required", description: "Scalar required test tool.", commands: { @@ -212,22 +261,117 @@ describe("registries", () => { }), }, }); -`, - "utf8", +`; + +const writeToolEntry = async (registryPath: string, name: string, source: string) => { + const toolDir = join(registryPath, name); + await mkdir(toolDir, { recursive: true }); + await writeFile(join(toolDir, "index.rig.ts"), source, "utf8"); +}; + +const createVisibilityFixture = async (home: string) => { + const project = join(home, "project"); + const custom = join(project, "rig-tools"); + const outside = join(home, "outside-tools"); + const registry = registryService(home); + await mkdir(join(project, ".git"), { recursive: true }); + await registry.add(outside); + await registry.add(custom); + await Promise.all([ + writeToolEntry(outside, "local-only", localOnlyToolSource), + writeToolEntry(custom, "project-tool", projectToolSource), + writeToolEntry(custom, "many-fields", manyFieldsToolSource), + writeToolEntry(custom, "scalar-example", scalarExampleToolSource), + writeToolEntry(custom, "scalar-required", scalarRequiredToolSource), + ]); + return project; +}; + +const expectVisibleToolCommands = (rendered: string) => { + expect(rendered).toContain("rig run base-visible.example text=example #"); + expect(rendered).toContain("rig run project-tool.see text=VALUE #"); + expect(rendered).toContain("rig run project-tool.none #"); + expect(rendered).toContain("rig run many-fields.pack --input"); + expect(rendered).toContain("rig run scalar-example.say 'two words'"); + expect(rendered).toContain("rig run scalar-example.count count=2"); + expect(rendered).toContain("rig run scalar-required.say #"); + expect(rendered).toContain("rig run local-only.example"); +}; + +afterEach(async () => { + await homes.cleanup(); +}); + +describe("registries", () => { + test("adds and removes a custom registry", async () => { + const home = await homes.create(); + const registry = join(home, "project-tools"); + const service = registryService(home); + const added = await service.add(registry); + expect(added.customRegistries).toContain(registry); + + const removed = await service.remove(registry); + expect(removed.customRegistries).not.toContain(registry); + }); + + test("preserves concurrent registry additions", async () => { + const home = await homes.create(); + const registries = Array.from({ length: 12 }, (_, index) => + join(home, `project-tools-${index}`), + ); + + await Promise.all(registries.map((registry) => registryService(home).add(registry))); + + expect((await registryService(home).list()).customRegistries.toSorted()).toEqual( + registries.toSorted(), ); + }); + + test("discovers index.rig.tsx entry files", async () => { + const home = await homes.create(); + const toolDir = join(home, "rig", "tools", "view-tool"); + await mkdir(toolDir, { recursive: true }); + await writeFile(join(toolDir, "index.rig.tsx"), "export default {};\n", "utf8"); + + const discovery = discoveryService(home); + const tools = await discovery.discover(); - const service = new ToolListServiceClass({ homeDir: home }); + expect(tools).toMatchObject([ + { + name: "view-tool", + toolPath: join(toolDir, "index.rig.tsx"), + }, + ]); + expect(await discovery.find("view-tool")).toMatchObject({ + name: "view-tool", + toolPath: join(toolDir, "index.rig.tsx"), + }); + }); + + test("rejects legacy tool.ts entry files", async () => { + const home = await homes.create(); + const toolDir = join(home, "rig", "tools", "legacy"); + await mkdir(toolDir, { recursive: true }); + await writeFile(join(toolDir, "tool.ts"), "export default {};\n", "utf8"); + + const discovery = discoveryService(home); + await expect(discovery.discover()).rejects.toThrow( + "Tool legacy must use index.rig.ts or index.rig.tsx.", + ); + await expect(discovery.find("legacy")).rejects.toThrow( + "Tool legacy must use index.rig.ts or index.rig.tsx.", + ); + }); + + test("lists base tools and custom registries visible to a project path", async () => { + expect.assertions(8); + const home = await homes.create(); + await createTool(home, "base-visible"); + const project = await createVisibilityFixture(home); + const service = toolListService(home); const list = await service.list({ visibleFromPath: join(project, "AGENTS.md") }); - const rendered = service.renderPlain(list); - - expect(rendered).toContain("rig run base-visible.example text=example #"); - expect(rendered).toContain("rig run project-tool.see text=VALUE #"); - expect(rendered).toContain("rig run project-tool.none #"); - expect(rendered).toContain("rig run many-fields.pack --input"); - expect(rendered).toContain("rig run scalar-example.say 'two words'"); - expect(rendered).toContain("rig run scalar-example.count count=2"); - expect(rendered).toContain("rig run scalar-required.say #"); - expect(rendered).toContain("rig run local-only.example"); + const rendered = await service.renderPlain(list); + expectVisibleToolCommands(rendered); }); test("reuses serialized metadata until a tool entry changes", async () => { @@ -260,8 +404,8 @@ describe("registries", () => { "utf8", ); - const first = await new ToolListServiceClass({ homeDir: home }).list(); - const second = await new ToolListServiceClass({ homeDir: home }).list(); + const first = await toolListService(home).list(); + const second = await toolListService(home).list(); expect(first.tools[0]?.description).toBe("Cached metadata load 1."); expect(first.tools[0]?.collections).toEqual([ { name: "notes", hasSchema: true }, @@ -276,7 +420,7 @@ describe("registries", () => { 'description: "Updated metadata load " + load + ".",', ), ); - const updated = await new ToolListServiceClass({ homeDir: home }).list(); + const updated = await toolListService(home).list(); expect(updated.tools[0]?.description).toBe("Updated metadata load 2."); }); @@ -292,14 +436,14 @@ describe("registries", () => { const home = await homes.create(); await mkdir(join(home, "rig"), { recursive: true }); await writeFile(join(home, "rig", "tool-metadata.json"), JSON.stringify(value), "utf8"); - return new ToolMetadataCacheClass({ homeDir: home }).load([]); + return toolMetadataCacheService(home).load([]); }), ); expect(results).toEqual(incompatible.map(() => [])); }); test("renders plain list entries without embedded line breaks", async () => { - const rendered = new ToolListServiceClass().renderPlain({ + const rendered = await toolListService(process.cwd()).renderPlain({ tools: [ { name: "wrapped", @@ -329,13 +473,13 @@ describe("registries", () => { test("detects duplicate tool names across registries", async () => { const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); + await createTool(home, "sample"); const custom = join(home, "custom-tools"); - await new RegistryConfigServiceClass({ homeDir: home }).add(custom); + await registryService(home).add(custom); await mkdir(join(custom, "sample"), { recursive: true }); await writeFile(join(custom, "sample", "index.rig.ts"), "export default {};\n", "utf8"); - const discovery = new ToolDiscoveryServiceClass({ homeDir: home }); + const discovery = discoveryService(home); await expect(discovery.discover()).rejects.toThrow("Duplicate tool name: sample"); await expect(discovery.find("sample")).rejects.toThrow("Duplicate tool name: sample"); await expect(discovery.find("../sample")).rejects.toThrow("Tool not found: ../sample"); @@ -347,9 +491,9 @@ describe("registries", () => { await mkdir(toolsDir, { recursive: true }); await writeFile(join(toolsDir, "plain-file"), "not a tool\n", "utf8"); - await expect( - new ToolDiscoveryServiceClass({ homeDir: home }).find("plain-file"), - ).rejects.toThrow("Tool not found: plain-file"); + await expect(discoveryService(home).find("plain-file")).rejects.toThrow( + "Tool not found: plain-file", + ); }); test("rejects multiple direct entry files", async () => { @@ -361,7 +505,7 @@ describe("registries", () => { writeFile(join(toolDir, "index.rig.tsx"), "export default {};\n", "utf8"), ]); - await expect(new ToolDiscoveryServiceClass({ homeDir: home }).find("multiple")).rejects.toThrow( + await expect(discoveryService(home).find("multiple")).rejects.toThrow( "multiple Rig entry files", ); }); diff --git a/src/domains/tools/runtime/tool-runner-cache.integration.test.ts b/src/domains/tools/runtime/tool-runner-cache.integration.test.ts new file mode 100644 index 0000000..21b07ea --- /dev/null +++ b/src/domains/tools/runtime/tool-runner-cache.integration.test.ts @@ -0,0 +1,388 @@ +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { existsSync } from "node:fs"; +import { mkdir, writeFile } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import * as Harness from "../../../../test/fixtures/tool-runner-test-harness"; + +type ToolCache = Awaited>; + +const assertCachePathsAndKeys = (cache: ToolCache, cachePath: string, toolPath: string) => { + expect(Harness.cachePathForToolPath(toolPath)).toBe(cachePath); + expect(cache.path).toBe(cachePath); + expect(existsSync(cachePath)).toBe(false); + expect(cache.peek(["missing"])).toBeUndefined(); + expect(existsSync(cachePath)).toBe(true); + + const objectKey = ["todos", { b: 2, omitted: undefined, a: 1 }] as const; + cache.set(objectKey, { ok: true }); + expect(cache.peek(["todos", { a: 1, b: 2 }])).toEqual({ ok: true }); + expect(cache.peek(["todos", { a: 1, b: 3 }])).toBeUndefined(); + + const shared = { value: true }; + const nullPrototype = Object.assign(Object.create(null) as Record, { + enabled: true, + }); + cache.set(["json", null, true, 1, "text", shared, shared, nullPrototype], "valid"); + expect(cache.peek(["json", null, true, 1, "text", shared, shared, { enabled: true }])).toBe( + "valid", + ); +}; + +const assertCacheQueryControls = async (cache: ToolCache, warnings: unknown[]) => { + let calls = 0; + const first = cache.query({ + queryKey: ["dedupe"], + queryFn: async () => `value-${++calls}`, + }); + const second = cache.query({ + queryKey: ["dedupe"], + queryFn: async () => `ignored-${++calls}`, + }); + await expect(Promise.all([first, second])).resolves.toEqual(["value-1", "value-1"]); + expect(calls).toBe(1); + await expect( + cache.query({ queryKey: ["dedupe"], staleTime: Infinity, queryFn: () => "ignored" }), + ).resolves.toBe("value-1"); + + cache.set(["invalidated"], "old"); + cache.invalidate(["invalidated"]); + await expect( + cache.query({ queryKey: ["invalidated"], staleTime: Infinity, queryFn: () => "new" }), + ).resolves.toBe("new"); + expect(cache.peek(["invalidated"])).toBe("new"); + + cache.set(["failure"], "stale"); + await expect( + cache.query({ + queryKey: ["failure"], + staleTime: 0, + queryFn: () => { + throw new Error("foreground failure"); + }, + }), + ).rejects.toThrow("foreground failure"); + expect(cache.peek(["failure"])).toBe("stale"); + await expect( + cache.query({ + queryKey: ["cold-failure"], + queryFn: () => Promise.reject(new Error("cold failure")), + }), + ).rejects.toThrow("cold failure"); + expect(warnings).toHaveLength(0); +}; + +const assertCacheRemovalAndValidation = async (cache: ToolCache) => { + cache.set(["remove"], true); + cache.remove(["remove"]); + expect(cache.peek(["remove"])).toBeUndefined(); + cache.set(["clear-a"], true); + cache.set(["clear-b"], true); + cache.clear(); + expect(cache.peek(["clear-a"])).toBeUndefined(); + + expect(() => cache.peek([])).toThrow("query keys must be non-empty arrays"); + expect(() => cache.peek("bad" as never)).toThrow("query keys must be non-empty arrays"); + for (const invalid of [ + Number.NaN, + Number.POSITIVE_INFINITY, + undefined, + () => true, + Symbol("bad"), + 1n, + new Date(), + ]) { + expect(() => cache.peek([invalid])).toThrow("JSON-compatible values"); + } + const cyclic: unknown[] = []; + cyclic.push(cyclic); + expect(() => cache.peek(cyclic)).toThrow("cannot contain cycles"); + expect(() => cache.set(["undefined"], undefined)).toThrow("cannot store undefined"); + await Promise.all( + ([-1, Number.NaN, "bad"] as const).map((staleTime) => + expect( + cache.query({ + queryKey: ["bad-stale"], + staleTime: staleTime as never, + queryFn: () => true, + }), + ).rejects.toThrow("staleTime must be a non-negative number"), + ), + ); +}; + +const assertCacheCollisionDetection = (cache: ToolCache, cachePath: string) => { + cache.set(["collision"], true); + const row = [...(Harness.FakeSqliteDatabase.store(cachePath)?.cache.values() ?? [])].find( + (value) => value.valueJson === "true", + ); + expect(row).toBeDefined(); + row!.keyJson = '["different"]'; + expect(() => cache.peek(["collision"])).toThrow("query key hash collision"); +}; + +const assertUnavailableCache = async (toolPath: string, log: never) => { + new Harness.FakeSqliteEnvironment().uninstall(); + const cache = await Harness.setupToolCache({ path: toolPath } as never, log); + const runtimeError = "context.cache requires the Bun SQLite runtime"; + expect(() => cache.query({ queryKey: ["key"], queryFn: () => true })).toThrow(runtimeError); + expect(() => cache.peek(["key"])).toThrow(runtimeError); + expect(() => cache.set(["key"], true)).toThrow(runtimeError); + expect(() => cache.invalidate(["key"])).toThrow(runtimeError); + expect(() => cache.remove(["key"])).toThrow(runtimeError); + expect(() => cache.clear()).toThrow(runtimeError); + expect(() => cache.close()).not.toThrow(); +}; + +afterEach(Harness.cleanupToolRunnerTests); + +describe("tool persistence", () => { + test("provides a persistent query cache with foreground refreshes", async () => { + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "cached"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "cached", + description: "Cache test tool.", + commands: { + read: rig.defineCommand({ + description: "Read cached data.", + input: rig.z.object({ staleTime: rig.z.number(), fail: rig.z.boolean().optional() }), + output: rig.z.object({ value: rig.z.string(), calls: rig.z.number(), cachePath: rig.z.string() }), + run: async (context) => { + const value = await context.cache.query({ + queryKey: ["value"], + staleTime: context.input.staleTime, + queryFn: async () => { + const calls = (context.kv.get("calls") ?? 0) + 1; + context.kv.set("calls", calls); + if (context.input.fail) throw new Error("refresh failed"); + return "value-" + calls; + }, + }); + return { value, calls: context.kv.get("calls") ?? 0, cachePath: context.cache.path }; + }, + }), + }, +}); +`, + "utf8", + ); + + const runner = Harness.createToolRunner({ homeDir: home }); + const cold = await runner.run("cached", "read", { + homeDir: home, + input: JSON.stringify({ staleTime: 60_000 }), + }); + const fresh = await runner.run("cached", "read", { + homeDir: home, + input: JSON.stringify({ staleTime: 60_000 }), + }); + const stale = await runner.run("cached", "read", { + homeDir: home, + input: JSON.stringify({ staleTime: 0 }), + }); + const refreshed = await runner.run("cached", "read", { + homeDir: home, + input: JSON.stringify({ staleTime: 60_000 }), + }); + const failedRefresh = await runner.run("cached", "read", { + homeDir: home, + input: JSON.stringify({ staleTime: 0, fail: true }), + }); + const cachePath = join(toolDir, "cache.sqlite"); + + expect(cold).toMatchObject({ + exitCode: 0, + envelope: { data: { value: "value-1", calls: 1, cachePath } }, + }); + expect(fresh).toMatchObject({ + exitCode: 0, + envelope: { data: { value: "value-1", calls: 1, cachePath } }, + }); + expect(stale).toMatchObject({ + exitCode: 0, + envelope: { data: { value: "value-2", calls: 2, cachePath } }, + }); + expect(refreshed).toMatchObject({ + exitCode: 0, + envelope: { data: { value: "value-2", calls: 2, cachePath } }, + }); + expect(failedRefresh).toMatchObject({ + exitCode: 1, + envelope: { errors: [{ code: "INTERNAL_ERROR", message: "refresh failed" }] }, + }); + expect(existsSync(cachePath)).toBe(true); + }); + + test("supports cache controls, deterministic keys, validation, and unavailable runtimes", async () => { + expect.hasAssertions(); + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const toolPath = join(home, "rig", "tools", "cache-api", "index.rig.ts"); + const warnings: Array<{ bindings: unknown; message: unknown }> = []; + const log = { + warn: (bindings: unknown, message: unknown) => warnings.push({ bindings, message }), + } as never; + const cache = await Harness.setupToolCache({ path: toolPath } as never, log); + const cachePath = join(dirname(toolPath), "cache.sqlite"); + + assertCachePathsAndKeys(cache, cachePath, toolPath); + await assertCacheQueryControls(cache, warnings); + await assertCacheRemovalAndValidation(cache); + assertCacheCollisionDetection(cache, cachePath); + cache.close(); + await assertUnavailableCache(toolPath, log); + }); + + test("closes partial lazy state initialization and retries cleanly", async () => { + const sqlite = new Harness.FakeSqliteEnvironment(); + sqlite.install(); + const home = await Harness.homes.create(); + const toolPath = join(home, "rig", "tools", "partial", "index.rig.ts"); + const kvPath = join(dirname(toolPath), "kv.sqlite"); + const cachePath = join(dirname(toolPath), "cache.sqlite"); + const log = { warn: () => undefined } as never; + + const kv = await Harness.setupToolKvStore({ path: toolPath } as never); + sqlite.failNextInitialization(); + expect(() => kv.get("key")).toThrow("sqlite initialization failed"); + expect(Harness.FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(1); + expect(kv.get("key")).toBeUndefined(); + (kv as { close(): void }).close(); + expect(Harness.FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(2); + + const cache = await Harness.setupToolCache({ path: toolPath } as never, log); + sqlite.failNextInitialization(); + expect(() => cache.peek(["key"])).toThrow("sqlite initialization failed"); + expect(Harness.FakeSqliteDatabase.store(cachePath)?.closeRuns).toBe(1); + expect(cache.peek(["key"])).toBeUndefined(); + cache.close(); + expect(Harness.FakeSqliteDatabase.store(cachePath)?.closeRuns).toBe(2); + }); + + test("returns changed migration errors as envelopes", async () => { + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const writer = new Harness.DbSetupTestToolWriter(home); + const toolPath = await writer.write( + "db-changed", + 'db.migrate(1, "create items", "create table items (id integer primary key);");', + ); + + await expect( + Harness.createToolRunner({ homeDir: home }).run("db-changed", "check", { homeDir: home }), + ).resolves.toMatchObject({ exitCode: 0 }); + await writer.write( + "db-changed", + 'db.migrate(1, "create items", "create table items (id integer primary key, text text);");', + ); + expect(toolPath).toBe(join(home, "rig", "tools", "db-changed", "index.rig.ts")); + + const result = await Harness.createToolRunner({ homeDir: home }).run("db-changed", "check", { + homeDir: home, + }); + expect(result).toMatchObject({ + exitCode: 1, + envelope: { + errors: [ + { + code: "TOOL_INVALID", + message: "Migration 1 has changed since it was applied.", + }, + ], + }, + }); + }); + + test("returns invalid migration declaration errors as envelopes", async () => { + new Harness.FakeSqliteEnvironment().install(); + const cases = [ + { + name: "db-order", + setup: + 'db.migrate(2, "create second", "create table second (id integer primary key);"); db.migrate(1, "create first", "create table first (id integer primary key);");', + message: "Migration versions must be declared in ascending order", + }, + { + name: "db-version", + setup: 'db.migrate(0, "bad", "select 1;");', + message: "Migration version must be a positive integer", + }, + { + name: "db-name", + setup: 'db.migrate(1, " ", "select 1;");', + message: "Migration name must not be empty.", + }, + { + name: "db-sql", + setup: 'db.migrate(1, "empty", " ");', + message: "Migration 1 SQL must not be empty.", + }, + ]; + + const results = await Promise.all( + cases.map(async (item) => { + const home = await Harness.homes.create(); + await new Harness.DbSetupTestToolWriter(home).write(item.name, item.setup); + return { + item, + result: await Harness.createToolRunner({ homeDir: home }).run(item.name, "check", { + homeDir: home, + }), + }; + }), + ); + + for (const { item, result } of results) { + expect(result).toMatchObject({ + exitCode: 1, + envelope: { + errors: [{ code: "TOOL_INVALID", message: expect.stringContaining(item.message) }], + }, + }); + } + }); + + test("requires setupDb before commands use context.db", async () => { + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "missing-db"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "missing-db", + description: "Missing setupDb test tool.", + commands: { + read: rig.defineCommand({ + description: "Read from DB without setupDb.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async (context) => { + context.db.query("select 1").get(); + return { ok: true }; + }, + }), + }, +}); +`, + "utf8", + ); + + await expect( + Harness.createToolRunner({ homeDir: home }).run("missing-db", "read", { homeDir: home }), + ).resolves.toMatchObject({ + exitCode: 1, + envelope: { + errors: [ + { + code: "TOOL_INVALID", + message: "Tool missing-db must define setupDb before using context.db.", + }, + ], + }, + }); + }); +}); diff --git a/src/domains/tools/runtime/tool-runner-command.integration.test.ts b/src/domains/tools/runtime/tool-runner-command.integration.test.ts new file mode 100644 index 0000000..6baa83e --- /dev/null +++ b/src/domains/tools/runtime/tool-runner-command.integration.test.ts @@ -0,0 +1,543 @@ +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { existsSync } from "node:fs"; +import { mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import type { CommandDefinition } from "../types/tool-types"; +import * as Harness from "../../../../test/fixtures/tool-runner-test-harness"; + +afterEach(Harness.cleanupToolRunnerTests); + +describe("tool commands", () => { + test("normalizes non-Error JSON parser failures", async () => { + const parse = JSON.parse; + JSON.parse = () => { + throw "parser failed"; + }; + + try { + await expect( + Harness.readRunInput({} as CommandDefinition, { input: "{" }), + ).rejects.toMatchObject({ + code: "INPUT_ERROR", + details: { message: "parser failed" }, + }); + } finally { + JSON.parse = parse; + } + }); + + test("creates a starter tool with definition-owned examples", async () => { + const home = await Harness.homes.create(); + const result = await Harness.createTool({ homeDir: home }, "sample"); + expect(result.files).toHaveLength(1); + expect(result.toolPath).toBe(join(home, "rig", "tools", "sample", "index.rig.ts")); + expect(result.id).toBe("sample.example"); + + const help = await Harness.renderToolHelp({ homeDir: home }, "sample", "example"); + const commandIdHelp = await Harness.renderToolHelp({ homeDir: home }, "sample.example"); + expect(commandIdHelp).toMatch( + /^Tool: sample\nCommand: example\nRun: rig run sample\.example \[args\.\.\.\]/, + ); + expect(commandIdHelp).not.toContain("```bash"); + expect(help).toContain("Run the example command"); + expect(help).toContain("rig run sample.example"); + expect(help).toContain("Input:"); + expect(help).toContain("Output:"); + }); + + test("inspects command metadata as JSON", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + const inspected = await Harness.inspectTool({ homeDir: home }, "sample", "example"); + expect(inspected).toMatchObject({ + tool: "sample", + command: "example", + id: "sample.example", + run: "rig run sample.example [args...]", + }); + }); + + test("type-checks generated tools with injected Rig runtime types", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + const result = await Harness.typecheckTools({ homeDir: home }, "sample"); + + expect(result.ok).toBe(true); + expect(result.exitCode).toBe(0); + expect(result.checked).toHaveLength(1); + }); + + test("type-checks command output against output schemas", async () => { + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "bad-output"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `const tool: RigToolFactory = (rig) => rig.defineTool({ + name: "bad-output", + description: "Bad output test tool.", + commands: { + example: rig.defineCommand({ + description: "Return the wrong output type.", + input: rig.z.object({ text: rig.z.string() }), + output: rig.z.object({ text: rig.z.string() }), + run: async ({ input }) => ({ text: 123 }), + }), + }, +}); + +export default tool; +`, + "utf8", + ); + + const result = await Harness.typecheckTools({ homeDir: home }, "bad-output"); + + expect(result.ok).toBe(false); + expect(result.stdout).toContain("Type 'number' is not assignable to type 'string'"); + }); + + test("runs a command and returns a success envelope", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + const result = await Harness.createToolRunner({ homeDir: home }).run("sample", "example", { + homeDir: home, + args: ["Agent"], + }); + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ + data: { text: "Agent" }, + errors: [], + }); + const toolDir = join(home, "rig", "tools", "sample"); + for (const file of [ + "kv.sqlite", + "kv.sqlite-wal", + "kv.sqlite-shm", + "cache.sqlite", + "cache.sqlite-wal", + "cache.sqlite-shm", + ]) { + expect(existsSync(join(toolDir, file))).toBe(false); + } + }); + + test("resolves pipeline references in command input", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + + const result = await Harness.createToolRunner({ homeDir: home }).run("sample", "example", { + homeDir: home, + args: ["text=@clean.output"], + pipeContext: { clean: { output: "image.cleaned.png" } }, + }); + + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ + data: { text: "image.cleaned.png" }, + errors: [], + }); + + const exactId = await Harness.createToolRunner({ homeDir: home }).run("sample", "example", { + homeDir: home, + args: ["text=@clean"], + pipeContext: { clean: "image.cleaned.png" }, + }); + expect(exactId.envelope).toMatchObject({ + data: { text: "image.cleaned.png" }, + errors: [], + }); + + const embedded = await Harness.createToolRunner({ homeDir: home }).run("sample", "example", { + homeDir: home, + args: ["text=output:@clean.output"], + pipeContext: { clean: { output: "image.cleaned.png" } }, + }); + expect(embedded.envelope).toMatchObject({ + data: { text: "output:image.cleaned.png" }, + errors: [], + }); + + const arrayInput = await Harness.createToolRunner({ homeDir: home }).run("sample", "example", { + homeDir: home, + input: JSON.stringify({ text: "@names.0", ignored: ["@names.0"] }), + pipeContext: { names: ["image.cleaned.png"] }, + }); + expect(arrayInput.envelope).toMatchObject({ + data: { text: "image.cleaned.png" }, + errors: [], + }); + }); + + test("returns pipeline reference errors as envelopes", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + const runner = Harness.createToolRunner({ homeDir: home }); + + const unknown = await runner.run("sample", "example", { + homeDir: home, + args: ["text=@missing.output"], + pipeContext: {}, + }); + expect(unknown.envelope).toMatchObject({ + errors: [{ code: "INPUT_ERROR", message: "Pipeline reference is unknown: @missing" }], + }); + + const cannotAccess = await runner.run("sample", "example", { + homeDir: home, + args: ["text=@clean.output.path"], + pipeContext: { clean: { output: "image.cleaned.png" } }, + }); + expect(cannotAccess.envelope).toMatchObject({ + errors: [ + { + code: "INPUT_ERROR", + message: "Pipeline reference cannot access: @clean.output.path", + }, + ], + }); + + const missing = await runner.run("sample", "example", { + homeDir: home, + args: ["text=@clean.missing"], + pipeContext: { clean: { output: "image.cleaned.png" } }, + }); + expect(missing.envelope).toMatchObject({ + errors: [{ code: "INPUT_ERROR", message: "Pipeline reference is missing: @clean.missing" }], + }); + }); + + test("runs registered tools from a tool context", async () => { + const home = await Harness.homes.create(); + await Harness.createTool({ homeDir: home }, "sample"); + const toolDir = join(home, "rig", "tools", "caller"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "caller", + description: "Tool runner test tool.", + commands: { + call: rig.defineCommand({ + description: "Call another Rig tool.", + input: rig.z.object({ text: rig.z.string() }), + output: rig.z.object({ text: rig.z.string() }), + run: async (context) => { + const result = await context.rig.run({ + command: "sample.example", + input: { text: context.input.text }, + }); + return { text: result.text }; + }, + }), + }, +}); +`, + "utf8", + ); + + const result = await Harness.createToolRunner({ homeDir: home }).run("caller", "call", { + homeDir: home, + input: '{"text":"Nested"}', + }); + + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ + data: { text: "Nested" }, + errors: [], + }); + }); + + test("reuses loaded definitions across nested calls in one execution session", async () => { + const home = await Harness.homes.create(); + const toolsDir = join(home, "rig", "tools"); + const calleeDir = join(toolsDir, "callee"); + const callerDir = join(toolsDir, "caller"); + await mkdir(calleeDir, { recursive: true }); + await mkdir(callerDir, { recursive: true }); + await writeFile( + join(calleeDir, "index.rig.ts"), + `export default (rig) => { + globalThis.__rigNestedDefinitionLoads = (globalThis.__rigNestedDefinitionLoads ?? 0) + 1; + const definitionLoad = globalThis.__rigNestedDefinitionLoads; + return rig.defineTool({ + name: "callee", + description: "Nested cache test callee.", + commands: { + read: rig.defineCommand({ + description: "Return the definition load count.", + input: rig.z.object({}), + output: rig.z.number(), + run: () => definitionLoad, + }), + }, + }); +}; +`, + "utf8", + ); + await writeFile( + join(callerDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "caller", + description: "Nested cache test caller.", + commands: { + call: rig.defineCommand({ + description: "Call another Rig tool repeatedly.", + input: rig.z.object({ count: rig.z.number() }), + output: rig.z.array(rig.z.number()), + run: async (context) => { + const values = []; + for (let index = 0; index < context.input.count; index++) { + values.push(await context.rig.run({ command: "callee.read" })); + } + return values; + }, + }), + }, +}); +`, + "utf8", + ); + + const result = await Harness.createToolRunner({ homeDir: home }).run("caller", "call", { + input: JSON.stringify({ count: 50 }), + }); + + expect(result).toMatchObject({ exitCode: 0 }); + expect((result.envelope as { data: number[] }).data).toEqual(Array(50).fill(1)); + }); + + test("runs tools that import modules resolved from node_modules", async () => { + const home = await Harness.homes.create(); + const packageDir = join(home, "node_modules", "tool-helper"); + const toolDir = join(home, "rig", "tools", "external-import"); + await mkdir(packageDir, { recursive: true }); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(packageDir, "package.json"), + '{"name":"tool-helper","type":"module","exports":"./index.js"}\n', + "utf8", + ); + await writeFile( + join(packageDir, "index.js"), + "export function shout(value) { return String(value).toUpperCase(); }\n", + "utf8", + ); + await writeFile( + join(toolDir, "index.rig.ts"), + `import { shout } from "tool-helper"; + +export default (rig) => rig.defineTool({ + name: "external-import", + description: "External import test tool.", + commands: { + shout: rig.defineCommand({ + description: "Use an imported helper.", + input: rig.z.object({ text: rig.z.string() }), + output: rig.z.object({ text: rig.z.string() }), + run: async (context) => ({ text: shout(context.input.text) }), + }), + }, +}); +`, + "utf8", + ); + + const originalCwd = process.cwd(); + try { + const result = await Harness.createToolRunner({ homeDir: home }).run( + "external-import", + "shout", + { + homeDir: home, + input: '{"text":"modules"}', + }, + ); + expect(result).toMatchObject({ + exitCode: 0, + envelope: { data: { text: "MODULES" }, errors: [] }, + }); + } finally { + process.chdir(originalCwd); + } + }); + + test("validates tool .env and passes it to command contexts", async () => { + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "with-env"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, ".env"), + [ + "# Local tool env", + "API_TOKEN=secret", + "LIMIT=3", + 'MESSAGE="hello world"', + "SINGLE='literal value'", + "export OWNER=agent", + "", + ].join("\n"), + "utf8", + ); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "with-env", + description: "Env test tool.", + env: rig.z.object({ + API_TOKEN: rig.z.string().min(1), + LIMIT: rig.z.coerce.number(), + MESSAGE: rig.z.string(), + SINGLE: rig.z.string(), + OWNER: rig.z.string(), + }), + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ + token: rig.z.string(), + limit: rig.z.number(), + message: rig.z.string(), + single: rig.z.string(), + owner: rig.z.string(), + }), + run: async (context) => ({ + token: context.env.API_TOKEN, + limit: context.env.LIMIT, + message: context.env.MESSAGE, + single: context.env.SINGLE, + owner: context.env.OWNER, + }), + }), + }, +}); +`, + "utf8", + ); + + const result = await Harness.createToolRunner({ homeDir: home }).run("with-env", "read", { + homeDir: home, + }); + expect(result).toMatchObject({ + exitCode: 0, + envelope: { + data: { + token: "secret", + limit: 3, + message: "hello world", + single: "literal value", + owner: "agent", + }, + errors: [], + }, + }); + + const dryRun = await Harness.createToolRunner({ homeDir: home }).run("with-env", "read", { + homeDir: home, + dryRun: true, + }); + expect(JSON.stringify(dryRun.envelope)).not.toContain("secret"); + }); + + test("renders metadata without loading required tool environment values", async () => { + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "metadata-env"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "metadata-env", + description: "Metadata without env values.", + env: rig.z.object({ API_TOKEN: rig.z.string() }), + commands: { + read: rig.defineCommand({ + description: "Read required env.", + input: rig.z.object({}), + output: rig.z.string(), + run: (context) => context.env.API_TOKEN, + }), + }, +});\n`, + "utf8", + ); + + expect((await Harness.makeToolListClient({ homeDir: home }).list()).tools[0]?.name).toBe( + "metadata-env", + ); + expect(await Harness.renderToolHelp({ homeDir: home }, "metadata-env")).toContain( + "# metadata-env", + ); + expect(await Harness.inspectTool({ homeDir: home }, "metadata-env")).toMatchObject({ + name: "metadata-env", + }); + }); + + test("returns tool env load errors as envelopes", async () => { + const cases = [ + { + name: "missing-env", + envFile: undefined, + envSchema: "env: rig.z.object({ API_TOKEN: rig.z.string().min(1) }),", + message: "env is invalid", + }, + { + name: "env-without-schema", + envFile: "API_TOKEN=secret\n", + envSchema: "", + message: "has .env but no env schema", + }, + { + name: "bad-env-line", + envFile: "not a valid env line\n", + envSchema: "env: rig.z.object({}),", + message: "Invalid .env line", + }, + ]; + + const results = await Promise.all( + cases.map(async (item) => { + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", item.name); + await mkdir(toolDir, { recursive: true }); + if (item.envFile !== undefined) + await writeFile(join(toolDir, ".env"), item.envFile, "utf8"); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: ${JSON.stringify(item.name)}, + description: "Env error test tool.", + ${item.envSchema} + commands: { + read: rig.defineCommand({ + description: "Read env.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`, + "utf8", + ); + + return { + item, + result: await Harness.createToolRunner({ homeDir: home }).run(item.name, "read", { + homeDir: home, + }), + }; + }), + ); + + for (const { item, result } of results) { + expect(result).toMatchObject({ + exitCode: 1, + envelope: { + errors: [{ code: "TOOL_INVALID", message: expect.stringContaining(item.message) }], + }, + }); + } + }); +}); diff --git a/src/domains/tools/runtime/tool-runner-database.integration.test.ts b/src/domains/tools/runtime/tool-runner-database.integration.test.ts new file mode 100644 index 0000000..383e341 --- /dev/null +++ b/src/domains/tools/runtime/tool-runner-database.integration.test.ts @@ -0,0 +1,233 @@ +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { existsSync } from "node:fs"; +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import * as Harness from "../../../../test/fixtures/tool-runner-test-harness"; + +const assertUnavailableKvRuntime = async (home: string) => { + new Harness.FakeSqliteEnvironment().uninstall(); + const unavailable = await Harness.setupToolKvStore({ + path: join(home, "unavailable", "index.rig.ts"), + } as never); + expect(unavailable.path).toBe(join(home, "unavailable", "kv.sqlite")); + expect(() => unavailable.get("key")).toThrow("context.kv requires the Bun SQLite runtime"); + expect(() => unavailable.set("key", "value")).toThrow( + "context.kv requires the Bun SQLite runtime", + ); +}; + +afterEach(Harness.cleanupToolRunnerTests); + +describe("tool databases", () => { + test("runs setupDb before commands and stores index.sqlite beside the tool", async () => { + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "notes"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "notes", + description: "SQLite notes test tool.", + setupDb: (db) => { + db.migrate(1, "create notes", \` + create table notes ( + id integer primary key, + text text not null + ); + create table setup_runs ( + id integer primary key, + created_at text not null + ); + \`); + db.migrate(2, "index notes", "create index notes_text_idx on notes(text);"); + db.query("insert into setup_runs (created_at) values ($createdAt)").run({ + createdAt: new Date().toISOString(), + }); + }, + commands: { + add: rig.defineCommand({ + description: "Add a note.", + input: rig.z.object({ text: rig.z.string() }), + output: rig.z.object({ + id: rig.z.number(), + setupRuns: rig.z.number(), + dbPath: rig.z.string(), + }), + run: async (context) => { + const inserted = context.db + .query("insert into notes (text) values ($text)") + .run({ text: context.input.text }); + const row = context.db.query("select count(*) as count from setup_runs").get() as { count: number }; + return { id: inserted.lastInsertRowid, setupRuns: row.count, dbPath: context.db.path }; + }, + }), + }, +}); +`, + "utf8", + ); + + const runner = Harness.createToolRunner({ homeDir: home }); + const first = await runner.run("notes", "add", { homeDir: home, args: ["text=one"] }); + const second = await runner.run("notes", "add", { homeDir: home, args: ["text=two"] }); + const dbPath = join(toolDir, "index.sqlite"); + + expect(Harness.databasePathForToolPath(join(toolDir, "index.rig.ts"))).toBe(dbPath); + expect(first).toMatchObject({ + exitCode: 0, + envelope: { data: { id: 1, setupRuns: 1, dbPath } }, + }); + expect(second).toMatchObject({ + exitCode: 0, + envelope: { data: { id: 2, setupRuns: 2, dbPath } }, + }); + expect(existsSync(dbPath)).toBe(true); + }); + + test("closes databases when factory initialization or setupDb fails", async () => { + const sqlite = new Harness.FakeSqliteEnvironment(); + sqlite.install(); + const home = await Harness.homes.create(); + const toolPath = join(home, "rig", "tools", "failure", "index.rig.ts"); + const dbPath = join(dirname(toolPath), "index.sqlite"); + sqlite.failNextInitialization(); + await expect( + Harness.setupToolDatabase({ + path: toolPath, + definition: { setupDb: () => undefined }, + } as never), + ).rejects.toThrow("sqlite initialization failed"); + expect(Harness.FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(1); + + await expect( + Harness.setupToolDatabase({ + path: toolPath, + definition: { + setupDb: () => { + throw new Error("setupDb failed"); + }, + }, + } as never), + ).rejects.toThrow("setupDb failed"); + expect(Harness.FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(2); + }); + + test("skips collection initialization when a command does not use collections", async () => { + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "partial-setup"); + const toolPath = join(toolDir, "index.rig.ts"); + const dbPath = join(toolDir, "index.sqlite"); + await mkdir(toolDir, { recursive: true }); + await writeFile(join(toolDir, "blocked"), "prevents collection directory creation", "utf8"); + await writeFile( + toolPath, + `export default (rig) => rig.defineTool({ + name: "partial-setup", + description: "Partial setup cleanup test.", + setupDb: () => {}, + collections: { blocked: {} }, + commands: { + read: rig.defineCommand({ + description: "Should not run.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +});`, + "utf8", + ); + + const result = await Harness.createToolRunner({ homeDir: home }).run("partial-setup", "read", { + homeDir: home, + }); + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ data: { ok: true } }); + expect(Harness.FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(1); + }); + + test("provides tool loggers and sqlite-backed key-value state", async () => { + new Harness.FakeSqliteEnvironment().install(); + const home = await Harness.homes.create(); + const toolDir = join(home, "rig", "tools", "stateful"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "stateful", + description: "KV and logger test tool.", + commands: { + write: rig.defineCommand({ + description: "Write lightweight state.", + input: rig.z.object({ key: rig.z.string(), value: rig.z.string() }), + output: rig.z.object({ previous: rig.z.string().optional(), current: rig.z.string(), kvPath: rig.z.string() }), + run: async (context) => { + const previous = context.kv.get(context.input.key); + context.kv.set(context.input.key, context.input.value); + context.log.info({ key: context.input.key }, "Stored key-value state."); + return { + previous, + current: context.kv.get(context.input.key), + kvPath: context.kv.path, + }; + }, + }), + bad: rig.defineCommand({ + description: "Reject bad state.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async (context) => { + context.kv.set("bad", undefined); + return { ok: true }; + }, + }), + }, +}); +`, + "utf8", + ); + + const runner = Harness.createToolRunner({ homeDir: home }); + const first = await runner.run("stateful", "write", { + homeDir: home, + args: ["key=name", "value=one"], + }); + const second = await runner.run("stateful", "write", { + homeDir: home, + args: ["key=name", "value=two"], + }); + const bad = await runner.run("stateful", "bad", { homeDir: home }); + const kvPath = join(toolDir, "kv.sqlite"); + + expect(Harness.kvPathForToolPath(join(toolDir, "index.rig.ts"))).toBe(kvPath); + expect(first).toMatchObject({ + exitCode: 0, + envelope: { data: { current: "one", kvPath } }, + }); + expect(second).toMatchObject({ + exitCode: 0, + envelope: { data: { previous: "one", current: "two", kvPath } }, + }); + expect(bad).toMatchObject({ + exitCode: 1, + envelope: { errors: [{ code: "INPUT_ERROR" }] }, + }); + expect(existsSync(kvPath)).toBe(true); + expect(existsSync(join(toolDir, "cache.sqlite"))).toBe(false); + expect(Harness.FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(3); + + const logOutput = await readFile(join(home, "rig", ".logs", "rig.log"), "utf8"); + expect(logOutput).toContain('"prefix":"tool:stateful.write"'); + expect(logOutput).toContain("Stored key-value state."); + + const kv = await Harness.setupToolKvStore({ + path: join(toolDir, "index.rig.ts"), + } as never); + expect(() => kv.get("")).toThrow("context.kv keys must be non-empty strings."); + (kv as unknown as { close(): void }).close(); + + await assertUnavailableKvRuntime(home); + }); +}); diff --git a/src/domains/tools/runtime/tool-runner-output.test.ts b/src/domains/tools/runtime/tool-runner-output.test.ts new file mode 100644 index 0000000..768733d --- /dev/null +++ b/src/domains/tools/runtime/tool-runner-output.test.ts @@ -0,0 +1,206 @@ +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, +} from "../../../providers/paths/rig-paths"; +import { RunToolTestService, runTestHarnessLayer } from "./run-test-harness-service"; + +class RunOutputTestHome { + private readonly paths: string[] = []; + + async create(): Promise { + const home = await mkdtemp(join(tmpdir(), "rig-output-test-")); + this.paths.push(home); + return home; + } + + async cleanup(): Promise { + await Promise.all( + this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); + } +} + +const homes = new RunOutputTestHome(); +const layerForHome = (homeDir: string) => + runTestHarnessLayer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(RigPathsOptionsConfigService, { homeDir }), + RigPathsPlatformService.layer, + ), + ), + ); +const run = (homeDir: string, effect: Effect.Effect) => + Effect.runPromise(effect.pipe(Effect.provide(layerForHome(homeDir)))); + +afterEach(async () => homes.cleanup()); + +describe("tool command output", () => { + test("renders a compact plain command list", async () => { + const home = await homes.create(); + await run( + home, + RunToolTestService.use((service) => service.createTool("sample")), + ); + const list = await run( + home, + RunToolTestService.use((service) => service.list), + ); + const rendered = await run( + home, + RunToolTestService.use((service) => service.renderList(list)), + ); + expect(rendered).toContain("rig run sample.example text=example #"); + }); + + test("dry-runs a command without execution", async () => { + const home = await homes.create(); + await run( + home, + RunToolTestService.use((service) => service.createTool("sample")), + ); + const toolDir = join(home, "rig", "tools", "writer"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "writer", + description: "Writer test tool.", + commands: { + save: rig.defineCommand({ + description: "Save text.", + input: rig.z.object({ text: rig.z.string() }), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => { + throw new Error("dry-run should not execute command code"); + }, + }), + }, +}); +`, + "utf8", + ); + + const result = await run( + home, + RunToolTestService.use((service) => + service.run("writer", "save", { homeDir: home, args: ["text=Agent"], dryRun: true }), + ), + ); + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ + data: { + dryRun: true, + wouldRun: false, + id: "writer.save", + input: { text: "Agent" }, + }, + errors: [], + }); + }); + + test("truncates large command output and saves the full data to a temp file", async () => { + const home = await homes.create(); + const toolDir = join(home, "rig", "tools", "large"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "large", + description: "Large output test tool.", + commands: { + dump: rig.defineCommand({ + description: "Dump large text.", + input: rig.z.object({}), + output: rig.z.object({ text: rig.z.string() }), + run: async () => ({ text: "x".repeat(60 * 1024) }), + }), + }, +}); +`, + "utf8", + ); + const result = await run( + home, + RunToolTestService.use((service) => service.run("large", "dump", { homeDir: home })), + ); + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ + data: { + truncated: true, + previewFormat: "partial-json", + fullOutputFormat: "json", + totalBytes: expect.any(Number), + shownBytes: expect.any(Number), + }, + errors: [], + }); + const envelope = result.envelope as { data: { fullOutputPath: string } }; + const fullOutput = await readFile(envelope.data.fullOutputPath, "utf8"); + expect(fullOutput).toContain('"text"'); + expect(fullOutput.length).toBeGreaterThan(60 * 1024); + }); + + test("accepts raw Zod schemas without rig.input/output wrappers", async () => { + const home = await homes.create(); + const toolDir = join(home, "rig", "tools", "raw-schema"); + await mkdir(toolDir, { recursive: true }); + await writeFile( + join(toolDir, "index.rig.ts"), + `import { RigTool, z } from "../../runtime/sdk"; + +export default RigTool.define({ + name: "raw-schema", + description: "Raw schema test tool.", + commands: { + echo: { + description: "Echo using raw z.object.", + input: z.object({ text: z.string() }), + output: z.object({ text: z.string() }), + run: async ({ input }) => ({ text: input.text }), + }, + }, +}); +`, + "utf8", + ); + const result = await run( + home, + RunToolTestService.use((service) => + service.run("raw-schema", "echo", { homeDir: home, input: '{"text":"Agent"}' }), + ), + ); + expect(result.exitCode).toBe(0); + expect(result.envelope).toMatchObject({ data: { text: "Agent" }, errors: [] }); + }); + + test("returns an error envelope for invalid input", async () => { + const home = await homes.create(); + await run( + home, + RunToolTestService.use((service) => service.createTool("sample")), + ); + const result = await run( + home, + RunToolTestService.use((service) => + service.run("sample", "example", { homeDir: home, input: '{"text":123}' }), + ), + ); + expect(result.exitCode).toBe(1); + expect(result.envelope).toMatchObject({ + data: null, + errors: [ + { + code: "VALIDATION_ERROR", + message: "Invalid input.", + details: { issues: [{ path: "text", message: expect.any(String) }] }, + }, + ], + }); + }); +}); diff --git a/src/domains/tools/runtime/tool-runner-reliability.test.ts b/src/domains/tools/runtime/tool-runner-reliability.test.ts new file mode 100644 index 0000000..d273a23 --- /dev/null +++ b/src/domains/tools/runtime/tool-runner-reliability.test.ts @@ -0,0 +1,78 @@ +/** @effect-diagnostics strictEffectProvide:skip-file */ +import { afterEach, expect, test } from "vite-plus/test"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, +} from "../../../providers/paths/rig-paths"; +import { toolServicesLayer } from "./tool-services-layer"; +import { ToolRunnerService } from "./tool-runner"; + +class ReliabilityTestHome { + private readonly paths: string[] = []; + + async create(): Promise { + const path = await mkdtemp(join(tmpdir(), "rig-effect-reliability-")); + this.paths.push(path); + return path; + } + + async cleanup(): Promise { + await Promise.all( + this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), + ); + } +} + +const homes = new ReliabilityTestHome(); + +afterEach(async () => homes.cleanup()); + +test("returns an error envelope when output serialization fails", async () => { + const home = await homes.create(); + const toolDirectory = join(home, "rig", "tools", "circular-output"); + await mkdir(toolDirectory, { recursive: true }); + await writeFile( + join(toolDirectory, "index.rig.ts"), + `export default (rig) => rig.defineTool({ + name: "circular-output", + description: "Reliability test tool.", + commands: { + read: rig.defineCommand({ + description: "Return a circular result.", + input: rig.z.object({}), + output: rig.z.any(), + run: () => { + const result = {}; + result.self = result; + return result; + }, + }), + }, +}); +`, + "utf8", + ); + + const layer = toolServicesLayer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(RigPathsOptionsConfigService, { homeDir: home }), + RigPathsPlatformService.layer, + ), + ), + ); + const result = await Effect.runPromise( + ToolRunnerService.use((service) => + service.run("circular-output", "read", { homeDir: home }), + ).pipe(Effect.provide(layer)), + ); + + expect(result).toMatchObject({ + exitCode: 1, + envelope: { errors: [{ code: "INTERNAL_ERROR" }] }, + }); +}); diff --git a/src/domains/tools/runtime/tool-runner.ts b/src/domains/tools/runtime/tool-runner.ts new file mode 100644 index 0000000..fb28f96 --- /dev/null +++ b/src/domains/tools/runtime/tool-runner.ts @@ -0,0 +1,309 @@ +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer, Result } from "effect"; +import pino, { type Logger } from "pino"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigLoggerService, rigLoggerLayer } from "../../../providers/logging/rig-logger"; +import { + OutputTruncatorService, + outputTruncatorLayer, +} from "../../../providers/output/output-truncator"; +import { ToolIdentifierService, toolIdentifierLayer } from "../service/tool-identifier"; +import type { CommandDefinition } from "../types/tool-types"; +import type { RunCommandOptions, RunCommandResult } from "../types/tool-runner"; +import { ToolLoaderService, type LoadedToolCommand } from "./tool-loader"; +import { RigToolKitService } from "./tool-sdk"; +import { ToolInputParserService, toolInputParserLayer } from "../service/tool-input-parser"; +import { + ToolExecutionResourcesService, + toolExecutionResourcesLayer, +} from "./tool-execution-resources"; +import { ToolExecutionError } from "../types/tool-execution"; +import { + ToolRunnerProtocolService, + ToolRunnerValidationService, + toolRunnerProtocolLayer, +} from "../service/tool-runner-protocol"; + +type RunInSession = ( + toolName: string, + commandName: string, + runOptions?: RunCommandOptions, +) => Effect.Effect; + +export class RunInputService extends Context.Service< + RunInputService, + { + readonly read: ( + command: CommandDefinition, + options: RunCommandOptions, + ) => Effect.Effect<{ value: unknown; source: string }, RigError>; + } +>()("@rendotdev/rig/tools/execution/RunInputService", { + make: Effect.gen(function* () { + const parser = yield* ToolInputParserService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const parseJsonInput = (source: string): unknown => { + try { + return JSON.parse(source); + } catch (error) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Input JSON is invalid.", + details: { message: error instanceof Error ? error.message : String(error) }, + }); + } + }; + const read = Effect.fn("RunInputService.read")(function* ( + command: CommandDefinition, + options: RunCommandOptions, + ) { + const context = options.pipeContext ?? {}; + const args = yield* parser.interpolateArguments(context, options.args ?? []); + const sourceCount = [ + Boolean(options.input), + Boolean(options.inputFile), + args.length > 0, + ].filter(Boolean).length; + if (sourceCount > 1) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: "Use args, --input, or --input-file, not more than one.", + }); + } + if (options.inputFile) { + const source = yield* Effect.tryPromise({ + /* v8 ignore next -- the built CLI always uses Bun.file */ + try: () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? Bun.file(options.inputFile!).text() + : readFile(options.inputFile!, "utf8"), + catch: toError, + }); + return { + value: yield* Effect.try({ + try: () => parseJsonInput(source), + catch: toError, + }).pipe(Effect.flatMap((value) => parser.interpolate(context, value))), + source: `--input-file ${options.inputFile}`, + }; + } + if (options.input) { + return { + value: yield* Effect.try({ + try: () => parseJsonInput(options.input!), + catch: toError, + }).pipe(Effect.flatMap((value) => parser.interpolate(context, value))), + source: `--input '${options.input}'`, + }; + } + if (args.length > 0) { + return { + value: yield* parser.parseArguments(command.input, args), + source: yield* parser.renderSource(args), + }; + } + return { value: {}, source: "--input '{}'" }; + }); + return { read } as const; + }), +}) { + static readonly layer = Layer.effect(RunInputService, RunInputService.make); +} + +class ToolRunnerExecutionService extends Context.Service< + ToolRunnerExecutionService, + { + readonly execute: (params: { + toolName: string; + commandName: string; + tool: LoadedToolCommand["tool"]; + command: CommandDefinition; + input: unknown; + log: Logger; + runNested: RunInSession; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolRunnerExecutionService", { + make: Effect.gen(function* () { + const resources = yield* ToolExecutionResourcesService; + const toolkits = yield* RigToolKitService; + const truncator = yield* OutputTruncatorService; + const protocol = yield* ToolRunnerProtocolService; + const validation = yield* ToolRunnerValidationService; + const execute = Effect.fn("ToolRunnerExecutionService.execute")(function* (params: { + toolName: string; + commandName: string; + tool: LoadedToolCommand["tool"]; + command: CommandDefinition; + input: unknown; + log: Logger; + runNested: RunInSession; + }) { + const rig = yield* toolkits.create({ + run: (tool, command, runOptions) => params.runNested(tool, command, runOptions), + }); + const program = Effect.gen(function* () { + const { db, kv, cache, collections } = yield* resources.acquire({ + tool: params.tool, + log: params.log, + }); + const commandDatabase = db; + params.log.info("Tool command started."); + const data = yield* Effect.tryPromise({ + try: () => + Promise.resolve( + params.command.run({ + input: params.input, + env: params.tool.env, + processEnv: process.env, + cwd: process.cwd(), + db: commandDatabase, + kv, + cache, + log: params.log, + rig, + collections: collections ?? {}, + }), + ), + catch: (cause) => new ToolExecutionError({ operation: "command", cause }), + }); + const output = yield* validation.output(params.command, data); + params.log.info("Tool command finished."); + const truncated = yield* truncator + .truncate(output) + .pipe(Effect.mapError((cause) => new ToolExecutionError({ operation: "output", cause }))); + return yield* protocol.success(truncated); + }).pipe( + Effect.withSpan("ToolRunnerService.run", { + attributes: { "rig.tool": params.toolName, "rig.command": params.commandName }, + }), + Effect.scoped, + Effect.result, + ); + const result = yield* program; + return Result.isSuccess(result) + ? result.success + : yield* protocol.failure(params.log, result.failure.cause); + }); + return { execute } as const; + }), +}) { + static readonly layer = Layer.effect(ToolRunnerExecutionService, ToolRunnerExecutionService.make); +} + +export class ToolRunnerService extends Context.Service< + ToolRunnerService, + { + readonly run: ( + toolName: string, + commandName: string, + options?: RunCommandOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolRunnerService", { + make: Effect.gen(function* () { + const loader = yield* ToolLoaderService; + const runInput = yield* RunInputService; + const identifiers = yield* ToolIdentifierService; + const truncator = yield* OutputTruncatorService; + const loggers = yield* RigLoggerService; + const protocol = yield* ToolRunnerProtocolService; + const validation = yield* ToolRunnerValidationService; + const execution = yield* ToolRunnerExecutionService; + const commands = new Map(); + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const loadCommand = Effect.fn("ToolRunnerService.loadCommand")(function* ( + toolName: string, + commandName: string, + ) { + const id = yield* identifiers.commandId(toolName, commandName); + const cached = commands.get(id); + if (cached) return cached; + const loaded = yield* loader.loadCommand(toolName, commandName); + commands.set(id, loaded); + return loaded; + }); + const run: RunInSession = Effect.fn("ToolRunnerService.run")(function* ( + toolName: string, + commandName: string, + runOptions: RunCommandOptions = {}, + ) { + const log = yield* loggers + .tool(toolName, commandName) + .pipe(Effect.orElseSucceed(() => pino({ enabled: false }))); + const program = Effect.gen(function* () { + const { tool, command } = yield* loadCommand(toolName, commandName); + const input = yield* runInput.read(command, runOptions); + const inputResult = command.input.safeParse(input.value); + if (!inputResult.success) { + return yield* new RigError({ + code: "VALIDATION_ERROR", + message: "Invalid input.", + details: yield* validation.details(inputResult.error), + }); + } + if (runOptions.dryRun) { + const data = yield* protocol.dryRun({ + tool: toolName, + command: commandName, + input: inputResult.data, + inputSource: input.source, + id: yield* identifiers.commandId(toolName, commandName), + }); + const truncated = yield* truncator.truncate(data).pipe(Effect.mapError(toError)); + return yield* protocol.success(truncated); + } + return yield* execution.execute({ + toolName, + commandName, + tool, + command, + input: inputResult.data, + log, + runNested: run, + }); + }); + return yield* program.pipe( + Effect.matchEffect({ + onFailure: (error) => protocol.failure(log, error), + onSuccess: Effect.succeed, + }), + ); + }); + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(ToolRunnerService, ToolRunnerService.make); +} + +export const runInputLayer = RunInputService.layer.pipe(Layer.provide(toolInputParserLayer)); +const toolRunnerExecutionLayer = ToolRunnerExecutionService.layer.pipe( + Layer.provide( + Layer.mergeAll(toolExecutionResourcesLayer, outputTruncatorLayer, toolRunnerProtocolLayer), + ), +); +export const toolRunnerLayer = ToolRunnerService.layer.pipe( + Layer.provide( + Layer.mergeAll( + runInputLayer, + toolIdentifierLayer, + toolExecutionResourcesLayer, + rigLoggerLayer, + outputTruncatorLayer, + toolRunnerProtocolLayer, + toolRunnerExecutionLayer, + ), + ), +); diff --git a/src/domains/tools/runtime/tool-runtime-instruction-sync.ts b/src/domains/tools/runtime/tool-runtime-instruction-sync.ts new file mode 100644 index 0000000..363eac8 --- /dev/null +++ b/src/domains/tools/runtime/tool-runtime-instruction-sync.ts @@ -0,0 +1,204 @@ +import { readFile } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { + AtomicFileWriterService, + atomicFileWriterLayer, +} from "../../../providers/filesystem/atomic-file-writer"; +import { rigPathsConfigLayer, rigPathsLayer } from "../../../providers/paths/rig-paths"; +import { runtimeSupportRendererLayer } from "./runtime-support"; +import { registryLayer } from "../../registry/service/registry"; +import { ToolDiscoveryService, toolDiscoveryLayer } from "../../registry/service/tool-discovery"; +import { RigError } from "../../../providers/errors/rig-error"; +import { CurrentRigToolApiVersion } from "../config/tool-api"; + +type ToolRuntimeInstructionUpdate = { + name: string; + path: string; + changed: boolean; +}; + +export type ToolRuntimeInstructionSyncResult = { + tools: ToolRuntimeInstructionUpdate[]; +}; + +const StartMarker = "// rig:runtime-reference:start"; +const EndMarker = "// rig:runtime-reference:end"; +const ToolApiVersionMarker = "// rig:tool-api-version"; + +const RuntimeReference = `Generated by Rig. This block is refreshed automatically. + +Rig tool runtime: +- Tool API v2 uses a direct factory export: \`export default (rig: RigToolKit) => rig.defineTool({ ... });\`. +- Rig derives the tool name from its folder. The \`name\` property remains supported for v1 tools. +- \`rig.z\` is Zod. Docs: https://zod.dev/ +- \`rig.defineTool(definition)\` defines the tool. +- Define \`commands\` as \`(command) => ({ ... })\`, then define each command with the bound \`command({ description, input, output, run })\` helper. \`rig.defineCommand\` remains supported for v1 tools. +- Use \`rig.z.object({ ... })\` for input/output schemas. +- Tools run under Bun with fallback auto-install enabled, so tool files can import npm packages; add explicit package versions when reproducibility matters. Example: \`import sharp from "sharp@0.34.4";\`. +- \`rig.run({ command: "tool.command", input })\` runs another registered Rig command and returns its data. +- \`rig.$\` runs a Bun Shell tagged template command with escaped interpolations. Docs: https://bun.com/docs/runtime/shell +- \`rig.args()\` builds argv arrays with \`.raw()\`, \`.flag()\`, \`.value()\`, \`.values()\`, and \`.toArray()\`. +- \`rig.paths.home()\`, \`rig.paths.resolve(cwd, path)\`, \`rig.paths.ensureParent(path)\`, and \`rig.paths.size(path)\` cover common path work. +- Add \`env: rig.z.object({ KEY: rig.z.string() })\` to validate local secrets or settings and receive parsed values on \`context.env\`. +- Run \`rig env KEY=VALUE\` to write the tool \`.env\` file; run \`rig env remove KEY\` to remove a value. +- To add another registry for tool files, run \`rig registry create [path]\` (defaults to current directory). + +Tool logging: +- Use \`context.log.info(...)\`, \`context.log.warn(...)\`, or another Pino-style method for structured logs with a default \`tool:.\` prefix. +- Rig writes app and tool logs to \`~/rig/.logs\`, rolls files by size, and keeps logs for seven days by default. + +Tool key-value state: +- Use \`context.kv.set(key, value)\` and \`context.kv.get(key)\` for lightweight JSON state stored beside the tool entry file as \`kv.sqlite\`. + +Tool query cache: +- Use \`await context.cache.query({ queryKey, queryFn, staleTime })\` for JSON-compatible derived data stored beside the tool entry file as \`cache.sqlite\`. +- Query keys are non-empty JSON-compatible arrays. Missing and stale data wait for \`queryFn\` and return its refreshed value; fresh data returns immediately. +- Use \`context.cache.peek(queryKey)\`, \`.set(queryKey, value)\`, \`.invalidate(queryKey)\`, \`.remove(queryKey)\`, and \`.clear()\` for explicit cache control. + +Tool SQLite database: +- Add \`setupDb: (db) => { db.migrate(1, "create table", "create table ..."); }\` when a tool needs persistent state. +- Rig stores the database beside the tool entry file as \`index.sqlite\` and runs \`setupDb\` before every command. +- \`db\` is Bun SQLite with \`strict: true\`, WAL mode, and an extra \`db.migrate(version, name, sql)\` helper. Docs: https://bun.com/docs/runtime/sqlite + +Tool collections: +- Add \`collections: { name: { schema: rig.z.object({ ... }) } }\` to the tool definition to declare document stores. +- Each collection is a directory of \`.md\` files (beside the tool entry file) with validated YAML frontmatter and a markdown body. +- Access via \`context.collections.\` in command run functions. +- API: \`.create({ id, data, body })\`, \`.getEntry(id)\`, \`.update(id, { data, body })\`, \`.upsert({ id, data, body })\`, \`.remove(id)\` +- Query: \`.list({ where, sort, limit })\`, \`.search(query, { limit })\`, \`.count(where)\`, \`.getCollection(filterFn)\`, \`.clear()\` +- A hidden \`.index.sqlite\` with FTS5 is auto-managed for instant full-text search and structured queries on frontmatter fields. +- Files on disk are the source of truth; hand-edits are reconciled on next access. +- Optional \`generateId: (data) => string\` derives the filename from entry data; otherwise pass \`id\` explicitly. +- Schema-less collections (no \`schema\` key) are allowed; FTS still works, no validation on write. + +Command run context: +- \`context.input\` is already validated by the input schema. +- \`context.env\` is already validated by the tool env schema when defined. +- \`context.processEnv\` is the process environment. +- \`context.cwd\` is the current working directory. +- \`context.db\` is available when the tool defines \`setupDb\`. +- \`context.kv\` is always available for lightweight JSON key-value state. +- \`context.cache\` is always available for persistent query data with explicit foreground refreshes. +- \`context.log\` is a structured Pino logger for this tool command. +- \`context.rig\` is the toolkit (same as the factory \`rig\` arg, useful for nested tool calls). +- \`context.collections\` provides handles for each declared collection (empty object if none defined). +- Return a value that matches the output schema. +`; + +export class ToolRuntimeInstructionRendererService extends Context.Service< + ToolRuntimeInstructionRendererService, + { + readonly renderPrefix: (version?: number) => Effect.Effect; + readonly upsertPrefix: (source: string) => Effect.Effect; + } +>()("@rendotdev/rig/generated/ToolRuntimeInstructionRendererService", { + make: Effect.gen(function* () { + const escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const managedBlockPattern = (): RegExp => + new RegExp(`${escapeRegExp(StartMarker)}[\\s\\S]*?${escapeRegExp(EndMarker)}\\n*`); + const sourceVersion = (source: string): number => { + const match = /^\/\/ rig:tool-api-version (\d+)\s*$/m.exec(source); + if (!match) return 1; + const version = Number.parseInt(match[1]!, 10); + return Number.isSafeInteger(version) && version > 0 ? version : 1; + }; + const renderPrefix = Effect.fn("ToolRuntimeInstructionRendererService.renderPrefix")(function* ( + version = CurrentRigToolApiVersion, + ) { + return [ + StartMarker, + `${ToolApiVersionMarker} ${version}`, + ...RuntimeReference.trimEnd() + .split("\n") + .map((line) => (line ? `// ${line}` : "//")), + EndMarker, + ].join("\n"); + }); + const upsertPrefix = Effect.fn("ToolRuntimeInstructionRendererService.upsertPrefix")(function* ( + source: string, + ) { + const version = sourceVersion(source); + const withoutExisting = source + .replace(managedBlockPattern(), "") + .replace(/^\/\/ rig:tool-api-version \d+\s*$/gm, "") + .trimStart(); + const shebang = withoutExisting.match(/^(#!.*\n)/); + const prefix = yield* renderPrefix(version); + const body = shebang ? withoutExisting.slice(shebang[0].length).trimStart() : withoutExisting; + const next = [prefix, body.trimEnd()].filter(Boolean).join("\n\n"); + return `${shebang?.[0] ?? ""}${next}\n`; + }); + return { renderPrefix, upsertPrefix } as const; + }), +}) { + // oxfmt-ignore + static readonly layer = Layer.effect(ToolRuntimeInstructionRendererService, ToolRuntimeInstructionRendererService.make); +} + +export class ToolRuntimeInstructionSyncService extends Context.Service< + ToolRuntimeInstructionSyncService, + { readonly sync: Effect.Effect } +>()("@rendotdev/rig/generated/ToolRuntimeInstructionSyncService", { + make: Effect.gen(function* () { + const discovery = yield* ToolDiscoveryService; + const renderer = yield* ToolRuntimeInstructionRendererService; + const writer = yield* AtomicFileWriterService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const updateFile = Effect.fn("ToolRuntimeInstructionSyncService.updateFile")(function* ( + path: string, + ) { + const existing = yield* Effect.tryPromise({ + try: () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? Bun.file(path).text() + : readFile(path, "utf8"), + catch: toError, + }); + const next = yield* renderer.upsertPrefix(existing); + if (next === existing) return false; + yield* writer.write(path, next).pipe(Effect.mapError((error) => toError(error.cause))); + return true; + }); + const sync = Effect.gen(function* () { + const tools = yield* discovery.discover(); + const updates = yield* Effect.all( + tools.map((tool) => + updateFile(tool.toolPath).pipe( + Effect.map((changed) => ({ name: tool.name, path: tool.toolPath, changed })), + ), + ), + { concurrency: "unbounded" }, + ); + return { tools: updates }; + }).pipe(Effect.withSpan("ToolRuntimeInstructionSyncService.sync")); + return { sync } as const; + }), +}) { + // oxfmt-ignore + static readonly layer = Layer.effect(ToolRuntimeInstructionSyncService, ToolRuntimeInstructionSyncService.make); +} + +const runtimeRendererForToolSyncLayer = runtimeSupportRendererLayer.pipe( + Layer.provide(rigPathsConfigLayer), +); +const registryForToolSyncLayer = registryLayer.pipe( + Layer.provide(Layer.merge(atomicFileWriterLayer, runtimeRendererForToolSyncLayer)), +); +const discoveryForToolSyncLayer = toolDiscoveryLayer.pipe( + Layer.provide(Layer.merge(registryForToolSyncLayer, rigPathsLayer)), +); +export const toolRuntimeInstructionSyncLayer = ToolRuntimeInstructionSyncService.layer.pipe( + Layer.provide( + Layer.mergeAll( + ToolRuntimeInstructionRendererService.layer, + discoveryForToolSyncLayer, + atomicFileWriterLayer, + ), + ), +); diff --git a/src/domains/tools/runtime/tool-sdk.ts b/src/domains/tools/runtime/tool-sdk.ts new file mode 100644 index 0000000..6f7acbf --- /dev/null +++ b/src/domains/tools/runtime/tool-sdk.ts @@ -0,0 +1,301 @@ +import { existsSync, statSync } from "node:fs"; +import { mkdir } from "node:fs/promises"; +import { isAbsolute, resolve as resolvePathValue } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { z } from "zod"; +import { + RigPathsConfigService, + RigPathsOptionsConfigService, + RigPathsPlatformService, + RigPathsService, + type PathOptions, +} from "../../../providers/paths/rig-paths"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigShellService, rigShellLayer } from "../../../providers/process/rig-shell-service"; +import type { SuccessEnvelope } from "../types/envelope"; +import { + type NormalizedToolDefinitionInput, + type RigArgBuilder, + type RigPathHelper, + type RigRunOptions, + type RigSchema, + type RigToolKit, + type ToolCollectionDefinitions, + type ToolCommandMap, + type ToolDefinitionInput, + type ToolFactory, + type ToolModuleDefault, +} from "../types/tool-types"; + +export { z }; + +export type RigCommandRunnerDependencies = { + run( + tool: string, + command: string, + options: PathOptions & { args?: string[]; input?: string; dryRun?: boolean }, + ): Promise<{ envelope: unknown; exitCode: number }>; +}; + +export type RigCommandRunnerEffectDependencies = { + run( + tool: string, + command: string, + options: PathOptions & { args?: string[]; input?: string; dryRun?: boolean }, + ): Effect.Effect<{ envelope: unknown; exitCode: number }>; +}; + +class RigArgumentBuilderService extends Context.Service< + RigArgumentBuilderService, + { readonly create: Effect.Effect } +>()("@rendotdev/rig/tools/sdk/RigArgumentBuilderService", { + make: Effect.gen(function* () { + const create = Effect.sync(() => { + const values: string[] = []; + const builder: RigArgBuilder = { + raw: (...items) => { + values.push(...items); + return builder; + }, + flag: (name, enabled = true) => { + if (enabled) values.push(name); + return builder; + }, + value: (name, value) => { + const hasValue = value !== undefined && value !== null; + if (hasValue) values.push(name, String(value)); + return builder; + }, + values: (name, items) => { + for (const value of items ?? []) values.push(name, value); + return builder; + }, + toArray: () => [...values], + }; + return builder; + }).pipe(Effect.withSpan("RigArgumentBuilderService.create")); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(RigArgumentBuilderService, RigArgumentBuilderService.make); +} + +class RigPathHelperService extends Context.Service< + RigPathHelperService, + { + readonly home: Effect.Effect; + readonly resolvePath: (cwd: string, pathValue: string) => Effect.Effect; + readonly ensureParent: (pathValue: string) => Effect.Effect; + readonly size: (pathValue: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/sdk/RigPathHelperService", { + make: Effect.gen(function* () { + const pathValues = yield* RigPathsConfigService; + const paths = yield* RigPathsService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const home = Effect.succeed(pathValues.homeDir).pipe( + Effect.withSpan("RigPathHelperService.home"), + ); + const resolvePath = Effect.fn("RigPathHelperService.resolvePath")(function* ( + cwd: string, + pathValue: string, + ) { + const expanded = yield* paths.expandTilde(pathValue); + return isAbsolute(expanded) ? expanded : resolvePathValue(cwd, expanded); + }); + const ensureParent = Effect.fn("RigPathHelperService.ensureParent")(function* ( + pathValue: string, + ) { + const parent = yield* paths.parentDir(pathValue); + yield* Effect.tryPromise({ + try: () => mkdir(parent, { recursive: true }), + catch: toError, + }); + }); + const size = Effect.fn("RigPathHelperService.size")(function* (pathValue: string) { + return existsSync(pathValue) ? statSync(pathValue).size : 0; + }); + return { home, resolvePath, ensureParent, size } as const; + }), +}) { + static readonly layer = Layer.effect(RigPathHelperService, RigPathHelperService.make); +} + +class RigRunTargetService extends Context.Service< + RigRunTargetService, + { + readonly parse: ( + options: RigRunOptions, + ) => Effect.Effect<{ tool: string; command: string; id: string }, RigError>; + } +>()("@rendotdev/rig/tools/sdk/RigRunTargetService", { + make: Effect.gen(function* () { + const parse = Effect.fn("RigRunTargetService.parse")(function* (options: RigRunOptions) { + const id = options.tool ? `${options.tool}.${options.command}` : options.command; + const match = /^([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)$/.exec(id); + if (!match) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: `Command id must use .: ${id}`, + }); + } + return { tool: match[1]!, command: match[2]!, id }; + }); + return { parse } as const; + }), +}) { + static readonly layer = Layer.effect(RigRunTargetService, RigRunTargetService.make); +} + +export class RigToolKitService extends Context.Service< + RigToolKitService, + { + readonly create: (runner?: RigCommandRunnerEffectDependencies) => Effect.Effect; + } +>()("@rendotdev/rig/tools/sdk/RigToolKitService", { + make: Effect.gen(function* () { + const shell = yield* RigShellService; + const argumentBuilders = yield* RigArgumentBuilderService; + const pathHelpers = yield* RigPathHelperService; + const targets = yield* RigRunTargetService; + const runtimeContext = yield* Effect.context(); + const runPromise = Effect.runPromiseWith(runtimeContext); + const runSync = Effect.runSyncWith(runtimeContext); + const normalizeToolDefinition = < + Env extends RigSchema, + Collections extends ToolCollectionDefinitions, + Commands extends ToolCommandMap, + >( + definition: ToolDefinitionInput, + ): NormalizedToolDefinitionInput => + (typeof definition.commands === "function" + ? { ...definition, commands: definition.commands((command) => command) } + : definition) as NormalizedToolDefinitionInput; + const create = Effect.fn("RigToolKitService.create")(function* ( + runner?: RigCommandRunnerEffectDependencies, + ) { + const runShell = (effect: Effect.Effect): Promise => runPromise(effect); + const pathHelper: RigPathHelper = { + home: () => runSync(pathHelpers.home), + resolve: (cwd, pathValue) => runSync(pathHelpers.resolvePath(cwd, pathValue)), + ensureParent: (pathValue) => runPromise(pathHelpers.ensureParent(pathValue)), + size: (pathValue) => runSync(pathHelpers.size(pathValue)), + }; + const toolkit: RigToolKit = { + z, + defineTool: normalizeToolDefinition, + defineCommand: (definition) => definition, + run: (options: RigRunOptions): Promise => { + const target = runSync(targets.parse(options)); + if (!runner) { + return Promise.reject( + new RigError({ + code: "TOOL_RUN_ERROR", + message: `Rig tool command runner is unavailable: ${target.id}`, + }), + ); + } + return runPromise( + runner.run(target.tool, target.command, { + args: options.args, + input: options.input === undefined ? undefined : JSON.stringify(options.input), + dryRun: options.dryRun, + }), + ).then((result) => { + if (result.exitCode === 0) return (result.envelope as SuccessEnvelope).data as Result; + throw new RigError({ + code: "TOOL_RUN_ERROR", + message: `Rig tool command failed: ${target.id}`, + details: { command: target.id, envelope: result.envelope }, + }); + }); + }, + $: (strings, ...values) => + runShell(shell.template(strings, values).pipe(Effect.mapError((error) => error.cause))), + args: () => runSync(argumentBuilders.create), + paths: pathHelper, + shell: { + exec: (args, options) => + runShell(shell.exec(args, options).pipe(Effect.mapError((error) => error.cause))), + bash: (command, options) => + runShell(shell.bash(command, options).pipe(Effect.mapError((error) => error.cause))), + json: (args, options) => + runShell(shell.json(args, options).pipe(Effect.mapError((error) => error.cause))), + $: (strings, ...values) => + runShell(shell.template(strings, values).pipe(Effect.mapError((error) => error.cause))), + }, + }; + return toolkit; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(RigToolKitService, RigToolKitService.make); +} + +const rigPathHelperLayer = RigPathHelperService.layer; +export const rigToolKitLayer = RigToolKitService.layer.pipe( + Layer.provide( + Layer.mergeAll( + RigArgumentBuilderService.layer, + rigPathHelperLayer, + RigRunTargetService.layer, + rigShellLayer, + ), + ), +); + +export function createRigToolKit( + options: PathOptions = {}, + runner?: RigCommandRunnerDependencies, +): RigToolKit { + const pathOptionsLayer = Layer.succeed(RigPathsOptionsConfigService, { + homeDir: options.homeDir, + }); + const pathPlatformLayer = RigPathsPlatformService.layer; + const pathConfigLayer = RigPathsConfigService.layer.pipe( + Layer.provide(Layer.merge(pathOptionsLayer, pathPlatformLayer)), + ); + const pathLayer = RigPathsService.layer.pipe( + Layer.provide(Layer.mergeAll(pathOptionsLayer, pathPlatformLayer, pathConfigLayer)), + ); + const layer = rigToolKitLayer.pipe(Layer.provide(Layer.merge(pathConfigLayer, pathLayer))); + const effectRunner = runner + ? { + run: (tool: string, command: string, runOptions: PathOptions) => + Effect.promise(() => runner.run(tool, command, runOptions)), + } + : undefined; + return Effect.runSync( + RigToolKitService.use((service) => service.create(effectRunner)).pipe(Effect.provide(layer)), + ); +} + +export const rig = createRigToolKit(); + +export function defineTool< + Env extends RigSchema = RigSchema, + Collections extends ToolCollectionDefinitions = ToolCollectionDefinitions, + const Commands extends ToolCommandMap = ToolCommandMap, +>( + definition: ToolDefinitionInput, +): NormalizedToolDefinitionInput; +export function defineTool(factory: ToolFactory): ToolFactory; +export function defineTool(value: ToolModuleDefault): ToolModuleDefault { + return typeof value === "function" ? value : rig.defineTool(value as never); +} + +export const defineCommand = rig.defineCommand; +export const run = rig.run; +export const args = rig.args; +export const paths = rig.paths; + +export const RigTool = { + define: (value: ToolModuleDefault): ToolModuleDefault => defineTool(value as never), +}; diff --git a/src/domains/tools/runtime/tool-services-layer.ts b/src/domains/tools/runtime/tool-services-layer.ts new file mode 100644 index 0000000..e5b6f45 --- /dev/null +++ b/src/domains/tools/runtime/tool-services-layer.ts @@ -0,0 +1,135 @@ +import { Layer } from "effect"; +import { rigConfigStoreLayer } from "../../settings/service/config-store"; +import { atomicFileWriterLayer } from "../../../providers/filesystem/atomic-file-writer"; +import { rigPathsConfigLayer, rigPathsLayer } from "../../../providers/paths/rig-paths"; +import { runtimeSupportLayer, runtimeSupportRendererLayer } from "./runtime-support"; +import { registryLayer } from "../../registry/service/registry"; +import { toolDiscoveryLayer } from "../../registry/service/tool-discovery"; +import { rigLoggerLayer } from "../../../providers/logging/rig-logger"; +import { outputTruncatorLayer } from "../../../providers/output/output-truncator"; +import { rigShellLayer } from "../../../providers/process/rig-shell-service"; +import { toolIdentifierLayer } from "../service/tool-identifier"; +import { schemaRendererLayer } from "../service/schema-renderer"; +import { toolSearchLayer } from "../service/tool-search"; +import { toolExecutionResourcesLayer } from "./tool-execution-resources"; +import { toolRunnerLayer } from "./tool-runner"; +import { toolInputParserLayer } from "../service/tool-input-parser"; +import { toolDefinitionLayer } from "../service/tool-definition"; +import { toolEnvironmentLayer } from "./tool-environment"; +import { toolLoaderLayer } from "./tool-loader"; +import { toolEnvLayer } from "./tool-env"; +import { ToolFilesService } from "./tool-files"; +import { toolTypecheckLayer } from "./tool-typecheck"; +import { toolApiMigrationLayer } from "./tool-api-migration"; +import { toolFindLayer } from "./tool-find"; +import { toolHelpLayer } from "./tool-help"; +import { ToolInspectorService } from "./tool-inspector"; +import { ToolListService, toolMetadataCacheLayer } from "./tool-list"; +import { rigToolKitLayer } from "./tool-sdk"; + +const runtimeRendererLayer = runtimeSupportRendererLayer.pipe(Layer.provide(rigPathsConfigLayer)); +const configStoreLayer = rigConfigStoreLayer.pipe( + Layer.provide(Layer.merge(atomicFileWriterLayer, runtimeRendererLayer)), +); +const registryServiceLayer = registryLayer.pipe( + Layer.provide(Layer.merge(atomicFileWriterLayer, runtimeRendererLayer)), +); +const discoveryServiceLayer = toolDiscoveryLayer.pipe( + Layer.provide(Layer.merge(registryServiceLayer, rigPathsLayer)), +); +const toolkitServiceLayer = rigToolKitLayer.pipe( + Layer.provide(Layer.mergeAll(rigShellLayer, rigPathsConfigLayer, rigPathsLayer)), +); +const loaderServiceLayer = toolLoaderLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolEnvironmentLayer, + discoveryServiceLayer, + toolDefinitionLayer, + toolIdentifierLayer, + toolkitServiceLayer, + ), + ), +); +const metadataServiceLayer = toolMetadataCacheLayer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + loaderServiceLayer, + atomicFileWriterLayer, + schemaRendererLayer, + toolIdentifierLayer, + ), + ), +); +const listServiceLayer = ToolListService.layer.pipe( + Layer.provide(Layer.mergeAll(discoveryServiceLayer, metadataServiceLayer, toolIdentifierLayer)), +); +const findServiceLayer = toolFindLayer.pipe( + Layer.provide( + Layer.mergeAll( + discoveryServiceLayer, + metadataServiceLayer, + toolIdentifierLayer, + toolSearchLayer, + ), + ), +); +const inspectorServiceLayer = ToolInspectorService.layer.pipe( + Layer.provide(Layer.mergeAll(loaderServiceLayer, toolIdentifierLayer, schemaRendererLayer)), +); +const helpServiceLayer = toolHelpLayer.pipe( + Layer.provide(Layer.mergeAll(loaderServiceLayer, toolIdentifierLayer, schemaRendererLayer)), +); +const filesServiceLayer = ToolFilesService.layer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsLayer, + configStoreLayer, + discoveryServiceLayer, + atomicFileWriterLayer, + toolDefinitionLayer, + ), + ), +); +const environmentServiceLayer = toolEnvLayer.pipe( + Layer.provide(Layer.mergeAll(loaderServiceLayer, atomicFileWriterLayer, schemaRendererLayer)), +); +const typecheckServiceLayer = toolTypecheckLayer.pipe( + Layer.provide( + Layer.mergeAll( + rigPathsConfigLayer, + configStoreLayer, + discoveryServiceLayer, + toolDefinitionLayer, + ), + ), +); +const runnerServiceLayer = toolRunnerLayer.pipe( + Layer.provide( + Layer.mergeAll( + toolExecutionResourcesLayer, + loaderServiceLayer, + toolInputParserLayer, + toolIdentifierLayer, + toolkitServiceLayer, + outputTruncatorLayer, + rigLoggerLayer, + ), + ), +); +const migrationServiceLayer = toolApiMigrationLayer.pipe(Layer.provide(discoveryServiceLayer)); + +export const toolServicesLayer = Layer.mergeAll( + listServiceLayer, + findServiceLayer, + inspectorServiceLayer, + helpServiceLayer, + filesServiceLayer, + environmentServiceLayer, + typecheckServiceLayer, + runnerServiceLayer, + migrationServiceLayer, + runtimeSupportLayer, + toolIdentifierLayer, +); diff --git a/src/domains/tools/runtime/tool-typecheck.ts b/src/domains/tools/runtime/tool-typecheck.ts new file mode 100644 index 0000000..73b0dd0 --- /dev/null +++ b/src/domains/tools/runtime/tool-typecheck.ts @@ -0,0 +1,189 @@ +import { mkdir, writeFile } from "node:fs/promises"; +import { createRequire } from "node:module"; +import { dirname, join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import ts from "typescript"; +import { RigConfigStoreService } from "../../settings/service/config-store"; +import { RigPathsConfigService } from "../../../providers/paths/rig-paths"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolDiscoveryService } from "../../registry/service/tool-discovery"; +import { ToolDefinitionService } from "../service/tool-definition"; + +export type ToolTypecheckResult = { + ok: boolean; + exitCode: number; + stdout: string; + stderr: string; + checked: string[]; + tsconfigPath: string; +}; + +class ToolTypeScriptCompilerService extends Context.Service< + ToolTypeScriptCompilerService, + { + readonly run: ( + tsconfigPath: string, + ) => Effect.Effect<{ stdout: string; exitCode: number }, RigError>; + } +>()("@rendotdev/rig/tools/management/ToolTypeScriptCompilerService", { + make: Effect.gen(function* () { + const diagnosticsHost = (): ts.FormatDiagnosticsHost => ({ + getCanonicalFileName: (fileName) => fileName, + getCurrentDirectory: () => process.cwd(), + getNewLine: () => "\n", + }); + const parseHost = (): ts.ParseConfigFileHost => ({ + ...ts.sys, + onUnRecoverableConfigFileDiagnostic: (diagnostic) => { + throw new RigError({ + code: "TYPECHECK_ERROR", + message: "Unable to parse generated Rig tool tsconfig.", + details: { diagnostic: ts.formatDiagnostic(diagnostic, diagnosticsHost()) }, + }); + }, + }); + const compile = (tsconfigPath: string): { stdout: string; exitCode: number } => { + const parsedConfig = ts.getParsedCommandLineOfConfigFile(tsconfigPath, {}, parseHost()); + /* v8 ignore next */ + if (!parsedConfig) { + return { stdout: "Unable to parse generated Rig tool tsconfig.\n", exitCode: 2 }; + } + const program = ts.createProgram(parsedConfig.fileNames, parsedConfig.options); + const diagnostics = [...parsedConfig.errors, ...ts.getPreEmitDiagnostics(program)]; + if (diagnostics.length === 0) return { stdout: "", exitCode: 0 }; + return { stdout: ts.formatDiagnostics(diagnostics, diagnosticsHost()), exitCode: 2 }; + }; + const run = Effect.fn("ToolTypeScriptCompilerService.run")(function* (tsconfigPath: string) { + return yield* Effect.try({ + try: () => compile(tsconfigPath), + catch: (cause) => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }), + }); + }); + return { run } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolTypeScriptCompilerService, + ToolTypeScriptCompilerService.make, + ); +} + +class ToolTypeScriptConfigService extends Context.Service< + ToolTypeScriptConfigService, + { + readonly create: (toolPaths: string[]) => Effect.Effect, RigError>; + } +>()("@rendotdev/rig/tools/management/ToolTypeScriptConfigService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const requireFromModule = createRequire(import.meta.url); + const packagePath = (packageName: string): string => + dirname(requireFromModule.resolve(`${packageName}/package.json`)); + const create = Effect.fn("ToolTypeScriptConfigService.create")(function* (toolPaths: string[]) { + return yield* Effect.try({ + try: () => { + const bunTypesRoot = dirname(packagePath("@types/bun")); + const zodRoot = packagePath("zod"); + return { + compilerOptions: { + target: "ES2022", + module: "ESNext", + moduleResolution: "Bundler", + strict: true, + noEmit: true, + skipLibCheck: true, + allowImportingTsExtensions: true, + lib: ["ES2023", "DOM"], + jsx: "preserve", + types: ["bun"], + typeRoots: [bunTypesRoot], + paths: { zod: [join(zodRoot, "index.d.ts")] }, + }, + files: [paths.runtimeGlobalsPath, paths.runtimeTypesPath, ...toolPaths], + }; + }, + catch: (cause) => + new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }), + }); + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolTypeScriptConfigService, + ToolTypeScriptConfigService.make, + ); +} + +export class ToolTypecheckService extends Context.Service< + ToolTypecheckService, + { + readonly typecheck: (toolName?: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/management/ToolTypecheckService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const config = yield* RigConfigStoreService; + const discovery = yield* ToolDiscoveryService; + const definitions = yield* ToolDefinitionService; + const compiler = yield* ToolTypeScriptCompilerService; + const configurations = yield* ToolTypeScriptConfigService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const typecheck = Effect.fn("ToolTypecheckService.typecheck")(function* (toolName?: string) { + yield* config.ensure.pipe(Effect.mapError((error) => toError(error.cause))); + const discovered = yield* discovery.discover(); + if (toolName) yield* definitions.validateToolName(toolName); + const selected = toolName ? discovered.filter((tool) => tool.name === toolName) : discovered; + const isSelectedToolMissing = toolName !== undefined && selected.length === 0; + if (isSelectedToolMissing) { + return yield* new RigError({ + code: "TOOL_NOT_FOUND", + message: `Tool not found: ${toolName}`, + details: { available: discovered.map((tool) => tool.name) }, + }); + } + const checked = selected.map((tool) => tool.toolPath); + const tsconfig = yield* configurations.create(checked); + yield* Effect.tryPromise({ + try: async () => { + await mkdir(paths.runtimeDir, { recursive: true }); + await writeFile(paths.runtimeToolTsconfigPath, JSON.stringify(tsconfig, null, 2), "utf8"); + }, + catch: toError, + }); + const result = yield* compiler.run(paths.runtimeToolTsconfigPath); + return { + ok: result.exitCode === 0, + exitCode: result.exitCode, + stdout: result.stdout, + stderr: "", + checked, + tsconfigPath: paths.runtimeToolTsconfigPath, + }; + }); + return { typecheck } as const; + }), +}) { + static readonly layer = Layer.effect(ToolTypecheckService, ToolTypecheckService.make); +} + +const toolTypeScriptCompilerLayer = ToolTypeScriptCompilerService.layer; +const toolTypeScriptConfigLayer = ToolTypeScriptConfigService.layer; +export const toolTypecheckLayer = ToolTypecheckService.layer.pipe( + Layer.provide(Layer.merge(toolTypeScriptCompilerLayer, toolTypeScriptConfigLayer)), +); diff --git a/src/domains/tools/service/agent-instruction-documents.ts b/src/domains/tools/service/agent-instruction-documents.ts new file mode 100644 index 0000000..ebecfd5 --- /dev/null +++ b/src/domains/tools/service/agent-instruction-documents.ts @@ -0,0 +1,228 @@ +import { readFile, realpath, stat } from "node:fs/promises"; +import { Context, Effect, Layer } from "effect"; +import { AtomicFileWriterService } from "../../../providers/filesystem/atomic-file-writer"; +import { RigError } from "../../../providers/errors/rig-error"; +import { + AgentInstructionEndMarker, + AgentInstructionIgnoreMarker, + AgentInstructionStartMarker, + type AgentInstructionTarget, +} from "../types/agent-instruction"; + +class AgentInstructionDocumentStorageService extends Context.Service< + AgentInstructionDocumentStorageService, + { + readonly isDirectory: (path: string) => Effect.Effect; + readonly isFile: (path: string) => Effect.Effect; + readonly isIgnored: (path: string) => Effect.Effect; + readonly readText: (path: string) => Effect.Effect; + readonly safeRealPath: (path: string) => Effect.Effect; + readonly writeText: (path: string, content: string) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionDocumentStorageService", { + make: Effect.gen(function* () { + const writer = yield* AtomicFileWriterService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const readText = Effect.fn("AgentInstructionDocumentStorageService.readText")(function* ( + path: string, + ) { + return yield* Effect.tryPromise({ + try: () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? Bun.file(path).text() + : readFile(path, "utf8"), + catch: toError, + }); + }); + const writeText = Effect.fn("AgentInstructionDocumentStorageService.writeText")(function* ( + path: string, + content: string, + ) { + yield* writer.write(path, content).pipe(Effect.mapError((error) => toError(error.cause))); + }); + const isFile = Effect.fn("AgentInstructionDocumentStorageService.isFile")(function* ( + path: string, + ) { + return yield* Effect.tryPromise({ try: () => stat(path), catch: () => undefined }).pipe( + Effect.map((status) => status.isFile()), + Effect.orElseSucceed(() => false), + ); + }); + const isDirectory = Effect.fn("AgentInstructionDocumentStorageService.isDirectory")(function* ( + path: string, + ) { + return yield* Effect.tryPromise({ try: () => stat(path), catch: () => undefined }).pipe( + Effect.map((status) => status.isDirectory()), + Effect.orElseSucceed(() => false), + ); + }); + const isIgnored = Effect.fn("AgentInstructionDocumentStorageService.isIgnored")(function* ( + path: string, + ) { + return yield* readText(path).pipe( + Effect.map((source) => source.includes(AgentInstructionIgnoreMarker)), + Effect.orElseSucceed(() => false), + ); + }); + const safeRealPath = Effect.fn("AgentInstructionDocumentStorageService.safeRealPath")( + function* (path: string) { + return yield* Effect.tryPromise({ try: () => realpath(path), catch: () => undefined }).pipe( + Effect.orElseSucceed(() => path), + ); + }, + ); + return { isDirectory, isFile, isIgnored, readText, safeRealPath, writeText } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionDocumentStorageService, + AgentInstructionDocumentStorageService.make, + ); +} + +class AgentInstructionManagedBlockService extends Context.Service< + AgentInstructionManagedBlockService, + { + readonly remove: (target: AgentInstructionTarget) => Effect.Effect; + readonly upsert: ( + target: AgentInstructionTarget, + block: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionManagedBlockService", { + make: Effect.gen(function* () { + const documents = yield* AgentInstructionDocumentStorageService; + const escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const managedBlockPattern = (): RegExp => + new RegExp( + `${escapeRegExp(AgentInstructionStartMarker)}[\\s\\S]*?${escapeRegExp(AgentInstructionEndMarker)}`, + ); + const upsert = Effect.fn("AgentInstructionManagedBlockService.upsert")(function* ( + target: AgentInstructionTarget, + block: string, + ) { + const existing = target.existed ? yield* documents.readText(target.path) : ""; + const pattern = managedBlockPattern(); + const nextBody = pattern.test(existing) + ? existing.replace(managedBlockPattern(), block) + : [existing.trimEnd(), block].filter(Boolean).join("\n\n"); + const next = `${nextBody.trimEnd()}\n`; + if (next === existing) return false; + yield* documents.writeText(target.path, next); + return true; + }); + const remove = Effect.fn("AgentInstructionManagedBlockService.remove")(function* ( + target: AgentInstructionTarget, + ) { + if (!target.existed) return false; + const existing = yield* documents.readText(target.path); + if (!managedBlockPattern().test(existing)) return false; + const next = existing + .replace(managedBlockPattern(), "") + .replace(/\n{3,}/g, "\n\n") + .trim(); + if (next === existing.trim()) return false; + yield* documents.writeText(target.path, next ? `${next}\n` : ""); + return true; + }); + return { remove, upsert } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionManagedBlockService, + AgentInstructionManagedBlockService.make, + ); +} + +export class AgentInstructionDocumentsService extends Context.Service< + AgentInstructionDocumentsService, + { + readonly isDirectory: (path: string) => Effect.Effect; + readonly isFile: (path: string) => Effect.Effect; + readonly isIgnored: (path: string) => Effect.Effect; + readonly readText: (path: string) => Effect.Effect; + readonly safeRealPath: (path: string) => Effect.Effect; + readonly removeManagedBlock: ( + target: AgentInstructionTarget, + ) => Effect.Effect; + readonly upsertManagedBlock: ( + target: AgentInstructionTarget, + block: string, + ) => Effect.Effect; + readonly writeText: (path: string, content: string) => Effect.Effect; + } +>()("@rendotdev/rig/generated/agent/AgentInstructionDocumentsService", { + make: Effect.gen(function* () { + const storage = yield* AgentInstructionDocumentStorageService; + const blocks = yield* AgentInstructionManagedBlockService; + const isDirectory = Effect.fn("AgentInstructionDocumentsService.isDirectory")(function* ( + path: string, + ) { + return yield* storage.isDirectory(path); + }); + const isFile = Effect.fn("AgentInstructionDocumentsService.isFile")(function* (path: string) { + return yield* storage.isFile(path); + }); + const isIgnored = Effect.fn("AgentInstructionDocumentsService.isIgnored")(function* ( + path: string, + ) { + return yield* storage.isIgnored(path); + }); + const readText = Effect.fn("AgentInstructionDocumentsService.readText")(function* ( + path: string, + ) { + return yield* storage.readText(path); + }); + const safeRealPath = Effect.fn("AgentInstructionDocumentsService.safeRealPath")(function* ( + path: string, + ) { + return yield* storage.safeRealPath(path); + }); + const removeManagedBlock = Effect.fn("AgentInstructionDocumentsService.removeManagedBlock")( + function* (target: AgentInstructionTarget) { + return yield* blocks.remove(target); + }, + ); + const upsertManagedBlock = Effect.fn("AgentInstructionDocumentsService.upsertManagedBlock")( + function* (target: AgentInstructionTarget, block: string) { + return yield* blocks.upsert(target, block); + }, + ); + const writeText = Effect.fn("AgentInstructionDocumentsService.writeText")(function* ( + path: string, + content: string, + ) { + yield* storage.writeText(path, content); + }); + return { + isDirectory, + isFile, + isIgnored, + readText, + safeRealPath, + removeManagedBlock, + upsertManagedBlock, + writeText, + } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentInstructionDocumentsService, + AgentInstructionDocumentsService.make, + ); +} + +const agentInstructionManagedBlockLayer = AgentInstructionManagedBlockService.layer.pipe( + Layer.provide(AgentInstructionDocumentStorageService.layer), +); +export const agentInstructionDocumentsLayer = AgentInstructionDocumentsService.layer.pipe( + Layer.provide( + Layer.merge(AgentInstructionDocumentStorageService.layer, agentInstructionManagedBlockLayer), + ), +); diff --git a/src/domains/tools/service/agent-sync-fingerprint.ts b/src/domains/tools/service/agent-sync-fingerprint.ts new file mode 100644 index 0000000..56dc40a --- /dev/null +++ b/src/domains/tools/service/agent-sync-fingerprint.ts @@ -0,0 +1,158 @@ +import { createHash } from "node:crypto"; +import { readFile, readdir } from "node:fs/promises"; +import { join } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigAgentInstructions } from "../config/agent-instructions"; +import { RigPathsConfigService, RigPathsService } from "../../../providers/paths/rig-paths"; +import { RigToolEntryFiles } from "../../registry/types/tool-discovery"; +import type { AgentInstructionTarget } from "../types/agent-instruction"; + +class AgentSourceFingerprintService extends Context.Service< + AgentSourceFingerprintService, + { readonly source: Effect.Effect } +>()("@rendotdev/rig/generated/agent/AgentSourceFingerprintService", { + make: Effect.gen(function* () { + const pathConfig = yield* RigPathsConfigService; + const paths = yield* RigPathsService; + const directoryEntries = async (path: string): Promise => { + try { + return (await readdir(path, { withFileTypes: true })) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .toSorted(); + } catch { + return []; + } + }; + const fingerprintHash = (values: Array): string => { + const digest = createHash("sha256"); + for (const value of values) digest.update(value ?? "").update("\0"); + return digest.digest("hex"); + }; + const source = Effect.gen(function* () { + const configSource = yield* Effect.tryPromise({ + try: () => readFile(pathConfig.configPath, "utf8"), + catch: () => undefined, + }); + const config = yield* Effect.try({ + try: () => + JSON.parse(configSource) as { + baseRegistryDir?: string; + customRegistries?: string[]; + }, + catch: () => undefined, + }); + const registryPaths = yield* Effect.all([ + paths.resolve(config.baseRegistryDir || pathConfig.defaultBaseRegistryDir), + ...(config.customRegistries ?? []).map(paths.resolve), + ]); + const sources = yield* Effect.tryPromise({ + try: () => + Promise.all( + registryPaths.map(async (registryPath) => { + const entries = await directoryEntries(registryPath); + return Promise.all( + entries.flatMap((entry) => + RigToolEntryFiles.map(async (file) => { + const path = join(registryPath, entry, file); + try { + return `${path}\0${await readFile(path, "utf8")}`; + } catch { + return undefined; + } + }), + ), + ); + }), + ), + catch: () => undefined, + }); + return fingerprintHash([ + RigAgentInstructions, + configSource, + ...sources.flat(2).filter(Boolean), + ]); + }).pipe( + Effect.orElseSucceed(() => undefined), + Effect.withSpan("AgentSourceFingerprintService.source"), + ); + return { source } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentSourceFingerprintService, + AgentSourceFingerprintService.make, + ); +} + +class AgentTargetFingerprintService extends Context.Service< + AgentTargetFingerprintService, + { + readonly targets: ( + targets: AgentInstructionTarget[], + ) => Effect.Effect | undefined>; + } +>()("@rendotdev/rig/generated/agent/AgentTargetFingerprintService", { + make: Effect.gen(function* () { + const fingerprintHash = (values: Array): string => { + const digest = createHash("sha256"); + for (const value of values) digest.update(value ?? "").update("\0"); + return digest.digest("hex"); + }; + const targets = Effect.fn("AgentTargetFingerprintService.targets")(function* ( + instructionTargets: AgentInstructionTarget[], + ) { + return yield* Effect.tryPromise({ + try: async () => + Object.fromEntries( + await Promise.all( + instructionTargets.map(async (target) => [ + target.path, + fingerprintHash([await readFile(target.path, "utf8")]), + ]), + ), + ), + catch: () => undefined, + }).pipe(Effect.orElseSucceed(() => undefined)); + }); + return { targets } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentTargetFingerprintService, + AgentTargetFingerprintService.make, + ); +} + +export class AgentSyncFingerprintService extends Context.Service< + AgentSyncFingerprintService, + { + readonly source: Effect.Effect; + readonly targets: ( + targets: AgentInstructionTarget[], + ) => Effect.Effect | undefined>; + } +>()("@rendotdev/rig/generated/agent/AgentSyncFingerprintService", { + make: Effect.gen(function* () { + const sources = yield* AgentSourceFingerprintService; + const targetFingerprints = yield* AgentTargetFingerprintService; + const source = sources.source.pipe(Effect.withSpan("AgentSyncFingerprintService.source")); + const targets = Effect.fn("AgentSyncFingerprintService.targets")(function* ( + instructionTargets: AgentInstructionTarget[], + ) { + return yield* targetFingerprints.targets(instructionTargets); + }); + return { source, targets } as const; + }), +}) { + static readonly layer = Layer.effect( + AgentSyncFingerprintService, + AgentSyncFingerprintService.make, + ); +} + +export const agentSyncFingerprintLayer = AgentSyncFingerprintService.layer.pipe( + Layer.provide( + Layer.merge(AgentSourceFingerprintService.layer, AgentTargetFingerprintService.layer), + ), +); diff --git a/src/domains/tools/service/help-topics.test.ts b/src/domains/tools/service/help-topics.test.ts new file mode 100644 index 0000000..926062c --- /dev/null +++ b/src/domains/tools/service/help-topics.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vite-plus/test"; +import { Effect } from "effect"; +import { HelpTopicsService, helpTopicsLayer } from "./help-topics"; + +const expectedTopics = [ + "collections", + "kv", + "cache", + "db", + "env", + "log", + "shell", + "run", + "tool", + "args", + "paths", +]; + +describe("help topics", () => { + it("lists and renders topics", async () => { + const service = await Effect.runPromise( + HelpTopicsService.use(Effect.succeed).pipe(Effect.provide(helpTopicsLayer)), + ); + expect(await Effect.runPromise(service.list)).toEqual(expectedTopics); + expect(await Effect.runPromise(service.render("collections"))).toContain( + "# Help • Collections", + ); + expect(await Effect.runPromise(service.render("missing"))).toBeUndefined(); + expect(await Effect.runPromise(service.renderList)).toContain("Usage: `rig help `"); + expect(await Effect.runPromise(service.renderList)).toContain(" collections Collections"); + }); +}); diff --git a/src/tools/presentation/help-topics.ts b/src/domains/tools/service/help-topics.ts similarity index 72% rename from src/tools/presentation/help-topics.ts rename to src/domains/tools/service/help-topics.ts index d9db828..2bfcae7 100644 --- a/src/tools/presentation/help-topics.ts +++ b/src/domains/tools/service/help-topics.ts @@ -1,5 +1,3 @@ -import { defineSingleton } from "../../define"; - type HelpTopic = { title: string; content: string; @@ -175,74 +173,34 @@ API: }, }; -export const HelpTopicSingleton = defineSingleton({ - params: {}, - deps: {}, - isKnownTopic(params: { name: string }): boolean { - return params.name in TOPICS; - }, - listTopics(_params: {}): string[] { - return Object.keys(TOPICS); - }, - render(params: { name: string }): string | undefined { - const topic = TOPICS[params.name]; - if (!topic) return undefined; - return `# Help \u2022 ${topic.title}\n\n${topic.content}`; - }, - renderTopicList(_params: {}): string { - const lines = ["# Help Topics", "", "Usage: `rig help `", ""]; - for (const [name, topic] of Object.entries(TOPICS)) { - lines.push(` ${name.padEnd(14)} ${topic.title}`); - } - return lines.join("\n"); - }, -}); - -export type HelpTopicServiceClass = { - isKnownTopic(name: string): boolean; - listTopics(): string[]; - render(name: string): string | undefined; - renderTopicList(): string; -}; - -type HelpTopicServiceConstructor = { - new (): HelpTopicServiceClass; - readonly prototype: HelpTopicServiceClass; -}; - -const HelpTopicServiceClassAdapter = function constructHelpTopicService(): void {}; -Object.defineProperty(HelpTopicServiceClassAdapter, "name", { value: "HelpTopicServiceClass" }); -Object.defineProperties(HelpTopicServiceClassAdapter.prototype, { - isKnownTopic: { - configurable: true, - value: function isKnownTopic(name: string): boolean { - return HelpTopicSingleton.isKnownTopic({ name }); - }, - writable: true, - }, - listTopics: { - configurable: true, - value: function listTopics(): string[] { - return HelpTopicSingleton.listTopics({}); - }, - writable: true, - }, - render: { - configurable: true, - value: function render(name: string): string | undefined { - return HelpTopicSingleton.render({ name }); - }, - writable: true, - }, - renderTopicList: { - configurable: true, - value: function renderTopicList(): string { - return HelpTopicSingleton.renderTopicList({}); - }, - writable: true, - }, -}); - -export const HelpTopicServiceClass = - HelpTopicServiceClassAdapter as unknown as HelpTopicServiceConstructor; -export const helpTopicService = new HelpTopicServiceClass(); +export class HelpTopicsService extends Context.Service< + HelpTopicsService, + { + readonly list: Effect.Effect; + readonly render: (name: string) => Effect.Effect; + readonly renderList: Effect.Effect; + } +>()("@rendotdev/rig/tools/presentation/HelpTopicsService", { + make: Effect.gen(function* () { + const list = Effect.sync(() => Object.keys(TOPICS)).pipe( + Effect.withSpan("HelpTopicsService.list"), + ); + const render = Effect.fn("HelpTopicsService.render")(function* (name: string) { + const topic = TOPICS[name]; + return topic ? `# Help \u2022 ${topic.title}\n\n${topic.content}` : undefined; + }); + const renderList = Effect.sync(() => { + const lines = ["# Help Topics", "", "Usage: `rig help `", ""]; + for (const [name, topic] of Object.entries(TOPICS)) { + lines.push(` ${name.padEnd(14)} ${topic.title}`); + } + return lines.join("\n"); + }).pipe(Effect.withSpan("HelpTopicsService.renderList")); + return { list, render, renderList } as const; + }), +}) { + static readonly layer = Layer.effect(HelpTopicsService, HelpTopicsService.make); +} + +export const helpTopicsLayer = HelpTopicsService.layer; +import { Context, Effect, Layer } from "effect"; diff --git a/src/domains/tools/service/schema-renderer.test.ts b/src/domains/tools/service/schema-renderer.test.ts new file mode 100644 index 0000000..67a65db --- /dev/null +++ b/src/domains/tools/service/schema-renderer.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vite-plus/test"; +import { Effect } from "effect"; +import { z } from "zod"; +import { SchemaRendererService, schemaRendererLayer } from "./schema-renderer"; + +const injectedConverter = (schema: unknown) => ({ type: "injected", schema }); + +describe("schema rendering", () => { + it("renders injected JSON schemas and summaries", async () => { + const renderer = await Effect.runPromise( + SchemaRendererService.use(Effect.succeed).pipe(Effect.provide(schemaRendererLayer)), + ); + await expect( + Effect.runPromise(renderer.renderJson("value", injectedConverter)), + ).resolves.toEqual({ + type: "injected", + schema: "value", + }); + await expect( + Effect.runPromise(renderer.renderSummary("value", injectedConverter)), + ).resolves.toBe(JSON.stringify({ type: "injected", schema: "value" }, null, 2)); + }); + + it("reports unavailable and failed conversion", async () => { + const renderer = await Effect.runPromise( + SchemaRendererService.use(Effect.succeed).pipe(Effect.provide(schemaRendererLayer)), + ); + await expect(Effect.runPromise(renderer.renderJson({}, null))).resolves.toEqual({ + type: "unknown", + note: "JSON Schema conversion is unavailable.", + }); + await expect( + Effect.runPromise( + renderer.renderJson({}, () => { + throw new Error("failed"); + }), + ), + ).resolves.toEqual({ + type: "unknown", + note: "JSON Schema conversion failed.", + }); + }); + + it("renders production Zod schemas", async () => { + await expect( + Effect.runPromise( + SchemaRendererService.use((service) => + service.renderSummary(z.object({ title: z.string() })), + ).pipe(Effect.provide(schemaRendererLayer)), + ), + ).resolves.toContain("title"); + }); +}); diff --git a/src/domains/tools/service/schema-renderer.ts b/src/domains/tools/service/schema-renderer.ts new file mode 100644 index 0000000..0a597eb --- /dev/null +++ b/src/domains/tools/service/schema-renderer.ts @@ -0,0 +1,48 @@ +import { Context, Effect, Layer, Result } from "effect"; +import { z } from "zod"; + +export type JsonSchemaConverter = (schema: unknown) => unknown; + +export class SchemaRendererService extends Context.Service< + SchemaRendererService, + { + readonly renderJson: ( + schema: unknown, + converter?: JsonSchemaConverter | null, + ) => Effect.Effect; + readonly renderSummary: ( + schema: unknown, + converter?: JsonSchemaConverter | null, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/domain/SchemaRendererService", { + make: Effect.gen(function* () { + const defaultJsonSchemaConverter = (z as unknown as { toJSONSchema?: JsonSchemaConverter }) + .toJSONSchema; + const renderJson = Effect.fn("SchemaRendererService.renderJson")(function* ( + schema: unknown, + converter: JsonSchemaConverter | null | undefined = defaultJsonSchemaConverter, + ) { + if (typeof converter !== "function") { + return { type: "unknown", note: "JSON Schema conversion is unavailable." }; + } + const converted = yield* Effect.result( + Effect.try({ try: () => converter(schema), catch: () => undefined }), + ); + return Result.isSuccess(converted) + ? converted.success + : { type: "unknown", note: "JSON Schema conversion failed." }; + }); + const renderSummary = Effect.fn("SchemaRendererService.renderSummary")(function* ( + schema: unknown, + converter?: JsonSchemaConverter | null, + ) { + return JSON.stringify(yield* renderJson(schema, converter), null, 2); + }); + return { renderJson, renderSummary } as const; + }), +}) { + static readonly layer = Layer.effect(SchemaRendererService, SchemaRendererService.make); +} + +export const schemaRendererLayer = SchemaRendererService.layer; diff --git a/src/domains/tools/service/tool-definition.ts b/src/domains/tools/service/tool-definition.ts new file mode 100644 index 0000000..80493ab --- /dev/null +++ b/src/domains/tools/service/tool-definition.ts @@ -0,0 +1,276 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { ToolIdentifierService, toolIdentifierLayer } from "./tool-identifier"; +import type { ToolDefinition } from "../types/tool-types"; + +class ToolDefinitionFieldsService extends Context.Service< + ToolDefinitionFieldsService, + { + readonly validateSchema: ( + value: unknown, + role: string, + id: string, + ) => Effect.Effect; + readonly validateExamples: (value: unknown, path: string) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolDefinitionFieldsService", { + make: Effect.gen(function* () { + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const validateSchema = Effect.fn("ToolDefinitionFieldsService.validateSchema")(function* ( + value: unknown, + role: string, + id: string, + ) { + const isInvalidSchema = !isRecord(value) || typeof value.safeParse !== "function"; + if (isInvalidSchema) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Command ${id} needs a Zod schema for ${role}.`, + details: { expected: "rig.z.object({ ... })" }, + }); + } + return undefined; + }); + const validateExamples = Effect.fn("ToolDefinitionFieldsService.validateExamples")(function* ( + value: unknown, + path: string, + ) { + if (value === undefined) return undefined; + if (!Array.isArray(value)) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Invalid examples at ${path}.`, + details: { expected: "array" }, + }); + } + for (const [index, example] of value.entries()) { + if (!isRecord(example)) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Invalid example at ${path}[${index}].`, + details: { expected: "object" }, + }); + } + const hasInvalidTitle = typeof example.title !== "string" || example.title.length === 0; + if (hasInvalidTitle) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Invalid example title at ${path}[${index}].`, + details: { expected: "non-empty string" }, + }); + } + const hasInvalidText = typeof example.text !== "string" || example.text.length === 0; + if (hasInvalidText) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Invalid example text at ${path}[${index}].`, + details: { expected: "non-empty string" }, + }); + } + } + return undefined; + }); + return { validateSchema, validateExamples } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolDefinitionFieldsService, + ToolDefinitionFieldsService.make, + ); +} + +class ToolCommandDefinitionService extends Context.Service< + ToolCommandDefinitionService, + { + readonly validate: ( + commandName: string, + value: unknown, + toolName: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolCommandDefinitionService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const fields = yield* ToolDefinitionFieldsService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const validate = Effect.fn("ToolCommandDefinitionService.validate")(function* ( + commandName: string, + value: unknown, + toolName: string, + ) { + yield* identifiers.parseCommandName(commandName); + const id = yield* identifiers.commandId(toolName, commandName); + if (!isRecord(value)) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Invalid command ${id}.`, + details: { expected: "object" }, + }); + } + const hasMissingDescription = + typeof value.description !== "string" || value.description.length === 0; + if (hasMissingDescription) { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Command ${id} needs a description.`, + details: { expected: "non-empty string" }, + }); + } + yield* fields.validateSchema(value.input, "input", id); + yield* fields.validateSchema(value.output, "output", id); + yield* fields.validateExamples(value.examples, `${id}.examples`); + if (typeof value.run !== "function") { + return yield* new RigError({ + code: "TOOL_INVALID", + message: `Command ${id} needs a run function.`, + details: { expected: "function" }, + }); + } + return undefined; + }); + return { validate } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolCommandDefinitionService, + ToolCommandDefinitionService.make, + ); +} + +export class ToolDefinitionService extends Context.Service< + ToolDefinitionService, + { + readonly validateToolName: (name: string) => Effect.Effect; + readonly validateCommandName: (name: string) => Effect.Effect; + readonly decode: ( + value: unknown, + expectedName?: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolDefinitionService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const decoder = yield* ToolDefinitionDecoderService; + const validateToolName = Effect.fn("ToolDefinitionService.validateToolName")(function* ( + name: string, + ) { + yield* identifiers.parseToolName(name); + }); + const validateCommandName = Effect.fn("ToolDefinitionService.validateCommandName")(function* ( + name: string, + ) { + yield* identifiers.parseCommandName(name); + }); + const decode = Effect.fn("ToolDefinitionService.decode")(function* ( + value: unknown, + expectedName?: string, + ) { + return yield* decoder.decode(value, expectedName); + }); + return { validateToolName, validateCommandName, decode } as const; + }), +}) { + static readonly layer = Layer.effect(ToolDefinitionService, ToolDefinitionService.make); +} + +class ToolDefinitionDecoderService extends Context.Service< + ToolDefinitionDecoderService, + { + readonly decode: ( + value: unknown, + expectedName?: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/loading/ToolDefinitionDecoderService", { + make: Effect.gen(function* () { + const identifiers = yield* ToolIdentifierService; + const fields = yield* ToolDefinitionFieldsService; + const commands = yield* ToolCommandDefinitionService; + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const invalidTool = (message: string, details?: unknown) => + new RigError({ code: "TOOL_INVALID", message, details }); + const decode = Effect.fn("ToolDefinitionDecoderService.decode")(function* ( + value: unknown, + expectedName?: string, + ) { + if (!isRecord(value)) { + return yield* invalidTool("Tool default export must be an object."); + } + const name = + typeof value.name === "string" && value.name.length > 0 ? value.name : expectedName; + if (!name) { + return yield* invalidTool("Tool needs a name.", { + expected: "non-empty string or a discovered tool folder", + }); + } + yield* identifiers.parseToolName(name); + const doesNameMismatchFolder = expectedName !== undefined && name !== expectedName; + if (doesNameMismatchFolder) { + return yield* invalidTool( + `Tool name does not match its folder: ${name} should be ${expectedName}.`, + { expectedName, actualName: name }, + ); + } + const hasMissingDescription = + typeof value.description !== "string" || value.description.length === 0; + if (hasMissingDescription) { + return yield* invalidTool(`Tool ${name} needs a description.`, { + expected: "non-empty string", + }); + } + const hasInvalidDatabaseSetup = + value.setupDb !== undefined && typeof value.setupDb !== "function"; + if (hasInvalidDatabaseSetup) { + return yield* invalidTool(`Tool ${name} setupDb must be a function.`, { + expected: "function", + }); + } + if (value.env !== undefined) yield* fields.validateSchema(value.env, "env", name); + if (value.collections !== undefined) { + if (!isRecord(value.collections)) { + return yield* invalidTool(`Tool ${name} needs a collections object.`, { + expected: "object", + }); + } + yield* Effect.all(Object.keys(value.collections).map(identifiers.parseCollectionName)); + } + const hasNoCommands = !isRecord(value.commands) || Object.keys(value.commands).length === 0; + if (hasNoCommands) { + const message = isRecord(value.commands) + ? `Tool ${name} must define at least one command.` + : `Tool ${name} needs a commands object.`; + return yield* invalidTool( + message, + isRecord(value.commands) ? undefined : { expected: "object" }, + ); + } + const commandDefinitions = value.commands as Record; + yield* Effect.all( + Object.entries(commandDefinitions).map(([commandName, command]) => + commands.validate(commandName, command, name), + ), + ); + return (value.name === name ? value : { ...value, name }) as ToolDefinition; + }); + return { decode } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolDefinitionDecoderService, + ToolDefinitionDecoderService.make, + ); +} + +const fieldsLayer = ToolDefinitionFieldsService.layer; +const commandLayer = ToolCommandDefinitionService.layer.pipe( + Layer.provide(Layer.merge(toolIdentifierLayer, fieldsLayer)), +); +const decoderLayer = ToolDefinitionDecoderService.layer.pipe( + Layer.provide(Layer.mergeAll(toolIdentifierLayer, fieldsLayer, commandLayer)), +); +export const toolDefinitionLayer = ToolDefinitionService.layer.pipe( + Layer.provide(Layer.merge(toolIdentifierLayer, decoderLayer)), +); diff --git a/src/domains/tools/service/tool-identifier.ts b/src/domains/tools/service/tool-identifier.ts new file mode 100644 index 0000000..58682f3 --- /dev/null +++ b/src/domains/tools/service/tool-identifier.ts @@ -0,0 +1,102 @@ +import { Context, Effect, Layer, Schema } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { CollectionName, CommandName, CommandTarget, ToolName } from "../types/tool-identifier"; + +export class ToolIdentifierService extends Context.Service< + ToolIdentifierService, + { + readonly parseToolName: (value: unknown) => Effect.Effect; + readonly parseCommandName: (value: unknown) => Effect.Effect; + readonly parseCollectionName: (value: unknown) => Effect.Effect; + readonly makeCommandTarget: ( + tool: unknown, + command: unknown, + ) => Effect.Effect; + readonly parseCommandTarget: (id: unknown) => Effect.Effect; + readonly commandId: (tool: unknown, command: unknown) => Effect.Effect; + } +>()("@rendotdev/rig/tools/domain/ToolIdentifierService", { + make: Effect.gen(function* () { + const decodeName = (params: { + schema: Schema.Codec; + value: unknown; + kind: "tool" | "command" | "collection"; + }): Entity => { + try { + return Schema.decodeUnknownSync(params.schema)({ value: params.value }); + } catch { + throw new RigError({ + code: "TOOL_INVALID", + message: `Invalid ${params.kind} name: ${String(params.value)}`, + details: { expected: "letters, numbers, hyphens, or underscores" }, + }); + } + }; + const parseToolName = Effect.fn("ToolIdentifierService.parseToolName")(function* ( + value: unknown, + ) { + return yield* Effect.try({ + try: () => decodeName({ schema: ToolName, value, kind: "tool" }), + catch: (cause) => cause as RigError, + }); + }); + const parseCommandName = Effect.fn("ToolIdentifierService.parseCommandName")(function* ( + value: unknown, + ) { + return yield* Effect.try({ + try: () => decodeName({ schema: CommandName, value, kind: "command" }), + catch: (cause) => cause as RigError, + }); + }); + const parseCollectionName = Effect.fn("ToolIdentifierService.parseCollectionName")(function* ( + value: unknown, + ) { + return yield* Effect.try({ + try: () => decodeName({ schema: CollectionName, value, kind: "collection" }), + catch: (cause) => cause as RigError, + }); + }); + const makeCommandTarget = Effect.fn("ToolIdentifierService.makeCommandTarget")(function* ( + tool: unknown, + command: unknown, + ) { + const parsedTool = (yield* parseToolName(tool)).value; + const parsedCommand = (yield* parseCommandName(command)).value; + return new CommandTarget({ + tool: parsedTool, + command: parsedCommand, + id: `${parsedTool}.${parsedCommand}`, + }); + }); + const parseCommandTarget = Effect.fn("ToolIdentifierService.parseCommandTarget")(function* ( + id: unknown, + ) { + if (typeof id === "string") { + const match = /^([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)$/.exec(id); + if (match) return yield* makeCommandTarget(match[1], match[2]); + } + return yield* new RigError({ + code: "INPUT_ERROR", + message: `Command id must use .: ${String(id)}`, + }); + }); + const commandId = Effect.fn("ToolIdentifierService.commandId")(function* ( + tool: unknown, + command: unknown, + ) { + return (yield* makeCommandTarget(tool, command)).id; + }); + return { + parseToolName, + parseCommandName, + parseCollectionName, + makeCommandTarget, + parseCommandTarget, + commandId, + } as const; + }), +}) { + static readonly layer = Layer.effect(ToolIdentifierService, ToolIdentifierService.make); +} + +export const toolIdentifierLayer = ToolIdentifierService.layer; diff --git a/src/domains/tools/service/tool-input-parser.ts b/src/domains/tools/service/tool-input-parser.ts new file mode 100644 index 0000000..59ba603 --- /dev/null +++ b/src/domains/tools/service/tool-input-parser.ts @@ -0,0 +1,232 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { SchemaRendererService, schemaRendererLayer } from "./schema-renderer"; + +class ToolInputInterpolationService extends Context.Service< + ToolInputInterpolationService, + { + readonly interpolate: ( + context: Record, + value: unknown, + ) => Effect.Effect; + readonly interpolateArguments: ( + context: Record, + values: string[], + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolInputInterpolationService", { + make: Effect.gen(function* () { + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const isRecord = (value: unknown): value is Record => + Predicate.isObject(value) && !Array.isArray(value); + const lookup = (context: Record, id: string, path?: string): unknown => { + if (!(id in context)) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Pipeline reference is unknown: @${id}`, + details: { reference: `@${id}`, available: Object.keys(context) }, + }); + } + let value = context[id]; + for (const part of path?.split(".").filter(Boolean) ?? []) { + const cannotTraverseValue = !isRecord(value) && !Array.isArray(value); + if (cannotTraverseValue) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Pipeline reference cannot access: @${id}.${path}`, + details: { reference: `@${id}.${path}`, missing: part }, + }); + } + value = (value as Record)[part]; + if (value === undefined) { + throw new RigError({ + code: "INPUT_ERROR", + message: `Pipeline reference is missing: @${id}.${path}`, + details: { reference: `@${id}.${path}`, missing: part }, + }); + } + } + return value; + }; + const interpolateValue = (context: Record, value: unknown): unknown => { + if (typeof value === "string") { + const exact = value.match(/^@([A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_.-]+))?$/); + if (exact) return lookup(context, exact[1]!, exact[2]); + return value.replace(/@([A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_.-]+))?/g, (_match, id, path) => + String(lookup(context, id, path)), + ); + } + if (Array.isArray(value)) return value.map((item) => interpolateValue(context, item)); + if (!isRecord(value)) return value; + return Object.fromEntries( + Object.entries(value).map(([key, item]) => [key, interpolateValue(context, item)]), + ); + }; + const interpolate = Effect.fn("ToolInputInterpolationService.interpolate")(function* ( + context: Record, + value: unknown, + ) { + return yield* Effect.try({ try: () => interpolateValue(context, value), catch: toError }); + }); + const interpolateArguments = Effect.fn("ToolInputInterpolationService.interpolateArguments")( + function* (context: Record, values: string[]) { + return ((yield* interpolate(context, values)) as unknown[]).map(String); + }, + ); + return { interpolate, interpolateArguments } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolInputInterpolationService, + ToolInputInterpolationService.make, + ); +} + +class ToolInputArgumentService extends Context.Service< + ToolInputArgumentService, + { + readonly parse: (schema: unknown, args: string[]) => Effect.Effect; + readonly renderSource: (args: string[]) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolInputArgumentService", { + make: Effect.gen(function* () { + const schemas = yield* SchemaRendererService; + const toError = (cause: unknown): RigError => + cause instanceof RigError + ? cause + : new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + }); + const tryParseJson = (value: string): { parsed: true; value: unknown } | { parsed: false } => { + try { + return { parsed: true, value: JSON.parse(value) }; + } catch { + return { parsed: false }; + } + }; + const parseScalar = (value: string): unknown => { + const json = tryParseJson(value); + return json.parsed ? json.value : value; + }; + const parsePair = (argument: string): [string, unknown] => { + const separator = argument.indexOf("="); + const key = argument.slice(0, separator); + if (!key) { + throw new RigError({ + code: "INPUT_ERROR", + message: "Argument keys must not be empty.", + details: { arg: argument }, + }); + } + return [key, parseScalar(argument.slice(separator + 1))]; + }; + const parse = Effect.fn("ToolInputArgumentService.parse")(function* ( + schema: unknown, + args: string[], + ) { + if (args.length === 1) { + const json = tryParseJson(args[0]!); + const isParsedObject = json.parsed && Predicate.isObject(json.value); + if (isParsedObject) return json.value; + } + if (args.every((argument) => argument.includes("="))) { + return yield* Effect.try({ + try: () => Object.fromEntries(args.map(parsePair)), + catch: toError, + }); + } + const jsonSchema = yield* schemas.renderJson(schema); + const fields = + Predicate.isObject(jsonSchema) && Predicate.isObject(jsonSchema.properties) + ? Object.keys(jsonSchema.properties) + : []; + if (fields.length === 0) { + if (args.length === 1) return parseScalar(args[0]!); + return yield* new RigError({ + code: "INPUT_ERROR", + message: "This command does not declare positional input fields.", + details: { args }, + }); + } + if (args.length > fields.length) { + return yield* new RigError({ + code: "INPUT_ERROR", + message: "Too many positional arguments.", + details: { args, expectedFields: fields }, + }); + } + return Object.fromEntries( + args.map((argument, index) => [fields[index], parseScalar(argument)]), + ); + }); + const renderSource = Effect.fn("ToolInputArgumentService.renderSource")(function* ( + args: string[], + ) { + const render = (value: string): string => + /^[A-Za-z0-9_./:=@+-]+$/.test(value) ? value : `'${value.replaceAll("'", "'\\''")}'`; + return args.map(render).join(" "); + }); + return { parse, renderSource } as const; + }), +}) { + static readonly layer = Layer.effect(ToolInputArgumentService, ToolInputArgumentService.make); +} + +export class ToolInputParserService extends Context.Service< + ToolInputParserService, + { + readonly interpolate: ( + context: Record, + value: unknown, + ) => Effect.Effect; + readonly interpolateArguments: ( + context: Record, + values: string[], + ) => Effect.Effect; + readonly parseArguments: (schema: unknown, args: string[]) => Effect.Effect; + readonly renderSource: (args: string[]) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolInputParserService", { + make: Effect.gen(function* () { + const interpolation = yield* ToolInputInterpolationService; + const argumentsService = yield* ToolInputArgumentService; + const interpolate = Effect.fn("ToolInputParserService.interpolate")(function* ( + context: Record, + value: unknown, + ) { + return yield* interpolation.interpolate(context, value); + }); + const interpolateArguments = Effect.fn("ToolInputParserService.interpolateArguments")( + function* (context: Record, values: string[]) { + return yield* interpolation.interpolateArguments(context, values); + }, + ); + const parseArguments = Effect.fn("ToolInputParserService.parseArguments")(function* ( + schema: unknown, + args: string[], + ) { + return yield* argumentsService.parse(schema, args); + }); + const renderSource = Effect.fn("ToolInputParserService.renderSource")(function* ( + args: string[], + ) { + return yield* argumentsService.renderSource(args); + }); + return { interpolate, interpolateArguments, parseArguments, renderSource } as const; + }), +}) { + static readonly layer = Layer.effect(ToolInputParserService, ToolInputParserService.make); +} + +const interpolationLayer = ToolInputInterpolationService.layer; +const argumentLayer = ToolInputArgumentService.layer.pipe(Layer.provide(schemaRendererLayer)); +export const toolInputParserLayer = ToolInputParserService.layer.pipe( + Layer.provide(Layer.merge(interpolationLayer, argumentLayer)), +); diff --git a/src/domains/tools/service/tool-persistence.ts b/src/domains/tools/service/tool-persistence.ts new file mode 100644 index 0000000..6b78ad6 --- /dev/null +++ b/src/domains/tools/service/tool-persistence.ts @@ -0,0 +1,51 @@ +import { Context, Effect, Layer, Scope } from "effect"; +import { ToolCacheService, toolCacheLayer, type ManagedRigToolCache } from "../repo/tool-cache"; +import { ToolDatabaseService, toolDatabaseLayer } from "../repo/tool-database"; +import { ToolKvService, toolKvLayer, type ManagedRigToolKvStore } from "../repo/tool-kv"; +import { ToolExecutionError } from "../types/tool-execution"; +import type { LoadedTool, RigToolDatabase, RigToolLogger } from "../types/tool-types"; + +export type ToolPersistenceResourceSet = Readonly<{ + db: RigToolDatabase; + kv: ManagedRigToolKvStore; + cache: ManagedRigToolCache; +}>; + +export class ToolPersistenceService extends Context.Service< + ToolPersistenceService, + { + readonly acquire: (params: { + tool: LoadedTool; + log: RigToolLogger; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/service/ToolPersistenceService", { + make: Effect.gen(function* () { + const databases = yield* ToolDatabaseService; + const keyValues = yield* ToolKvService; + const caches = yield* ToolCacheService; + const acquire = Effect.fn("ToolPersistenceService.acquire")(function* (params: { + tool: LoadedTool; + log: RigToolLogger; + }) { + const acquiredDatabase = yield* databases + .acquire(params.tool) + .pipe(Effect.mapError((cause) => new ToolExecutionError({ operation: "database", cause }))); + const db = acquiredDatabase ?? (yield* databases.unavailable(params.tool.name)); + const kv = yield* keyValues + .acquire(params.tool) + .pipe(Effect.mapError((cause) => new ToolExecutionError({ operation: "kv", cause }))); + const cache = yield* caches + .acquire(params.tool, params.log) + .pipe(Effect.mapError((cause) => new ToolExecutionError({ operation: "cache", cause }))); + return { db, kv, cache } as const; + }); + return { acquire } as const; + }), +}) { + static readonly layer = Layer.effect(ToolPersistenceService, ToolPersistenceService.make); +} + +export const toolPersistenceLayer = ToolPersistenceService.layer.pipe( + Layer.provide(Layer.mergeAll(toolDatabaseLayer, toolKvLayer, toolCacheLayer)), +); diff --git a/src/domains/tools/service/tool-runner-protocol.ts b/src/domains/tools/service/tool-runner-protocol.ts new file mode 100644 index 0000000..feb728e --- /dev/null +++ b/src/domains/tools/service/tool-runner-protocol.ts @@ -0,0 +1,143 @@ +import { Context, Effect, Layer, Predicate } from "effect"; +import type { Logger } from "pino"; +import { + RigError, + RigErrorNormalizationService, + rigErrorNormalizationLayer, +} from "../../../providers/errors/rig-error"; +import type { CommandDefinition } from "../types/tool-types"; +import type { RunCommandResult } from "../types/tool-runner"; +import { ToolExecutionError } from "../types/tool-execution"; + +export class ToolRunnerProtocolService extends Context.Service< + ToolRunnerProtocolService, + { + readonly dryRun: (params: { + tool: string; + command: string; + input: unknown; + inputSource: string; + id: string; + }) => Effect.Effect>; + readonly failure: (log: Logger, error: unknown) => Effect.Effect; + readonly success: (data: unknown) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolRunnerProtocolService", { + make: Effect.gen(function* () { + const errors = yield* RigErrorNormalizationService; + const successEnvelope = (data: unknown) => ({ data, errors: [] as [] }); + const errorEnvelope = (error: RigError) => ({ + data: null, + errors: [{ code: error.code, message: error.message, details: error.details }], + }); + const dryRun = Effect.fn("ToolRunnerProtocolService.dryRun")(function* (params: { + tool: string; + command: string; + input: unknown; + inputSource: string; + id: string; + }) { + return { + dryRun: true, + wouldRun: false, + tool: params.tool, + command: params.command, + id: params.id, + input: params.input, + commandLine: `rig run ${params.id} ${params.inputSource}`, + }; + }); + const failure = Effect.fn("ToolRunnerProtocolService.failure")(function* ( + log: Logger, + error: unknown, + ) { + const rigError = yield* errors.normalize(error); + log.error({ err: rigError }, "Tool command failed."); + return { envelope: errorEnvelope(rigError), exitCode: 1 }; + }); + const success = Effect.fn("ToolRunnerProtocolService.success")(function* (data: unknown) { + return { envelope: successEnvelope(data), exitCode: 0 }; + }); + return { dryRun, failure, success } as const; + }), +}) { + static readonly layer = Layer.effect(ToolRunnerProtocolService, ToolRunnerProtocolService.make); +} + +export class ToolRunnerValidationService extends Context.Service< + ToolRunnerValidationService, + { + readonly details: (error: { + flatten: () => unknown; + issues?: unknown[]; + }) => Effect.Effect>; + readonly output: ( + command: CommandDefinition, + data: unknown, + ) => Effect.Effect; + } +>()("@rendotdev/rig/tools/execution/ToolRunnerValidationService", { + make: Effect.gen(function* () { + const presentIssue = (issue: unknown): Record => { + /* v8 ignore next */ + if (!Predicate.isObject(issue)) return { message: String(issue) }; + return Object.fromEntries( + Object.entries({ + /* v8 ignore next */ + path: Array.isArray(issue.path) ? issue.path.join(".") : "", + code: issue.code, + message: issue.message, + expected: issue.expected, + received: issue.received, + }).filter(([, value]) => value !== undefined), + ); + }; + const present = (error: { flatten: () => unknown; issues?: unknown[] }) => { + const flattened = error.flatten(); + return { + /* v8 ignore next -- Zod flatten always returns a record */ + ...(Predicate.isObject(flattened) ? flattened : {}), + /* v8 ignore next */ + issues: (error.issues ?? []).map(presentIssue), + }; + }; + const details = Effect.fn("ToolRunnerValidationService.details")(function* (error: { + flatten: () => unknown; + issues?: unknown[]; + }) { + return present(error); + }); + const output = Effect.fn("ToolRunnerValidationService.output")(function* ( + command: CommandDefinition, + data: unknown, + ) { + const result = yield* Effect.try({ + try: () => command.output.safeParse(data), + catch: (cause) => new ToolExecutionError({ operation: "output", cause }), + }); + if (result.success) return result.data; + return yield* new ToolExecutionError({ + operation: "output", + cause: new RigError({ + code: "OUTPUT_VALIDATION_ERROR", + message: "Command returned invalid output.", + details: present(result.error), + }), + }); + }); + return { details, output } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolRunnerValidationService, + ToolRunnerValidationService.make, + ); +} + +const protocolLayer = ToolRunnerProtocolService.layer.pipe( + Layer.provide(rigErrorNormalizationLayer), +); +export const toolRunnerProtocolLayer = Layer.merge( + protocolLayer, + ToolRunnerValidationService.layer, +); diff --git a/src/tools/domain/tool-search.test.ts b/src/domains/tools/service/tool-search.test.ts similarity index 54% rename from src/tools/domain/tool-search.test.ts rename to src/domains/tools/service/tool-search.test.ts index a6bd24c..8d50546 100644 --- a/src/tools/domain/tool-search.test.ts +++ b/src/domains/tools/service/tool-search.test.ts @@ -1,13 +1,8 @@ import { describe, expect, test } from "vite-plus/test"; -import { - ToolSearchEngineClass, - ToolSearchSingleton, - type ToolSearchDocument, - toolSearchEngine, -} from "./tool-search"; +import { Effect } from "effect"; +import { ToolSearchService, toolSearchLayer, type ToolSearchDocument } from "./tool-search"; describe("tool search", () => { - const engine = ToolSearchSingleton; const documents: ToolSearchDocument[] = [ { id: "languagetool.check-file", @@ -36,45 +31,38 @@ describe("tool search", () => { }, ]; - test("ranks typo-tolerant multi-token matches", () => { - const results = engine.search({ query: "grammer chek markdown", documents, limit: 3 }); - const adapter = new ToolSearchEngineClass(); - - expect(adapter).toBeInstanceOf(ToolSearchEngineClass); - expect(adapter.search({ query: "grammer chek markdown", documents, limit: 1 })[0]?.id).toBe( - results[0]?.id, - ); - expect(toolSearchEngine).toBeInstanceOf(ToolSearchEngineClass); - expect(ToolSearchSingleton.search({ query: "markdown", documents, limit: 1 })[0]?.id).toBe( - "languagetool.check-file", + const search = (query: string, source = documents, limit = 3) => + Effect.runPromise( + ToolSearchService.use((service) => service.search({ query, documents: source, limit })).pipe( + Effect.provide(toolSearchLayer), + ), ); + test("ranks typo-tolerant multi-token matches", async () => { + const results = await search("grammer chek markdown"); + + expect((await search("markdown", documents, 1))[0]?.id).toBe("languagetool.check-file"); + expect(results[0]?.id).toBe("languagetool.check-file"); expect(results[0]?.matches.map((match) => match.field)).toContain("command.description"); }); - test("uses names, prefixes, transpositions, and stop-word removal", () => { - expect(engine.search({ query: "inspect pdf", documents, limit: 1 })[0]?.id).toBe( - "documents.inspect-pdf", - ); - expect(engine.search({ query: "resize my imgae", documents, limit: 1 })[0]?.id).toBe( - "image.convert", - ); + test("uses names, prefixes, transpositions, and stop-word removal", async () => { + expect((await search("inspect pdf", documents, 1))[0]?.id).toBe("documents.inspect-pdf"); + expect((await search("resize my imgae", documents, 1))[0]?.id).toBe("image.convert"); }); - test("applies limits, stable ordering, and empty-query behavior", () => { + test("applies limits, stable ordering, and empty-query behavior", async () => { const tied: ToolSearchDocument[] = [ { id: "b.command", fields: [{ name: "description", value: "shared", weight: 1 }] }, { id: "a.command", fields: [{ name: "description", value: "shared", weight: 1 }] }, ]; - expect(engine.search({ query: "shared", documents: tied, limit: 1 })).toMatchObject([ - { id: "a.command" }, - ]); - expect(engine.search({ query: " ", documents, limit: 5 })).toEqual([]); + expect(await search("shared", tied, 1)).toMatchObject([{ id: "a.command" }]); + expect(await search(" ", documents, 5)).toEqual([]); }); - test("handles prefix directions, stop-word fallback, and irrelevant fields", () => { + test("handles prefix directions, stop-word fallback, and irrelevant fields", async () => { const edgeCases: ToolSearchDocument[] = [ { id: "prefixes", @@ -88,13 +76,9 @@ describe("tool search", () => { { id: "irrelevant", fields: [{ name: "value", value: "x", weight: 10 }] }, ]; - expect(engine.search({ query: "inspect", documents: edgeCases, limit: 5 })[0]?.id).toBe( - "prefixes", - ); - expect( - engine.search({ query: "inspection report", documents: edgeCases, limit: 5 })[0]?.id, - ).toBe("prefixes"); - expect(engine.search({ query: "the", documents: edgeCases, limit: 5 })).toEqual([]); - expect(engine.search({ query: "unrelated", documents: edgeCases, limit: 5 })).toEqual([]); + expect((await search("inspect", edgeCases, 5))[0]?.id).toBe("prefixes"); + expect((await search("inspection report", edgeCases, 5))[0]?.id).toBe("prefixes"); + expect(await search("the", edgeCases, 5)).toEqual([]); + expect(await search("unrelated", edgeCases, 5)).toEqual([]); }); }); diff --git a/src/domains/tools/service/tool-search.ts b/src/domains/tools/service/tool-search.ts new file mode 100644 index 0000000..47bb310 --- /dev/null +++ b/src/domains/tools/service/tool-search.ts @@ -0,0 +1,311 @@ +import { Context, Effect, Layer } from "effect"; + +type ToolSearchField = { + name: string; + value: string; + weight: number; +}; + +export type ToolSearchDocument = { + id: string; + fields: ToolSearchField[]; +}; + +type ToolSearchFieldMatch = { + field: string; + value: string; + score: number; +}; + +export type ToolSearchRankedDocument = { + id: string; + score: number; + matches: ToolSearchFieldMatch[]; +}; + +type CompiledToolSearchField = ToolSearchField & { + normalizedValue: string; + tokens: string[]; +}; + +type CompiledToolSearchDocument = { + id: string; + fields: CompiledToolSearchField[]; + tokens: string[]; +}; + +class ToolSearchTextService extends Context.Service< + ToolSearchTextService, + { + readonly normalize: (value: string) => Effect.Effect; + readonly tokens: (value: string, removeStopWords?: boolean) => Effect.Effect; + } +>()("@rendotdev/rig/tools/domain/ToolSearchTextService", { + make: Effect.gen(function* () { + const stopWords = new Set([ + "a", + "an", + "and", + "for", + "in", + "my", + "of", + "on", + "or", + "the", + "to", + "with", + ]); + const normalize = Effect.fn("ToolSearchTextService.normalize")(function* (value: string) { + return value + .normalize("NFKD") + .replace(/[\u0300-\u036f]/g, "") + .toLowerCase() + .replace(/[^a-z0-9]+/g, " ") + .trim(); + }); + const tokens = Effect.fn("ToolSearchTextService.tokens")(function* ( + value: string, + removeStopWords = false, + ) { + const values = value.split(/\s+/).filter(Boolean); + if (!removeStopWords) return [...new Set(values)]; + const meaningful = values.filter((token) => !stopWords.has(token)); + return [...new Set(meaningful.length > 0 ? meaningful : values)]; + }); + return { normalize, tokens } as const; + }), +}) { + static readonly layer = Layer.effect(ToolSearchTextService, ToolSearchTextService.make); +} + +class ToolSearchSimilarityService extends Context.Service< + ToolSearchSimilarityService, + { readonly score: (query: string, candidate: string) => Effect.Effect } +>()("@rendotdev/rig/tools/domain/ToolSearchSimilarityService", { + make: Effect.gen(function* () { + const distance = (query: string, candidate: string): number => { + const columns = candidate.length + 1; + let previousPrevious = new Uint32Array(columns); + let previous = new Uint32Array(columns); + let current = new Uint32Array(columns); + for (let column = 0; column < columns; column++) previous[column] = column; + for (let row = 1; row <= query.length; row++) { + current[0] = row; + for (let column = 1; column < columns; column++) { + const substitution = query[row - 1] === candidate[column - 1] ? 0 : 1; + current[column] = Math.min( + previous[column]! + 1, + current[column - 1]! + 1, + previous[column - 1]! + substitution, + ); + const isTransposition = + row > 1 && + column > 1 && + query[row - 1] === candidate[column - 2] && + query[row - 2] === candidate[column - 1]; + if (isTransposition) { + current[column] = Math.min( + current[column]!, + previousPrevious[column - 2]! + substitution, + ); + } + } + const recycled = previousPrevious; + previousPrevious = previous; + previous = current; + current = recycled; + } + return previous[columns - 1]!; + }; + const trigrams = (value: string): Set => + value.length < 3 + ? new Set() + : new Set( + Array.from({ length: value.length - 2 }, (_, index) => value.slice(index, index + 3)), + ); + const trigramDice = (query: string, candidate: string): number => { + const queryTrigrams = trigrams(query); + const candidateTrigrams = trigrams(candidate); + const hasNoComparableTrigrams = queryTrigrams.size === 0 || candidateTrigrams.size === 0; + if (hasNoComparableTrigrams) return 0; + let intersection = 0; + for (const value of queryTrigrams) if (candidateTrigrams.has(value)) intersection++; + return (2 * intersection) / (queryTrigrams.size + candidateTrigrams.size); + }; + const score = Effect.fn("ToolSearchSimilarityService.score")(function* ( + query: string, + candidate: string, + ) { + if (query === candidate) return 1; + const canUsePrefix = Math.min(query.length, candidate.length) >= 3; + const isPrefixMatch = + canUsePrefix && (candidate.startsWith(query) || query.startsWith(candidate)); + if (isPrefixMatch) return 0.9; + const longest = Math.max(query.length, candidate.length); + /* v8 ignore next -- empty normalized queries return before similarity scoring */ + if (longest === 0) return 0; + const minimumDistance = Math.abs(query.length - candidate.length); + const editSimilarity = + 1 - minimumDistance / longest >= 0.58 ? 1 - distance(query, candidate) / longest : 0; + const similarity = Math.max(editSimilarity, trigramDice(query, candidate)); + return similarity >= 0.58 ? similarity * 0.82 : 0; + }); + return { score } as const; + }), +}) { + static readonly layer = Layer.effect( + ToolSearchSimilarityService, + ToolSearchSimilarityService.make, + ); +} + +class ToolSearchCompilerService extends Context.Service< + ToolSearchCompilerService, + { readonly compile: (document: ToolSearchDocument) => Effect.Effect } +>()("@rendotdev/rig/tools/domain/ToolSearchCompilerService", { + make: Effect.gen(function* () { + const text = yield* ToolSearchTextService; + const compile = Effect.fn("ToolSearchCompilerService.compile")(function* ( + document: ToolSearchDocument, + ) { + const fields = yield* Effect.forEach(document.fields, (field) => + Effect.gen(function* () { + const normalizedValue = yield* text.normalize(field.value); + return { ...field, normalizedValue, tokens: yield* text.tokens(normalizedValue) }; + }), + ); + return { + id: document.id, + fields, + tokens: [...new Set(fields.flatMap((field) => field.tokens))], + }; + }); + return { compile } as const; + }), +}) { + static readonly layer = Layer.effect(ToolSearchCompilerService, ToolSearchCompilerService.make); +} + +class ToolSearchRankerService extends Context.Service< + ToolSearchRankerService, + { + readonly rank: (params: { + document: CompiledToolSearchDocument; + normalizedQuery: string; + queryTokens: string[]; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/domain/ToolSearchRankerService", { + make: Effect.gen(function* () { + const similarity = yield* ToolSearchSimilarityService; + const roundScore = (value: number): number => Math.round(value * 1000) / 1000; + const match = Effect.fn("ToolSearchRankerService.match")(function* (params: { + field: CompiledToolSearchField; + normalizedQuery: string; + queryTokens: string[]; + }) { + if (!params.field.normalizedValue) return undefined; + const tokenScores = yield* Effect.forEach(params.queryTokens, (query) => + Effect.all(params.field.tokens.map((candidate) => similarity.score(query, candidate))).pipe( + Effect.map((scores) => Math.max(0, ...scores)), + ), + ); + const matchedTokens = tokenScores.filter((score) => score > 0); + const phraseBonus = params.field.normalizedValue.includes(params.normalizedQuery) ? 0.5 : 0; + const hasNoMatch = matchedTokens.length === 0 && phraseBonus === 0; + if (hasNoMatch) return undefined; + const coverage = matchedTokens.length / params.queryTokens.length; + const average = + tokenScores.reduce((total, score) => total + score, 0) / params.queryTokens.length; + const score = params.field.weight * (average + coverage * 0.35 + phraseBonus); + if (score < 0.5) return undefined; + return { field: params.field.name, value: params.field.value, score: roundScore(score) }; + }); + const rank = Effect.fn("ToolSearchRankerService.rank")(function* (params: { + document: CompiledToolSearchDocument; + normalizedQuery: string; + queryTokens: string[]; + }) { + const rawMatches = (yield* Effect.forEach(params.document.fields, (field) => + match({ field, normalizedQuery: params.normalizedQuery, queryTokens: params.queryTokens }), + )) + .filter((value): value is ToolSearchFieldMatch => value !== undefined) + .toSorted((left, right) => right.score - left.score); + const matches = [ + ...new Map(rawMatches.map((value) => [value.field, value])).values(), + ].toSorted((left, right) => right.score - left.score); + if (matches.length === 0) return undefined; + const covered = yield* Effect.forEach(params.queryTokens, (query) => + Effect.forEach(params.document.tokens, (candidate) => + similarity.score(query, candidate), + ).pipe(Effect.map((scores) => scores.some((score) => score > 0))), + ); + const coverage = covered.filter(Boolean).length / params.queryTokens.length; + const multiplier = 0.2 + coverage ** 2 * 0.8; + return { + id: params.document.id, + score: roundScore(matches.reduce((total, value) => total + value.score, 0) * multiplier), + matches: matches.slice(0, 3), + }; + }); + return { rank } as const; + }), +}) { + static readonly layer = Layer.effect(ToolSearchRankerService, ToolSearchRankerService.make); +} + +export class ToolSearchService extends Context.Service< + ToolSearchService, + { + readonly search: (params: { + query: string; + documents: ToolSearchDocument[]; + limit: number; + }) => Effect.Effect; + } +>()("@rendotdev/rig/tools/domain/ToolSearchService", { + make: Effect.gen(function* () { + const text = yield* ToolSearchTextService; + const compiler = yield* ToolSearchCompilerService; + const ranker = yield* ToolSearchRankerService; + const search = Effect.fn("ToolSearchService.search")(function* (params: { + query: string; + documents: ToolSearchDocument[]; + limit: number; + }) { + const normalizedQuery = yield* text.normalize(params.query); + const queryTokens = yield* text.tokens(normalizedQuery, true); + const hasNoSearchTerms = !normalizedQuery || queryTokens.length === 0; + if (hasNoSearchTerms) return []; + const ranked = yield* Effect.forEach(params.documents, (document) => + compiler + .compile(document) + .pipe( + Effect.flatMap((compiled) => + ranker.rank({ document: compiled, normalizedQuery, queryTokens }), + ), + ), + ); + return ranked + .filter((value): value is ToolSearchRankedDocument => value !== undefined) + .toSorted((left, right) => right.score - left.score || left.id.localeCompare(right.id)) + .slice(0, params.limit); + }); + return { search } as const; + }), +}) { + static readonly layer = Layer.effect(ToolSearchService, ToolSearchService.make); +} + +const toolSearchCompilerLayer = ToolSearchCompilerService.layer.pipe( + Layer.provide(ToolSearchTextService.layer), +); +const toolSearchRankerLayer = ToolSearchRankerService.layer.pipe( + Layer.provide(ToolSearchSimilarityService.layer), +); +export const toolSearchLayer = ToolSearchService.layer.pipe( + Layer.provide( + Layer.mergeAll(ToolSearchTextService.layer, toolSearchCompilerLayer, toolSearchRankerLayer), + ), +); diff --git a/src/domains/tools/types/agent-instruction.ts b/src/domains/tools/types/agent-instruction.ts new file mode 100644 index 0000000..2329ee1 --- /dev/null +++ b/src/domains/tools/types/agent-instruction.ts @@ -0,0 +1,32 @@ +export type AgentInstructionTarget = { + path: string; + existed: boolean; + scope: "all" | "visible"; +}; + +type AgentInstructionUpdate = AgentInstructionTarget & { + changed: boolean; +}; + +export type AgentInstructionSyncResult = { + skipped: boolean; + targets: AgentInstructionUpdate[]; +}; + +export const AgentInstructionStartMarker = ""; +export const AgentInstructionEndMarker = ""; +export const AgentInstructionIgnoreMarker = ""; + +export const AgentInstructionSyncLocations = { + projectFiles: ["AGENTS.md", "CLAUDE.md"], + projectClaudeDirectories: [".claude"], + homeFiles: [ + [".agents", "AGENTS.md"], + [".pi", "agent", "AGENTS.md"], + ], + homeClaudeDirectories: [[".claude"]], + openCodeConfigFiles: [ + [".config", "opencode", "opencode.json"], + [".opencode", "opencode.json"], + ], +} as const; diff --git a/src/domains/tools/types/envelope.ts b/src/domains/tools/types/envelope.ts new file mode 100644 index 0000000..209c1e2 --- /dev/null +++ b/src/domains/tools/types/envelope.ts @@ -0,0 +1,4 @@ +export type SuccessEnvelope = { + data: unknown; + errors: []; +}; diff --git a/src/tools/globals.d.ts b/src/domains/tools/types/globals.d.ts similarity index 91% rename from src/tools/globals.d.ts rename to src/domains/tools/types/globals.d.ts index 624b437..d030bcc 100644 --- a/src/tools/globals.d.ts +++ b/src/domains/tools/types/globals.d.ts @@ -1,4 +1,4 @@ -import type { RigToolKit as RigToolKitType, ToolFactory } from "./types"; +import type { RigToolKit as RigToolKitType, ToolFactory } from "./tool-types"; declare global { type RigToolFactory = ToolFactory; diff --git a/src/domains/tools/types/tool-execution.ts b/src/domains/tools/types/tool-execution.ts new file mode 100644 index 0000000..283e00e --- /dev/null +++ b/src/domains/tools/types/tool-execution.ts @@ -0,0 +1,20 @@ +import { Schema } from "effect"; + +export const ToolExecutionOperation = Schema.Literals([ + "database", + "kv", + "cache", + "collections", + "command", + "output", +]); + +export type ToolExecutionOperation = typeof ToolExecutionOperation.Type; + +export class ToolExecutionError extends Schema.TaggedErrorClass()( + "ToolExecutionError", + { + operation: ToolExecutionOperation, + cause: Schema.Defect(), + }, +) {} diff --git a/src/domains/tools/types/tool-identifier.ts b/src/domains/tools/types/tool-identifier.ts new file mode 100644 index 0000000..d56bbb2 --- /dev/null +++ b/src/domains/tools/types/tool-identifier.ts @@ -0,0 +1,21 @@ +import { Schema } from "effect"; + +const RoutedName = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]+$/)); + +export class ToolName extends Schema.Class("ToolName")({ + value: RoutedName, +}) {} + +export class CommandName extends Schema.Class("CommandName")({ + value: RoutedName, +}) {} + +export class CollectionName extends Schema.Class("CollectionName")({ + value: RoutedName, +}) {} + +export class CommandTarget extends Schema.Class("CommandTarget")({ + tool: RoutedName, + command: RoutedName, + id: Schema.String, +}) {} diff --git a/src/domains/tools/types/tool-runner.ts b/src/domains/tools/types/tool-runner.ts new file mode 100644 index 0000000..2472348 --- /dev/null +++ b/src/domains/tools/types/tool-runner.ts @@ -0,0 +1,13 @@ +export type RunCommandOptions = Readonly<{ + homeDir?: string; + input?: string; + inputFile?: string; + args?: string[]; + dryRun?: boolean; + pipeContext?: Record; +}>; + +export type RunCommandResult = Readonly<{ + envelope: unknown; + exitCode: number; +}>; diff --git a/src/tools/domain/tool-types.ts b/src/domains/tools/types/tool-types.ts similarity index 79% rename from src/tools/domain/tool-types.ts rename to src/domains/tools/types/tool-types.ts index 005faad..156b044 100644 --- a/src/tools/domain/tool-types.ts +++ b/src/domains/tools/types/tool-types.ts @@ -1,26 +1,27 @@ import type { Database } from "bun:sqlite"; import type { z } from "zod"; -import { defineSingleton } from "../../define"; -import type { CollectionDefinition, CollectionHandle } from "../collection"; -import { CommandTargetSingleton } from "../identifiers"; +import type { + CollectionDefinition, + CollectionHandle, +} from "../../collections/types/tool-collection"; -export type MaybePromise = T | Promise; +type MaybePromise = T | Promise; -export type ShellOptions = { +type ShellOptions = { cwd?: string; env?: Record; timeoutMs?: number; maxOutputBytes?: number; }; -export type ShellResult = { +type ShellResult = { command: string[]; stdout: string; stderr: string; exitCode: number; }; -export type RigShell = { +type RigShell = { $(strings: TemplateStringsArray, ...values: unknown[]): Promise; exec(args: string[], options?: ShellOptions): Promise; bash(command: string, options?: ShellOptions): Promise; @@ -82,7 +83,7 @@ export type RigToolCache = { clear(): void; }; -export type RigLogMethod = { +type RigLogMethod = { (message: string): void; (bindings: Record, message?: string): void; (error: Error, message?: string): void; @@ -98,7 +99,7 @@ export type RigToolLogger = { child(bindings: Record): RigToolLogger; }; -export type ToolRunContext> = { +type ToolRunContext> = { input: Input; env: Env; processEnv: NodeJS.ProcessEnv; @@ -133,22 +134,19 @@ export type CommandDefinition< export type ToolCollectionDefinitions = Record; -export type ToolCollectionHandles = { +type ToolCollectionHandles = { [Name in keyof Collections]: Collections[Name] extends CollectionDefinition ? CollectionHandle> : CollectionHandle; }; -export type ToolCommandBuilder = < - Input extends RigSchema, - Output extends RigSchema, ->( +type ToolCommandBuilder = ( definition: CommandDefinition, ) => CommandDefinition; export type ToolCommandMap = Record>; -export type ToolCommandsFactory = ( +type ToolCommandsFactory = ( command: ToolCommandBuilder, ) => Commands; @@ -214,32 +212,3 @@ export type LoadedTool = { env: unknown; definition: ToolDefinition; }; - -export const CommandIdsSingleton = defineSingleton({ - params: {}, - deps: { CommandTargetSingleton }, - from(params: { tool: string; command: string }) { - return this.deps.CommandTargetSingleton.from(params).id; - }, -}); - -export type CommandIdsClass = { - from(tool: string, command: string): string; -}; - -type CommandIdsConstructor = { - new (): CommandIdsClass; - readonly prototype: CommandIdsClass; -}; - -export const CommandIdsClass = function () {} as unknown as CommandIdsConstructor; - -Object.defineProperty(CommandIdsClass.prototype, "from", { - configurable: true, - value: function from(tool: string, command: string) { - return CommandIdsSingleton.from({ tool, command }); - }, - writable: true, -}); - -export const commandIds = new CommandIdsClass(); diff --git a/src/domains/updates/config/dev-link-config.ts b/src/domains/updates/config/dev-link-config.ts new file mode 100644 index 0000000..6c73578 --- /dev/null +++ b/src/domains/updates/config/dev-link-config.ts @@ -0,0 +1,22 @@ +import { Context, Effect, Layer } from "effect"; +import type { DevLinkConfig } from "../types/dev-link"; + +export class DevLinkConfigService extends Context.Service< + DevLinkConfigService, + { + readonly get: Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkConfigService", { + make: Effect.gen(function* () { + const get = Effect.succeed({ + homeDir: "", + repoRoot: process.cwd(), + platform: process.platform, + pathEnvironment: process.env.PATH ?? "", + pathDelimiter: process.platform === "win32" ? ";" : ":", + } as const).pipe(Effect.withSpan("DevLinkConfigService.get")); + return { get } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkConfigService, DevLinkConfigService.make); +} diff --git a/src/domains/updates/config/npm-update-check-config.ts b/src/domains/updates/config/npm-update-check-config.ts new file mode 100644 index 0000000..147d04c --- /dev/null +++ b/src/domains/updates/config/npm-update-check-config.ts @@ -0,0 +1,24 @@ +import { Context, Effect, Layer } from "effect"; +import type { NpmUpdateCheckConfig } from "../types/updates"; + +export class NpmUpdateCheckConfigService extends Context.Service< + NpmUpdateCheckConfigService, + { + readonly get: Effect.Effect; + } +>()("@rendotdev/rig/updates/NpmUpdateCheckConfigService", { + make: Effect.gen(function* () { + const get = Effect.succeed({ + updateCheckCachePath: "", + cacheTtlMs: 24 * 60 * 60 * 1000, + timeoutMs: 750, + packageName: "@rendotdev/rig", + } as const).pipe(Effect.withSpan("NpmUpdateCheckConfigService.get")); + return { get } as const; + }), +}) { + static readonly layer = Layer.effect( + NpmUpdateCheckConfigService, + NpmUpdateCheckConfigService.make, + ); +} diff --git a/src/domains/updates/config/rig-updater-config.ts b/src/domains/updates/config/rig-updater-config.ts new file mode 100644 index 0000000..585730f --- /dev/null +++ b/src/domains/updates/config/rig-updater-config.ts @@ -0,0 +1,11 @@ +import { Context, Effect, Layer } from "effect"; +import type { RigUpdaterParams } from "../types/updates"; + +export class RigUpdaterConfigService extends Context.Service< + RigUpdaterConfigService, + { readonly params: RigUpdaterParams } +>()("@rendotdev/rig/updates/RigUpdaterConfigService", { + make: Effect.succeed({ params: { packageRoot: "", currentVersion: "" } }), +}) { + static readonly layer = Layer.effect(RigUpdaterConfigService, RigUpdaterConfigService.make); +} diff --git a/src/domains/updates/repo/dev-link-repository.ts b/src/domains/updates/repo/dev-link-repository.ts new file mode 100644 index 0000000..2be036d --- /dev/null +++ b/src/domains/updates/repo/dev-link-repository.ts @@ -0,0 +1,88 @@ +import { existsSync } from "node:fs"; +import { chmod as chmodFile, lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises"; +import { join as joinPath, resolve as resolvePath } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; + +export class DevLinkPlatformService extends Context.Service< + DevLinkPlatformService, + { + readonly exists: (path: string) => Effect.Effect; + readonly chmod: (path: string, mode: number) => Effect.Effect; + readonly isFile: (path: string) => Effect.Effect; + readonly makeDirectory: (path: string) => Effect.Effect; + readonly readText: (path: string) => Effect.Effect; + readonly remove: (path: string) => Effect.Effect; + readonly writeText: (path: string, content: string) => Effect.Effect; + readonly join: (parts: string[]) => Effect.Effect; + readonly resolve: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkPlatformService", { + make: Effect.gen(function* () { + const asError = (cause: unknown) => + new RigError({ code: "DEV_LINK_ERROR", message: String(cause), details: cause }); + const exists = Effect.fn("DevLinkPlatformService.exists")(function* (path: string) { + return existsSync(path); + }); + const chmod = Effect.fn("DevLinkPlatformService.chmod")(function* (path: string, mode: number) { + yield* Effect.tryPromise({ try: () => chmodFile(path, mode), catch: asError }); + }); + const isFile = Effect.fn("DevLinkPlatformService.isFile")(function* (path: string) { + return yield* Effect.tryPromise({ try: () => lstat(path), catch: asError }).pipe( + Effect.map((status) => status.isFile()), + ); + }); + const makeDirectory = Effect.fn("DevLinkPlatformService.makeDirectory")(function* ( + path: string, + ) { + yield* Effect.tryPromise({ try: () => mkdir(path, { recursive: true }), catch: asError }); + }); + const readText = Effect.fn("DevLinkPlatformService.readText")(function* (path: string) { + return yield* Effect.tryPromise({ + try: async () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(path).text() + : await readFile(path, "utf8"), + catch: asError, + }); + }); + const remove = Effect.fn("DevLinkPlatformService.remove")(function* (path: string) { + yield* Effect.tryPromise({ try: () => rm(path, { force: true }), catch: asError }); + }); + const writeText = Effect.fn("DevLinkPlatformService.writeText")(function* ( + path: string, + content: string, + ) { + yield* Effect.tryPromise({ + try: async () => { + const canWriteWithBun = typeof Bun !== "undefined" && typeof Bun.write === "function"; + if (canWriteWithBun) { + await Bun.write(path, content); + } else await writeFile(path, content, "utf8"); + }, + catch: asError, + }); + }); + const join = Effect.fn("DevLinkPlatformService.join")(function* (parts: string[]) { + return joinPath(...parts); + }); + const resolve = Effect.fn("DevLinkPlatformService.resolve")(function* (path: string) { + return resolvePath(path); + }); + return { + exists, + chmod, + isFile, + makeDirectory, + readText, + remove, + writeText, + join, + resolve, + } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkPlatformService, DevLinkPlatformService.make); +} + +export const devLinkRepositoryLayer = DevLinkPlatformService.layer; diff --git a/src/domains/updates/repo/npm-registry.ts b/src/domains/updates/repo/npm-registry.ts new file mode 100644 index 0000000..619d893 --- /dev/null +++ b/src/domains/updates/repo/npm-registry.ts @@ -0,0 +1,81 @@ +import { Context, Effect, Layer, Schema } from "effect"; +import { NpmRegistryError, NpmRegistryResponse, NpmRegistryResponseError } from "../types/updates"; + +export class NpmRegistryPlatformService extends Context.Service< + NpmRegistryPlatformService, + { + readonly fetchJson: ( + url: string, + ) => Effect.Effect< + { readonly ok: boolean; readonly status: number; readonly data: unknown }, + NpmRegistryError + >; + } +>()("@rendotdev/rig/updates/NpmRegistryPlatformService", { + make: Effect.gen(function* () { + const fetchJson = Effect.fn("NpmRegistryPlatformService.fetchJson")(function* (url: string) { + return yield* Effect.tryPromise({ + try: async (signal) => { + const response = await fetch(url, { signal }); + const data = response.ok ? await response.json() : undefined; + return { ok: response.ok, status: response.status, data } as const; + }, + catch: (cause) => new NpmRegistryError({ cause }), + }); + }); + return { fetchJson } as const; + }), +}) { + static readonly layer = Layer.effect(NpmRegistryPlatformService, NpmRegistryPlatformService.make); +} + +export class NpmRegistryService extends Context.Service< + NpmRegistryService, + { + readonly latestVersion: ( + packageName: string, + timeoutMs: number, + ) => Effect.Effect; + } +>()("@rendotdev/rig/updates/NpmRegistryService", { + make: Effect.gen(function* () { + const platform = yield* NpmRegistryPlatformService; + const latestVersion = Effect.fn("NpmRegistryService.latestVersion")(function* ( + packageName: string, + timeoutMs: number, + ) { + const response = yield* platform + .fetchJson(`https://registry.npmjs.org/${encodeURIComponent(packageName)}/latest`) + .pipe( + Effect.timeout(timeoutMs), + Effect.mapError((cause) => + cause instanceof NpmRegistryError ? cause : new NpmRegistryError({ cause }), + ), + ); + if (!response.ok) { + const isTransientResponse = + response.status === 408 || + response.status === 425 || + response.status === 429 || + response.status >= 500; + if (isTransientResponse) { + return yield* new NpmRegistryError({ + cause: new NpmRegistryResponseError({ status: response.status }), + }); + } + return undefined; + } + const value = yield* Schema.decodeUnknownEffect(NpmRegistryResponse)(response.data).pipe( + Effect.mapError((cause) => new NpmRegistryError({ cause })), + ); + return value.version; + }); + return { latestVersion } as const; + }), +}) { + static readonly layer = Layer.effect(NpmRegistryService, NpmRegistryService.make); +} + +export const npmRegistryLayer = NpmRegistryService.layer.pipe( + Layer.provide(NpmRegistryPlatformService.layer), +); diff --git a/src/domains/updates/repo/rig-updater-repository.ts b/src/domains/updates/repo/rig-updater-repository.ts new file mode 100644 index 0000000..ee0d597 --- /dev/null +++ b/src/domains/updates/repo/rig-updater-repository.ts @@ -0,0 +1,115 @@ +import { existsSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { + basename as pathBasename, + dirname as pathDirname, + join as pathJoin, + resolve as pathResolve, +} from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; +import { ManagedProcessError } from "../../../providers/process/managed-process"; +import { RigShellService, rigShellLayer } from "../../../providers/process/rig-shell-service"; +import { RigPackageManifest, RigUpdateError, type RigUpdateStep } from "../types/updates"; + +export class RigUpdateCommandRunnerService extends Context.Service< + RigUpdateCommandRunnerService, + { + readonly run: (step: RigUpdateStep) => Effect.Effect; + readonly read: (step: RigUpdateStep) => Effect.Effect; + } +>()("@rendotdev/rig/updates/RigUpdateCommandRunnerService", { + make: Effect.gen(function* () { + const shell = yield* RigShellService; + const execute = Effect.fn("RigUpdateCommandRunnerService.execute")(function* ( + step: RigUpdateStep, + ) { + const result = yield* shell.exec([step.command, ...step.args], { env: step.env }).pipe( + Effect.mapError( + (error) => + new RigUpdateError({ + operation: "command", + cause: error.cause instanceof ManagedProcessError ? error.cause.cause : error.cause, + }), + ), + ); + if (result.exitCode !== 0) { + const detail = `${result.stdout}${result.stderr}`.trim(); + return yield* new RigUpdateError({ + operation: "command", + cause: new Error( + `${step.command} exited with ${result.signal ?? (result.exitCodeKnown ? `code ${result.exitCode}` : "code unknown")}.${detail ? `\n${detail}` : ""}`, + ), + }); + } + return `${result.stdout}${result.stderr}`; + }); + const run = Effect.fn("RigUpdateCommandRunnerService.run")(function* (step: RigUpdateStep) { + return yield* execute(step); + }); + const read = Effect.fn("RigUpdateCommandRunnerService.read")(function* (step: RigUpdateStep) { + return yield* execute(step); + }); + return { run, read } as const; + }), +}) { + static readonly layer = Layer.effect( + RigUpdateCommandRunnerService, + RigUpdateCommandRunnerService.make, + ); +} + +export const rigUpdateCommandRunnerLayer = RigUpdateCommandRunnerService.layer.pipe( + Layer.provide(rigShellLayer), +); + +export class RigUpdaterPlatformService extends Context.Service< + RigUpdaterPlatformService, + { + readonly executableExists: (path: string) => Effect.Effect; + readonly basename: (path: string) => Effect.Effect; + readonly dirname: (path: string) => Effect.Effect; + readonly join: (parts: string[]) => Effect.Effect; + readonly resolve: (path: string) => Effect.Effect; + readonly readPackageVersion: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/updates/RigUpdaterPlatformService", { + make: Effect.gen(function* () { + const executableExists = Effect.fn("RigUpdaterPlatformService.executableExists")(function* ( + path: string, + ) { + return existsSync(path); + }); + const basename = Effect.fn("RigUpdaterPlatformService.basename")(function* (path: string) { + return pathBasename(path); + }); + const dirname = Effect.fn("RigUpdaterPlatformService.dirname")(function* (path: string) { + return pathDirname(path); + }); + const join = Effect.fn("RigUpdaterPlatformService.join")(function* (parts: string[]) { + return pathJoin(...parts); + }); + const resolve = Effect.fn("RigUpdaterPlatformService.resolve")(function* (path: string) { + return pathResolve(path); + }); + const readPackageVersion = Effect.fn("RigUpdaterPlatformService.readPackageVersion")(function* ( + path: string, + ) { + const value = yield* Effect.tryPromise({ + try: async () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(path).json() + : JSON.parse(await readFile(path, "utf8")), + catch: (cause) => new RigUpdateError({ operation: "plan", cause }), + }); + const manifest = yield* Schema.decodeUnknownEffect(RigPackageManifest)(value).pipe( + Effect.mapError((cause) => new RigUpdateError({ operation: "plan", cause })), + ); + return manifest.version; + }); + return { executableExists, basename, dirname, join, resolve, readPackageVersion } as const; + }), +}) { + static readonly layer = Layer.effect(RigUpdaterPlatformService, RigUpdaterPlatformService.make); +} + +export const rigUpdaterRepositoryLayer = RigUpdaterPlatformService.layer; diff --git a/src/domains/updates/repo/update-cache.ts b/src/domains/updates/repo/update-cache.ts new file mode 100644 index 0000000..762fdfb --- /dev/null +++ b/src/domains/updates/repo/update-cache.ts @@ -0,0 +1,55 @@ +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { dirname } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; +import { + NpmUpdateCheckError, + UpdateCheckCache, + type UpdateCheckCache as UpdateCheckCacheValue, +} from "../types/updates"; + +export class NpmUpdateCacheService extends Context.Service< + NpmUpdateCacheService, + { + readonly read: (path: string) => Effect.Effect; + readonly write: ( + path: string, + cache: UpdateCheckCacheValue, + ) => Effect.Effect; + } +>()("@rendotdev/rig/updates/NpmUpdateCacheService", { + make: Effect.gen(function* () { + const read = Effect.fn("NpmUpdateCacheService.read")(function* (path: string) { + const value = yield* Effect.tryPromise({ + try: async () => + typeof Bun !== "undefined" && typeof Bun.file === "function" + ? await Bun.file(path).json() + : JSON.parse(await readFile(path, "utf8")), + catch: (cause) => new NpmUpdateCheckError({ cause }), + }); + return yield* Schema.decodeUnknownEffect(UpdateCheckCache)(value).pipe( + Effect.mapError((cause) => new NpmUpdateCheckError({ cause })), + ); + }); + const write = Effect.fn("NpmUpdateCacheService.write")(function* ( + path: string, + cache: UpdateCheckCacheValue, + ) { + yield* Effect.tryPromise({ + try: async () => { + await mkdir(dirname(path), { recursive: true }); + const content = `${JSON.stringify(cache, null, 2)}\n`; + const canWriteWithBun = typeof Bun !== "undefined" && typeof Bun.write === "function"; + if (canWriteWithBun) { + await Bun.write(path, content); + } else await writeFile(path, content, "utf8"); + }, + catch: (cause) => new NpmUpdateCheckError({ cause }), + }); + }); + return { read, write } as const; + }), +}) { + static readonly layer = Layer.effect(NpmUpdateCacheService, NpmUpdateCacheService.make); +} + +export const npmUpdateCacheLayer = NpmUpdateCacheService.layer; diff --git a/src/domains/updates/repo/update-check-platform.ts b/src/domains/updates/repo/update-check-platform.ts new file mode 100644 index 0000000..56c2cf6 --- /dev/null +++ b/src/domains/updates/repo/update-check-platform.ts @@ -0,0 +1,28 @@ +import { Context, Effect, Layer } from "effect"; + +export class NpmUpdateCheckPlatformService extends Context.Service< + NpmUpdateCheckPlatformService, + { + readonly now: Effect.Effect; + readonly environmentFlag: (name: string) => Effect.Effect; + } +>()("@rendotdev/rig/updates/NpmUpdateCheckPlatformService", { + make: Effect.gen(function* () { + const now = Effect.sync(() => Date.now()).pipe( + Effect.withSpan("NpmUpdateCheckPlatformService.now"), + ); + const environmentFlag = Effect.fn("NpmUpdateCheckPlatformService.environmentFlag")(function* ( + name: string, + ) { + return process.env[name]; + }); + return { now, environmentFlag } as const; + }), +}) { + static readonly layer = Layer.effect( + NpmUpdateCheckPlatformService, + NpmUpdateCheckPlatformService.make, + ); +} + +export const npmUpdateCheckPlatformLayer = NpmUpdateCheckPlatformService.layer; diff --git a/src/domains/updates/runtime/dev-link.ts b/src/domains/updates/runtime/dev-link.ts new file mode 100644 index 0000000..2060933 --- /dev/null +++ b/src/domains/updates/runtime/dev-link.ts @@ -0,0 +1,67 @@ +import { delimiter } from "node:path"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { RigPathsConfigService, rigPathsConfigLayer } from "../../../providers/paths/rig-paths"; +import { DevLinkConfigService } from "../config/dev-link-config"; +import { + DevLinkMutationService, + devLinkOperationsLayer, + DevLinkRenderService, + DevLinkStateService, +} from "../service/dev-link"; +import type { DevLinkCommandOptions, DevLinkStatus } from "../types/dev-link"; + +export class DevLinkService extends Context.Service< + DevLinkService, + { + readonly link: (options?: DevLinkCommandOptions) => Effect.Effect; + readonly unlink: (options?: DevLinkCommandOptions) => Effect.Effect; + readonly status: (options?: DevLinkCommandOptions) => Effect.Effect; + readonly renderLink: (status: DevLinkStatus) => Effect.Effect; + readonly renderUnlink: (status: DevLinkStatus) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkService", { + make: Effect.gen(function* () { + const mutations = yield* DevLinkMutationService; + const state = yield* DevLinkStateService; + const renderer = yield* DevLinkRenderService; + const link = Effect.fn("DevLinkService.link")(function* (options?: DevLinkCommandOptions) { + return yield* mutations.link(options); + }); + const unlink = Effect.fn("DevLinkService.unlink")(function* (options?: DevLinkCommandOptions) { + return yield* mutations.unlink(options); + }); + const status = Effect.fn("DevLinkService.status")(function* (options?: DevLinkCommandOptions) { + return yield* state.status(options); + }); + const renderLink = Effect.fn("DevLinkService.renderLink")(function* (value: DevLinkStatus) { + return yield* renderer.renderLink(value); + }); + const renderUnlink = Effect.fn("DevLinkService.renderUnlink")(function* (value: DevLinkStatus) { + return yield* renderer.renderUnlink(value); + }); + return { link, unlink, status, renderLink, renderUnlink } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkService, DevLinkService.make); +} + +const devLinkConfigLayer = Layer.effect( + DevLinkConfigService, + Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const get = Effect.succeed({ + homeDir: paths.homeDir, + repoRoot: process.cwd(), + platform: process.platform, + pathEnvironment: process.env.PATH ?? "", + pathDelimiter: delimiter, + } as const).pipe(Effect.withSpan("DevLinkConfigService.get")); + return { get } as const; + }), +).pipe(Layer.provide(rigPathsConfigLayer)); + +export const devLinkLayer = DevLinkService.layer.pipe( + Layer.provide(devLinkOperationsLayer), + Layer.provide(devLinkConfigLayer), +); diff --git a/src/domains/updates/runtime/npm-update-check.ts b/src/domains/updates/runtime/npm-update-check.ts new file mode 100644 index 0000000..3be091b --- /dev/null +++ b/src/domains/updates/runtime/npm-update-check.ts @@ -0,0 +1,22 @@ +import { Effect, Layer } from "effect"; +import { RigPathsConfigService, rigPathsConfigLayer } from "../../../providers/paths/rig-paths"; +import { NpmUpdateCheckConfigService } from "../config/npm-update-check-config"; +import { npmUpdateCheckServiceLayer } from "../service/npm-update-check"; + +const npmUpdateCheckConfigLayer = Layer.effect( + NpmUpdateCheckConfigService, + Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const get = Effect.succeed({ + updateCheckCachePath: paths.updateCheckCachePath, + cacheTtlMs: 24 * 60 * 60 * 1000, + timeoutMs: 750, + packageName: "@rendotdev/rig", + } as const).pipe(Effect.withSpan("NpmUpdateCheckConfigService.get")); + return { get } as const; + }), +).pipe(Layer.provide(rigPathsConfigLayer)); + +export const npmUpdateCheckLayer = npmUpdateCheckServiceLayer.pipe( + Layer.provide(npmUpdateCheckConfigLayer), +); diff --git a/src/domains/updates/runtime/rig-updater.ts b/src/domains/updates/runtime/rig-updater.ts new file mode 100644 index 0000000..4b9b2f7 --- /dev/null +++ b/src/domains/updates/runtime/rig-updater.ts @@ -0,0 +1,57 @@ +import { Context, Effect, Layer } from "effect"; +import { RigUpdaterConfigService } from "../config/rig-updater-config"; +import { + RigUpdateExecutionService, + rigUpdatePlanningLayer, + RigUpdatePlanningService, + rigUpdaterSupportLayer, +} from "../service/rig-update-planning"; +import { + type RigUpdatePlan, + RigUpdateError, + type RigUpdateResult, + type RigUpdateStep, + type RunnableRigUpdatePlan, +} from "../types/updates"; + +export class RigUpdaterService extends Context.Service< + RigUpdaterService, + { + readonly currentVersion: Effect.Effect; + readonly plan: Effect.Effect; + readonly update: (plan?: RigUpdatePlan) => Effect.Effect; + readonly sync: ( + plan: RunnableRigUpdatePlan, + ) => Effect.Effect<{ step: RigUpdateStep; output: string }, RigUpdateError>; + } +>()("@rendotdev/rig/updates/RigUpdaterService", { + make: Effect.gen(function* () { + const config = yield* RigUpdaterConfigService; + const execution = yield* RigUpdateExecutionService; + const planning = yield* RigUpdatePlanningService; + const currentVersion = Effect.succeed(config.params.currentVersion).pipe( + Effect.withSpan("RigUpdaterService.currentVersion"), + ); + const plan = planning.plan.pipe(Effect.withSpan("RigUpdaterService.plan")); + const update = Effect.fn("RigUpdaterService.update")(function* (provided?: RigUpdatePlan) { + const updatePlan = provided ?? (yield* plan); + if (updatePlan.status === "current") { + return { status: "current" as const, version: updatePlan.version }; + } + if (updatePlan.status === "skipped") return updatePlan; + return yield* execution.update(updatePlan); + }); + const sync = Effect.fn("RigUpdaterService.sync")(function* (updatePlan: RunnableRigUpdatePlan) { + return yield* execution.sync(updatePlan); + }); + return { currentVersion, plan, update, sync } as const; + }), +}) { + static readonly layer = Layer.effect(RigUpdaterService, RigUpdaterService.make); +} + +export const rigUpdaterOperationLayer = RigUpdaterService.layer.pipe( + Layer.provide(Layer.merge(rigUpdatePlanningLayer, RigUpdateExecutionService.layer)), +); + +export const rigUpdaterLayer = RigUpdaterService.layer.pipe(Layer.provide(rigUpdaterSupportLayer)); diff --git a/src/domains/updates/service/dev-link.ts b/src/domains/updates/service/dev-link.ts new file mode 100644 index 0000000..536b907 --- /dev/null +++ b/src/domains/updates/service/dev-link.ts @@ -0,0 +1,218 @@ +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../../../providers/errors/rig-error"; +import { DevLinkConfigService } from "../config/dev-link-config"; +import { devLinkRepositoryLayer, DevLinkPlatformService } from "../repo/dev-link-repository"; +import type { DevLinkCommandOptions, DevLinkStatus, ExistingDevLinkShim } from "../types/dev-link"; + +export class DevLinkPathService extends Context.Service< + DevLinkPathService, + { + readonly repoRoot: Effect.Effect; + readonly binDirectory: (path?: string) => Effect.Effect; + readonly shimPath: (binDirectory: string) => Effect.Effect; + readonly shimSource: (repoRoot: string) => Effect.Effect; + readonly binDirectoryOnPath: (path: string) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkPathService", { + make: Effect.gen(function* () { + const config = yield* (yield* DevLinkConfigService).get; + const platform = yield* DevLinkPlatformService; + const repoRoot = Effect.gen(function* () { + const path = yield* platform.resolve(config.repoRoot); + const packagePath = yield* platform.join([path, "package.json"]); + const cliPath = yield* platform.join([path, "src", "app", "cli", "entrypoint.ts"]); + const isMissingRigEntrypoint = + !(yield* platform.exists(packagePath)) || !(yield* platform.exists(cliPath)); + if (isMissingRigEntrypoint) { + return yield* new RigError({ + code: "DEV_LINK_ERROR", + message: "Run dev link from the Rig repository root.", + details: { repoRoot: path, expected: ["package.json", "src/app/cli/entrypoint.ts"] }, + }); + } + return path; + }).pipe(Effect.withSpan("DevLinkPathService.repoRoot")); + const binDirectory = Effect.fn("DevLinkPathService.binDirectory")(function* (path?: string) { + return path + ? yield* platform.resolve(path) + : yield* platform.join([config.homeDir, ".local", "bin"]); + }); + const shimPath = Effect.fn("DevLinkPathService.shimPath")(function* (binDir: string) { + return yield* platform.join([binDir, config.platform === "win32" ? "rig.cmd" : "rig"]); + }); + const shimSource = Effect.fn("DevLinkPathService.shimSource")(function* (root: string) { + return config.platform === "win32" + ? `@echo off\nrem Rig dev shim. Safe to overwrite with \`rig dev link\`.\nset "RIG_DEV_REPO=${root}"\nbun --install=fallback run "%RIG_DEV_REPO%\\src\\app\\cli\\entrypoint.ts" %*\n` + : `#!/bin/sh\n# Rig dev shim. Safe to overwrite with \`rig dev link\`.\nRIG_DEV_REPO=${JSON.stringify(root)}\nexec bun --install=fallback run "$RIG_DEV_REPO/src/app/cli/entrypoint.ts" "$@"\n`; + }); + const binDirectoryOnPath = Effect.fn("DevLinkPathService.binDirectoryOnPath")(function* ( + path: string, + ) { + const resolved = yield* platform.resolve(path); + for (const entry of config.pathEnvironment.split(config.pathDelimiter)) { + if ((yield* platform.resolve(entry)) === resolved) return true; + } + return false; + }); + return { repoRoot, binDirectory, shimPath, shimSource, binDirectoryOnPath } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkPathService, DevLinkPathService.make); +} + +export class DevLinkStateService extends Context.Service< + DevLinkStateService, + { + readonly readExisting: (path: string) => Effect.Effect; + readonly status: (options?: DevLinkCommandOptions) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkStateService", { + make: Effect.gen(function* () { + const config = yield* (yield* DevLinkConfigService).get; + const platform = yield* DevLinkPlatformService; + const paths = yield* DevLinkPathService; + const readExisting = Effect.fn("DevLinkStateService.readExisting")(function* (path: string) { + if (!(yield* platform.exists(path))) { + return { exists: false, isRigDevShim: false, content: "" } as const; + } + if (!(yield* platform.isFile(path))) { + return { exists: true, isRigDevShim: false, content: "" } as const; + } + const content = yield* platform.readText(path); + return { exists: true, isRigDevShim: content.includes("Rig dev shim"), content } as const; + }); + const status = Effect.fn("DevLinkStateService.status")(function* ( + options: DevLinkCommandOptions = {}, + ) { + const repoRoot = yield* paths.repoRoot; + const binDir = yield* paths.binDirectory(options.binDir); + const shimPath = yield* paths.shimPath(binDir); + const existing = yield* readExisting(shimPath); + const marker = + config.platform === "win32" + ? `RIG_DEV_REPO=${repoRoot}` + : `RIG_DEV_REPO=${JSON.stringify(repoRoot)}`; + return { + repoRoot, + binDir, + shimPath, + exists: existing.exists, + isRigDevShim: existing.isRigDevShim, + pointsToCurrentRepo: existing.content.includes(marker), + binDirOnPath: yield* paths.binDirectoryOnPath(binDir), + }; + }); + return { readExisting, status } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkStateService, DevLinkStateService.make); +} + +export class DevLinkMutationService extends Context.Service< + DevLinkMutationService, + { + readonly link: (options?: DevLinkCommandOptions) => Effect.Effect; + readonly unlink: (options?: DevLinkCommandOptions) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkMutationService", { + make: Effect.gen(function* () { + const config = yield* (yield* DevLinkConfigService).get; + const platform = yield* DevLinkPlatformService; + const paths = yield* DevLinkPathService; + const state = yield* DevLinkStateService; + const link = Effect.fn("DevLinkMutationService.link")(function* ( + options: DevLinkCommandOptions = {}, + ) { + const repoRoot = yield* paths.repoRoot; + const binDir = yield* paths.binDirectory(options.binDir); + const shimPath = yield* paths.shimPath(binDir); + const existing = yield* state.readExisting(shimPath); + const wouldOverwriteForeignShim = existing.exists && !existing.isRigDevShim && !options.force; + if (wouldOverwriteForeignShim) { + return yield* new RigError({ + code: "DEV_LINK_ERROR", + message: `Refusing to overwrite existing file: ${shimPath}`, + details: { shimPath, hint: "Use --force to replace it." }, + }); + } + yield* platform.makeDirectory(binDir); + yield* platform.writeText(shimPath, yield* paths.shimSource(repoRoot)); + if (config.platform !== "win32") yield* platform.chmod(shimPath, 0o755); + return yield* state.status(options); + }); + const unlink = Effect.fn("DevLinkMutationService.unlink")(function* ( + options: DevLinkCommandOptions = {}, + ) { + const binDir = yield* paths.binDirectory(options.binDir); + const shimPath = yield* paths.shimPath(binDir); + const existing = yield* state.readExisting(shimPath); + const wouldRemoveForeignShim = existing.exists && !existing.isRigDevShim && !options.force; + if (wouldRemoveForeignShim) { + return yield* new RigError({ + code: "DEV_LINK_ERROR", + message: `Refusing to remove non-Rig dev shim: ${shimPath}`, + details: { shimPath, hint: "Use --force to remove it anyway." }, + }); + } + if (existing.exists) yield* platform.remove(shimPath); + return yield* state.status(options); + }); + return { link, unlink } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkMutationService, DevLinkMutationService.make); +} + +export class DevLinkRenderService extends Context.Service< + DevLinkRenderService, + { + readonly renderLink: (status: DevLinkStatus) => Effect.Effect; + readonly renderUnlink: (status: DevLinkStatus) => Effect.Effect; + } +>()("@rendotdev/rig/updates/DevLinkRenderService", { + make: Effect.gen(function* () { + const renderLink = Effect.fn("DevLinkRenderService.renderLink")(function* ( + status: DevLinkStatus, + ) { + const lines = [ + "Rig dev link is ready.", + "", + `Shim: ${status.shimPath}`, + `Repo: ${status.repoRoot}`, + `On PATH: ${status.binDirOnPath ? "yes" : "no"}`, + "", + "Try:", + " rig", + " rig list", + ]; + if (!status.binDirOnPath) { + lines.push("", `Add ${status.binDir} to PATH to run rig from any shell.`); + } + return lines.join("\n"); + }); + const renderUnlink = Effect.fn("DevLinkRenderService.renderUnlink")(function* ( + status: DevLinkStatus, + ) { + return ["Rig dev link removed.", "", `Shim: ${status.shimPath}`].join("\n"); + }); + return { renderLink, renderUnlink } as const; + }), +}) { + static readonly layer = Layer.effect(DevLinkRenderService, DevLinkRenderService.make); +} + +const devLinkPathLayer = DevLinkPathService.layer.pipe(Layer.provide(devLinkRepositoryLayer)); + +const devLinkStateLayer = DevLinkStateService.layer.pipe( + Layer.provide(Layer.merge(devLinkRepositoryLayer, devLinkPathLayer)), +); + +const devLinkMutationLayer = DevLinkMutationService.layer.pipe( + Layer.provide(Layer.mergeAll(devLinkRepositoryLayer, devLinkPathLayer, devLinkStateLayer)), +); + +export const devLinkOperationsLayer = Layer.mergeAll( + devLinkMutationLayer, + devLinkStateLayer, + DevLinkRenderService.layer, +); diff --git a/src/domains/updates/service/npm-update-check.test.ts b/src/domains/updates/service/npm-update-check.test.ts new file mode 100644 index 0000000..269a981 --- /dev/null +++ b/src/domains/updates/service/npm-update-check.test.ts @@ -0,0 +1,208 @@ +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { NpmUpdateCheckConfigService } from "../config/npm-update-check-config"; +import { NpmRegistryPlatformService, NpmRegistryService } from "../repo/npm-registry"; +import { NpmUpdateCacheService } from "../repo/update-cache"; +import { NpmUpdateCheckPlatformService } from "../repo/update-check-platform"; +import { NpmRegistryError } from "../types/updates"; +import { NpmUpdateCheckService } from "./npm-update-check"; + +const homes: string[] = []; + +const createHome = async () => { + const home = await mkdtemp(join(tmpdir(), "rig-update-check-")); + homes.push(home); + return home; +}; + +const createUpdateCheckLayer = (params: { + home: string; + now?: number; + cacheTtlMs?: number; + timeoutMs?: number; + packageName?: string; + updateCheckFlag?: string; + fetchJson: NpmRegistryPlatformService["Service"]["fetchJson"]; +}) => { + const registryLayer = NpmRegistryService.layer.pipe( + Layer.provide(Layer.succeed(NpmRegistryPlatformService, { fetchJson: params.fetchJson })), + ); + return NpmUpdateCheckService.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(NpmUpdateCheckConfigService, { + get: Effect.succeed({ + updateCheckCachePath: join(params.home, "rig", "update-check.json"), + cacheTtlMs: params.cacheTtlMs ?? 24 * 60 * 60 * 1000, + timeoutMs: params.timeoutMs ?? 750, + packageName: params.packageName ?? "@rendotdev/rig", + }), + }), + Layer.succeed(NpmUpdateCheckPlatformService, { + now: Effect.succeed(params.now ?? Date.now()), + environmentFlag: Effect.fn("NpmUpdateCheckPlatformService.environmentFlag")(function* ( + name: string, + ) { + return name === "RIG_UPDATE_CHECK" ? params.updateCheckFlag : undefined; + }), + }), + registryLayer, + NpmUpdateCacheService.layer, + ), + ), + ); +}; + +const check = (currentVersion: string, layer: Layer.Layer) => + Effect.runPromise( + NpmUpdateCheckService.use((service) => service.check(currentVersion)).pipe( + Effect.provide(layer), + ), + ); + +afterEach(async () => { + await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); +}); + +describe("npm update checks", () => { + test("skips checks when disabled", async () => { + const home = await createHome(); + const layer = createUpdateCheckLayer({ + home, + updateCheckFlag: "0", + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return yield* new NpmRegistryError({ cause: new Error("fetch should not be called") }); + }), + }); + await expect(check("0.0.3", layer)).resolves.toBeUndefined(); + }); + + test("fetches, caches, and renders newer npm versions", async () => { + const home = await createHome(); + const urls: string[] = []; + const fetchJson = Effect.fn("NpmRegistryPlatformService.fetchJson")(function* (url: string) { + urls.push(url); + return { ok: true, status: 200, data: { version: "1.2.4" } } as const; + }); + const layer = createUpdateCheckLayer({ + home, + now: 1000, + packageName: "@scope/tool", + fetchJson, + }); + await expect(check("1.2.3", layer)).resolves.toMatchObject({ + currentVersion: "1.2.3", + latestVersion: "1.2.4", + message: "Rig update available: @scope/tool 1.2.3 -> 1.2.4. Run npm install -g @scope/tool.", + }); + expect(urls).toEqual(["https://registry.npmjs.org/%40scope%2Ftool/latest"]); + const cachePath = join(home, "rig", "update-check.json"); + expect(await readFile(cachePath, "utf8")).toContain('"latestVersion": "1.2.4"'); + + const cachedLayer = createUpdateCheckLayer({ + home, + now: 1001, + packageName: "@scope/tool", + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return yield* new NpmRegistryError({ cause: new Error("fresh cache should avoid fetch") }); + }), + }); + await expect(check("1.2", cachedLayer)).resolves.toMatchObject({ latestVersion: "1.2.4" }); + await expect(check("1.2.3", cachedLayer)).resolves.toMatchObject({ latestVersion: "1.2.4" }); + await expect(check("1.2.4", cachedLayer)).resolves.toBeUndefined(); + await expect(check("1.2.5", cachedLayer)).resolves.toBeUndefined(); + + await writeFile(cachePath, '{"checkedAt":1002,"latestVersion":"1.2"}\n', "utf8"); + const shorterLayer = createUpdateCheckLayer({ + home, + now: 1003, + packageName: "@scope/tool", + fetchJson, + }); + await expect(check("1.2.1", shorterLayer)).resolves.toBeUndefined(); + }); + + test("ignores stale cache misses and invalid registry data", async () => { + const home = await createHome(); + const cachePath = join(home, "rig", "update-check.json"); + await mkdir(join(home, "rig"), { recursive: true }); + await writeFile(cachePath, '{"checkedAt":1}\n', "utf8"); + const missingLayer = createUpdateCheckLayer({ + home, + now: 10_000_000, + cacheTtlMs: 1, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return { ok: false, status: 404, data: undefined } as const; + }), + }); + await expect(check("1.0.0", missingLayer)).resolves.toBeUndefined(); + + const invalidLayer = createUpdateCheckLayer({ + home, + now: 10_000_000, + cacheTtlMs: 1, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return { ok: true, status: 200, data: { name: "rig" } } as const; + }), + }); + await expect(check("1.0.0", invalidLayer)).resolves.toBeUndefined(); + + await writeFile(cachePath, "[]\n", "utf8"); + await expect(check("1.0.0", missingLayer)).resolves.toBeUndefined(); + }); + + test("handles invalid cache files and registry timeouts", async () => { + const home = await createHome(); + await mkdir(join(home, "rig"), { recursive: true }); + await writeFile(join(home, "rig", "update-check.json"), "{ nope", "utf8"); + const failureLayer = createUpdateCheckLayer({ + home, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return yield* new NpmRegistryError({ cause: new Error("network failed") }); + }), + }); + await expect(check("1.0.0", failureLayer)).resolves.toBeUndefined(); + + const timeoutLayer = createUpdateCheckLayer({ + home, + timeoutMs: 1, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + return yield* Effect.never; + }), + }); + await expect(check("1.0.0", timeoutLayer)).resolves.toBeUndefined(); + }); + + test("retries transient registry failures once and preserves permanent failures", async () => { + const home = await createHome(); + let attempts = 0; + const transientLayer = createUpdateCheckLayer({ + home, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + attempts += 1; + return attempts === 1 + ? ({ ok: false, status: 503, data: undefined } as const) + : ({ ok: true, status: 200, data: { version: "2.0.0" } } as const); + }), + }); + await expect(check("1.0.0", transientLayer)).resolves.toMatchObject({ + latestVersion: "2.0.0", + }); + expect(attempts).toBe(2); + + const permanentHome = await createHome(); + let permanentAttempts = 0; + const permanentLayer = createUpdateCheckLayer({ + home: permanentHome, + fetchJson: Effect.fn("NpmRegistryPlatformService.fetchJson")(function* () { + permanentAttempts += 1; + return { ok: false, status: 404, data: undefined } as const; + }), + }); + await expect(check("1.0.0", permanentLayer)).resolves.toBeUndefined(); + expect(permanentAttempts).toBe(1); + }); +}); diff --git a/src/domains/updates/service/npm-update-check.ts b/src/domains/updates/service/npm-update-check.ts new file mode 100644 index 0000000..345735d --- /dev/null +++ b/src/domains/updates/service/npm-update-check.ts @@ -0,0 +1,89 @@ +import { Context, Effect, Layer, Schedule } from "effect"; +import { NpmUpdateCheckConfigService } from "../config/npm-update-check-config"; +import { npmRegistryLayer, NpmRegistryService } from "../repo/npm-registry"; +import { npmUpdateCacheLayer, NpmUpdateCacheService } from "../repo/update-cache"; +import { + npmUpdateCheckPlatformLayer, + NpmUpdateCheckPlatformService, +} from "../repo/update-check-platform"; +import { NpmUpdateCheckError, type UpdateCheckNotice } from "../types/updates"; + +const REGISTRY_RETRY_DELAY = "25 millis"; +const REGISTRY_RETRY_COUNT = 1; + +export class NpmUpdateCheckService extends Context.Service< + NpmUpdateCheckService, + { + readonly check: ( + currentVersion: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/updates/NpmUpdateCheckService", { + make: Effect.gen(function* () { + const config = yield* (yield* NpmUpdateCheckConfigService).get; + const platform = yield* NpmUpdateCheckPlatformService; + const registry = yield* NpmRegistryService; + const cache = yield* NpmUpdateCacheService; + const versionParts = (version: string): number[] => + version + .replace(/^v/u, "") + .split(/[.-]/u) + .map((part) => Number.parseInt(part, 10)) + .filter((part) => Number.isFinite(part)); + const isNewerVersion = (candidate: string, current: string): boolean => { + const candidateParts = versionParts(candidate); + const currentParts = versionParts(current); + const length = Math.max(candidateParts.length, currentParts.length); + for (let index = 0; index < length; index += 1) { + const candidatePart = candidateParts[index] ?? 0; + const currentPart = currentParts[index] ?? 0; + if (candidatePart > currentPart) return true; + if (candidatePart < currentPart) return false; + } + return false; + }; + const notice = (currentVersion: string, latestVersion?: string) => + !latestVersion || !isNewerVersion(latestVersion, currentVersion) + ? undefined + : { + packageName: config.packageName, + currentVersion, + latestVersion, + message: `Rig update available: ${config.packageName} ${currentVersion} -> ${latestVersion}. Run npm install -g ${config.packageName}.`, + }; + const check = Effect.fn("NpmUpdateCheckService.check")(function* (currentVersion: string) { + if ((yield* platform.environmentFlag("RIG_UPDATE_CHECK")) === "0") return undefined; + const now = yield* platform.now; + const cached = yield* cache + .read(config.updateCheckCachePath) + .pipe(Effect.orElseSucceed(() => undefined)); + const isCacheFresh = cached !== undefined && now - cached.checkedAt < config.cacheTtlMs; + if (isCacheFresh) { + return notice(currentVersion, cached.latestVersion); + } + const latestVersion = yield* registry + .latestVersion(config.packageName, config.timeoutMs) + .pipe( + Effect.retry({ + schedule: Schedule.exponential(REGISTRY_RETRY_DELAY).pipe( + Schedule.upTo({ times: REGISTRY_RETRY_COUNT }), + ), + }), + Effect.orElseSucceed(() => undefined), + ); + if (!latestVersion) return undefined; + yield* cache.write(config.updateCheckCachePath, { + checkedAt: yield* platform.now, + latestVersion, + }); + return notice(currentVersion, latestVersion); + }); + return { check } as const; + }), +}) { + static readonly layer = Layer.effect(NpmUpdateCheckService, NpmUpdateCheckService.make); +} + +export const npmUpdateCheckServiceLayer = NpmUpdateCheckService.layer.pipe( + Layer.provide(Layer.mergeAll(npmUpdateCheckPlatformLayer, npmRegistryLayer, npmUpdateCacheLayer)), +); diff --git a/src/domains/updates/service/rig-update-planning.ts b/src/domains/updates/service/rig-update-planning.ts new file mode 100644 index 0000000..7e29574 --- /dev/null +++ b/src/domains/updates/service/rig-update-planning.ts @@ -0,0 +1,232 @@ +import { Context, Effect, Layer } from "effect"; +import { PackageRootService, packageRootLayer } from "../../../providers/package/package-root"; +import { RigUpdaterConfigService } from "../config/rig-updater-config"; +import { + RigUpdateCommandRunnerService, + rigUpdateCommandRunnerLayer, + RigUpdaterPlatformService, + rigUpdaterRepositoryLayer, +} from "../repo/rig-updater-repository"; +import { + type RigInstallation, + RigUpdateError, + type RigUpdatePlan, + type RigUpdateResult, + type RigUpdateStep, + type RunnableRigUpdatePlan, +} from "../types/updates"; + +class RigInstallationService extends Context.Service< + RigInstallationService, + { readonly resolve: (packageRoot: string) => Effect.Effect } +>()("@rendotdev/rig/updates/RigInstallationService", { + make: Effect.gen(function* () { + const platform = yield* RigUpdaterPlatformService; + const resolve = Effect.fn("RigInstallationService.resolve")(function* (packageRoot: string) { + const scopeDirectory = yield* platform.dirname(packageRoot); + const nodeModulesDirectory = yield* platform.dirname(scopeDirectory); + const hasUnsupportedPackageLayout = + (yield* platform.basename(packageRoot)) !== "rig" || + (yield* platform.basename(scopeDirectory)) !== "@rendotdev" || + (yield* platform.basename(nodeModulesDirectory)) !== "node_modules"; + if (hasUnsupportedPackageLayout) { + return { reason: "Rig is not running from a supported global npm or Bun installation." }; + } + const parentDirectory = yield* platform.dirname(nodeModulesDirectory); + if ((yield* platform.basename(parentDirectory)) === "lib") { + const prefix = yield* platform.dirname(parentDirectory); + return { + kind: "npm" as const, + packageManager: yield* platform.join([prefix, "bin", "npm"]), + prefix, + rig: yield* platform.join([prefix, "bin", "rig"]), + }; + } + const installDirectory = yield* platform.dirname(parentDirectory); + const bunDirectory = yield* platform.dirname(installDirectory); + const hasSupportedBunLayout = + (yield* platform.basename(parentDirectory)) === "global" && + (yield* platform.basename(installDirectory)) === "install" && + (yield* platform.basename(bunDirectory)) === ".bun"; + if (hasSupportedBunLayout) { + return { + kind: "bun" as const, + packageManager: yield* platform.join([bunDirectory, "bin", "bun"]), + prefix: bunDirectory, + rig: yield* platform.join([bunDirectory, "bin", "rig"]), + }; + } + return { reason: "Rig is not running from a supported global npm or Bun installation." }; + }); + return { resolve } as const; + }), +}) { + static readonly layer = Layer.effect(RigInstallationService, RigInstallationService.make); +} + +export class RigUpdatePlanningService extends Context.Service< + RigUpdatePlanningService, + { readonly plan: Effect.Effect } +>()("@rendotdev/rig/updates/RigUpdatePlanningService", { + make: Effect.gen(function* () { + const config = yield* RigUpdaterConfigService; + const platform = yield* RigUpdaterPlatformService; + const commandRunner = yield* RigUpdateCommandRunnerService; + const installations = yield* RigInstallationService; + const packageRoot = yield* platform.resolve(config.params.packageRoot); + const parseLatestVersion = (output: string): string => { + let value: unknown; + try { + value = JSON.parse(output); + } catch { + value = output.trim(); + } + const isInvalidLatestVersion = + typeof value !== "string" || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value.trim()); + if (isInvalidLatestVersion) { + throw new Error("The package manager returned an invalid latest Rig version."); + } + return (value as string).trim(); + }; + const plan = Effect.gen(function* () { + const installation = yield* installations.resolve(packageRoot); + if ("reason" in installation) { + return { status: "skipped" as const, reason: installation.reason }; + } + if (!(yield* platform.executableExists(installation.packageManager))) { + return { + status: "skipped" as const, + reason: `The package manager for this Rig installation was not found at ${installation.packageManager}.`, + }; + } + if (!(yield* platform.executableExists(installation.rig))) { + return { + status: "skipped" as const, + reason: `The Rig executable for this installation was not found at ${installation.rig}.`, + }; + } + const output = yield* commandRunner.read({ + command: installation.packageManager, + args: + installation.kind === "npm" + ? ["view", "@rendotdev/rig@latest", "version", "--json"] + : ["pm", "view", "@rendotdev/rig@latest", "version"], + }); + const latestVersion = yield* Effect.try({ + try: () => parseLatestVersion(output), + catch: (cause) => new RigUpdateError({ operation: "plan", cause }), + }); + const syncStep = { + command: installation.rig, + args: ["init"], + env: { RIG_UPDATE_CHECK: "0" }, + }; + if (latestVersion === config.params.currentVersion) { + return { status: "current" as const, version: config.params.currentVersion, syncStep }; + } + return { + status: "ready" as const, + currentVersion: config.params.currentVersion, + latestVersion, + updateStep: { + command: installation.packageManager, + args: + installation.kind === "npm" + ? [ + "install", + "--global", + "--prefix", + installation.prefix, + `@rendotdev/rig@${latestVersion}`, + ] + : ["install", "--global", `@rendotdev/rig@${latestVersion}`], + }, + versionStep: { command: installation.rig, args: ["--version"] }, + syncStep, + }; + }).pipe(Effect.withSpan("RigUpdatePlanningService.plan")); + return { plan } as const; + }), +}) { + static readonly layer = Layer.effect(RigUpdatePlanningService, RigUpdatePlanningService.make); +} + +export class RigUpdateExecutionService extends Context.Service< + RigUpdateExecutionService, + { + readonly update: ( + plan: Exclude, + ) => Effect.Effect; + readonly sync: ( + plan: RunnableRigUpdatePlan, + ) => Effect.Effect<{ step: RigUpdateStep; output: string }, RigUpdateError>; + } +>()("@rendotdev/rig/updates/RigUpdateExecutionService", { + make: Effect.gen(function* () { + const commandRunner = yield* RigUpdateCommandRunnerService; + const update = Effect.fn("RigUpdateExecutionService.update")(function* ( + updatePlan: Exclude, + ) { + const output = yield* commandRunner.run(updatePlan.updateStep); + const version = (yield* commandRunner.read(updatePlan.versionStep)).trim(); + if (version !== updatePlan.latestVersion) { + return yield* new RigUpdateError({ + operation: "update", + cause: new Error( + `Rig reported version ${version || "unknown"} after updating to ${updatePlan.latestVersion}.`, + ), + }); + } + return { + status: "updated" as const, + previousVersion: updatePlan.currentVersion, + version, + step: updatePlan.updateStep, + output, + }; + }); + const sync = Effect.fn("RigUpdateExecutionService.sync")(function* ( + updatePlan: RunnableRigUpdatePlan, + ) { + return { + step: updatePlan.syncStep, + output: yield* commandRunner.run(updatePlan.syncStep), + }; + }); + return { update, sync } as const; + }), +}) { + static readonly layer = Layer.effect(RigUpdateExecutionService, RigUpdateExecutionService.make); +} + +export const rigUpdatePlanningLayer = RigUpdatePlanningService.layer.pipe( + Layer.provide(RigInstallationService.layer), +); + +const rigUpdaterConfigLayer = Layer.effect( + RigUpdaterConfigService, + Effect.gen(function* () { + const packageTargets = yield* PackageRootService; + const repository = yield* RigUpdaterPlatformService; + const packageRoot = yield* packageTargets.find(import.meta.url); + const packagePath = yield* packageTargets.packageFile(import.meta.url, "package.json"); + const currentVersion = yield* repository.readPackageVersion(packagePath); + return { params: { packageRoot, currentVersion } } as const; + }), +).pipe(Layer.provide(Layer.merge(packageRootLayer, rigUpdaterRepositoryLayer))); + +const rigUpdatePlanningConfiguredLayer = rigUpdatePlanningLayer.pipe( + Layer.provide( + Layer.mergeAll(rigUpdaterConfigLayer, rigUpdaterRepositoryLayer, rigUpdateCommandRunnerLayer), + ), +); + +const rigUpdateExecutionLayer = RigUpdateExecutionService.layer.pipe( + Layer.provide(rigUpdateCommandRunnerLayer), +); + +export const rigUpdaterSupportLayer = Layer.mergeAll( + rigUpdaterConfigLayer, + rigUpdatePlanningConfiguredLayer, + rigUpdateExecutionLayer, +); diff --git a/src/domains/updates/types/dev-link.ts b/src/domains/updates/types/dev-link.ts new file mode 100644 index 0000000..32fcdcd --- /dev/null +++ b/src/domains/updates/types/dev-link.ts @@ -0,0 +1,25 @@ +export type DevLinkCommandOptions = { readonly binDir?: string; readonly force?: boolean }; + +export type DevLinkConfig = Readonly<{ + homeDir: string; + repoRoot: string; + platform: NodeJS.Platform; + pathEnvironment: string; + pathDelimiter: string; +}>; + +export type DevLinkStatus = Readonly<{ + repoRoot: string; + binDir: string; + shimPath: string; + exists: boolean; + isRigDevShim: boolean; + pointsToCurrentRepo: boolean; + binDirOnPath: boolean; +}>; + +export type ExistingDevLinkShim = Readonly<{ + exists: boolean; + isRigDevShim: boolean; + content: string; +}>; diff --git a/src/domains/updates/types/updates.ts b/src/domains/updates/types/updates.ts new file mode 100644 index 0000000..d8f4f2f --- /dev/null +++ b/src/domains/updates/types/updates.ts @@ -0,0 +1,80 @@ +import { Schema } from "effect"; + +export type UpdateCheckNotice = Readonly<{ + packageName: string; + currentVersion: string; + latestVersion: string; + message: string; +}>; + +export type NpmUpdateCheckConfig = Readonly<{ + updateCheckCachePath: string; + cacheTtlMs: number; + timeoutMs: number; + packageName: string; +}>; + +export const UpdateCheckCache = Schema.Struct({ + checkedAt: Schema.Finite, + latestVersion: Schema.optional(Schema.String), +}); +export type UpdateCheckCache = typeof UpdateCheckCache.Type; + +export const NpmRegistryResponse = Schema.Struct({ version: Schema.String }); + +export class NpmRegistryError extends Schema.TaggedErrorClass()( + "NpmRegistryError", + { cause: Schema.Defect() }, +) {} + +export class NpmRegistryResponseError extends Schema.TaggedErrorClass()( + "NpmRegistryResponseError", + { status: Schema.Finite }, +) {} + +export class NpmUpdateCheckError extends Schema.TaggedErrorClass()( + "NpmUpdateCheckError", + { cause: Schema.Defect() }, +) {} + +export type RigUpdateStep = { command: string; args: string[]; env?: NodeJS.ProcessEnv }; + +export type RigUpdatePlan = + | { + status: "ready"; + currentVersion: string; + latestVersion: string; + updateStep: RigUpdateStep; + versionStep: RigUpdateStep; + syncStep: RigUpdateStep; + } + | { status: "current"; version: string; syncStep: RigUpdateStep } + | { status: "skipped"; reason: string }; + +export type RigUpdateResult = + | { + status: "updated"; + previousVersion: string; + version: string; + step: RigUpdateStep; + output: string; + } + | { status: "current"; version: string } + | { status: "skipped"; reason: string }; + +export type RunnableRigUpdatePlan = Exclude; + +export type RigUpdaterParams = Readonly<{ packageRoot: string; currentVersion: string }>; + +export type RigInstallation = + | { kind: "npm" | "bun"; packageManager: string; prefix: string; rig: string } + | { reason: string }; + +export const RigPackageManifest = Schema.Struct({ version: Schema.String }); + +const RigUpdateOperation = Schema.Literals(["command", "plan", "update", "sync"]); + +export class RigUpdateError extends Schema.TaggedErrorClass()("RigUpdateError", { + operation: RigUpdateOperation, + cause: Schema.Defect(), +}) {} diff --git a/src/errors/RigError.ts b/src/errors/RigError.ts deleted file mode 100644 index b7b23ff..0000000 --- a/src/errors/RigError.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../providers/errors/index.ts"; diff --git a/src/errors/codes.ts b/src/errors/codes.ts deleted file mode 100644 index b7b23ff..0000000 --- a/src/errors/codes.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../providers/errors/index.ts"; diff --git a/src/generated/agent/agent-instruction-sync.ts b/src/generated/agent/agent-instruction-sync.ts deleted file mode 100644 index 9329425..0000000 --- a/src/generated/agent/agent-instruction-sync.ts +++ /dev/null @@ -1,537 +0,0 @@ -import { createHash } from "node:crypto"; -import { existsSync } from "node:fs"; -import { readFile, readdir, realpath, stat, writeFile } from "node:fs/promises"; -import { dirname, isAbsolute, join, resolve } from "node:path"; -import { RigAgentInstructions } from "../../agents/instructions"; -import { type ConfigOptions } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import { RigToolEntryFiles } from "../../registry/discover"; -import { ToolListServiceClass } from "../../tools/list"; -import { ToolApiMigrationServiceClass } from "../../tools/migration/tool-api-migration"; - -export type AgentInstructionSyncOptions = ConfigOptions & { - cwd?: string; -}; - -export type AgentInstructionTarget = { - path: string; - existed: boolean; - scope: "all" | "visible"; -}; - -export type AgentInstructionUpdate = AgentInstructionTarget & { - changed: boolean; -}; - -export type AgentInstructionSyncResult = { - skipped: boolean; - targets: AgentInstructionUpdate[]; -}; - -const StartMarker = ""; -const EndMarker = ""; -const IgnoreMarker = ""; - -type AgentSyncStamp = { - sourceFingerprint: string; - targetFingerprints: Record; -}; - -/* v8 ignore start */ -class AgentSyncFingerprintClass { - constructor(private readonly params: { paths: RigPathsClass }) {} - - async source(): Promise { - try { - const configSource = await readFile(this.params.paths.configPath, "utf8"); - const config = JSON.parse(configSource) as { - baseRegistryDir?: string; - customRegistries?: string[]; - }; - const registryPaths = [ - this.params.paths.resolve( - config.baseRegistryDir || this.params.paths.defaultBaseRegistryDir, - ), - ...(config.customRegistries ?? []).map((path) => this.params.paths.resolve(path)), - ]; - const sources = await Promise.all( - registryPaths.map(async (registryPath) => { - const entries = await this.directoryEntries(registryPath); - return Promise.all( - entries.flatMap((entry) => - RigToolEntryFiles.map(async (file) => { - const path = join(registryPath, entry, file); - try { - return `${path}\0${await readFile(path, "utf8")}`; - } catch { - return undefined; - } - }), - ), - ); - }), - ); - return this.hash([RigAgentInstructions, configSource, ...sources.flat(2).filter(Boolean)]); - } catch { - return undefined; - } - } - - async targets(targets: AgentInstructionTarget[]): Promise | undefined> { - try { - return Object.fromEntries( - await Promise.all( - targets.map(async (target) => [ - target.path, - this.hash([await readFile(target.path, "utf8")]), - ]), - ), - ); - } catch { - return undefined; - } - } - - private async directoryEntries(path: string): Promise { - try { - return (await readdir(path, { withFileTypes: true })) - .filter((entry) => entry.isDirectory()) - .map((entry) => entry.name) - .toSorted(); - } catch { - return []; - } - } - - private hash(values: Array): string { - const hash = createHash("sha256"); - for (const value of values) hash.update(value ?? "").update("\0"); - return hash.digest("hex"); - } -} -/* v8 ignore stop */ - -export const AgentInstructionSyncLocations = { - projectFiles: ["AGENTS.md", "CLAUDE.md"], - projectClaudeDirectories: [".claude"], - homeFiles: [ - [".agents", "AGENTS.md"], - [".pi", "agent", "AGENTS.md"], - ], - homeClaudeDirectories: [[".claude"]], - openCodeConfigFiles: [ - [".config", "opencode", "opencode.json"], - [".opencode", "opencode.json"], - ], -} as const; - -export class AgentInstructionSyncServiceClass { - private readonly cwd: string; - private readonly paths: RigPathsClass; - private readonly listService: ToolListServiceClass; - private readonly migrationService: ToolApiMigrationServiceClass; - private readonly fingerprint: AgentSyncFingerprintClass; - - constructor(options: AgentInstructionSyncOptions = {}) { - this.cwd = resolve(options.cwd ?? process.cwd()); - this.paths = new RigPathsClass(options); - this.listService = new ToolListServiceClass(options); - this.migrationService = new ToolApiMigrationServiceClass(options, {}); - this.fingerprint = new AgentSyncFingerprintClass({ paths: this.paths }); - } - - async sync(): Promise { - if (process.env.RIG_AGENT_SYNC === "0") return { skipped: true, targets: [] }; - - const targets = await this.discoverTargets(); - if (targets.length === 0) return { skipped: false, targets: [] }; - - /* v8 ignore next 5 */ - if (await this.canSkipSync(targets)) { - return { - skipped: false, - targets: targets.map((target) => ({ ...target, changed: false })), - }; - } - - const migrationInstructions = this.migrationService.renderAgentInstructions({ - report: await this.migrationService.inspect(), - }); - const updates = await Promise.all( - targets.map(async (target) => { - /* v8 ignore next 3 */ - if (target.scope === "visible" && !(await this.projectHasRegistry())) { - return { ...target, changed: await this.removeManagedBlock(target) }; - } - const block = this.renderBlock(await this.renderToolList(target), migrationInstructions); - return { - ...target, - changed: await this.upsertManagedBlock(target, block), - }; - }), - ); - - await this.writeSyncStamp(targets); - return { skipped: false, targets: updates }; - } - - private get syncStampPath(): string { - return join(this.paths.rigDir, ".agent-sync-stamp"); - } - - /* v8 ignore start */ - private async canSkipSync(targets: AgentInstructionTarget[]): Promise { - try { - const stamp = JSON.parse(await readFile(this.syncStampPath, "utf8")) as AgentSyncStamp; - const [sourceFingerprint, targetFingerprints] = await Promise.all([ - this.fingerprint.source(), - this.fingerprint.targets(targets), - ]); - return ( - sourceFingerprint !== undefined && - targetFingerprints !== undefined && - stamp.sourceFingerprint === sourceFingerprint && - JSON.stringify(stamp.targetFingerprints) === JSON.stringify(targetFingerprints) - ); - } catch { - return false; - } - } - - private async writeSyncStamp(targets: AgentInstructionTarget[]): Promise { - try { - const [sourceFingerprint, targetFingerprints] = await Promise.all([ - this.fingerprint.source(), - this.fingerprint.targets(targets.map((target) => ({ ...target, existed: true }))), - ]); - if (!sourceFingerprint || !targetFingerprints) return; - await writeFile( - this.syncStampPath, - `${JSON.stringify({ sourceFingerprint, targetFingerprints } satisfies AgentSyncStamp)}\n`, - "utf8", - ); - } catch { - // non-critical - } - } - /* v8 ignore stop */ - - async discoverTargets(): Promise { - const targets = new Map(); - - await Promise.all( - this.projectDirectories().flatMap((directory) => [ - ...AgentInstructionSyncLocations.projectFiles.map((file) => - this.addExistingFile(targets, join(directory, file), "visible"), - ), - ...AgentInstructionSyncLocations.projectClaudeDirectories.map((directoryName) => - this.addClaudeDirectoryTarget(targets, join(directory, directoryName), "visible"), - ), - ]), - ); - - await Promise.all([ - ...AgentInstructionSyncLocations.homeClaudeDirectories.map((path) => - this.addClaudeDirectoryTarget(targets, join(this.paths.homeDir, ...path), "all"), - ), - ...AgentInstructionSyncLocations.homeFiles.map((path) => - this.addExistingFile(targets, join(this.paths.homeDir, ...path), "all"), - ), - this.addOpenCodeInstructionTargets(targets), - ]); - - return [...targets.values()].toSorted((left, right) => left.path.localeCompare(right.path)); - } - - renderBlock(toolList: string, migrationInstructions = ""): string { - return `${StartMarker} - -## Rig local tools - -${RigAgentInstructions}${migrationInstructions ? `${migrationInstructions}\n\n` : ""} -### Available Rig tools - -\`\`\`text -${toolList} -\`\`\` - -${EndMarker}`; - } - - private async renderToolList(target: AgentInstructionTarget): Promise { - const options = target.scope === "visible" ? { visibleFromPath: target.path } : {}; - return this.listService.renderPlain(await this.listService.list(options)); - } - - private async addExistingFile( - targets: Map, - path: string, - scope: AgentInstructionTarget["scope"], - ): Promise { - if ((await this.isFile(path)) && !(await this.isIgnored(path))) { - await this.setTarget(targets, path, true, scope); - } - } - - private async addClaudeDirectoryTarget( - targets: Map, - directory: string, - scope: AgentInstructionTarget["scope"], - ): Promise { - const path = join(directory, "CLAUDE.md"); - const fileExists = await this.isFile(path); - if (fileExists && (await this.isIgnored(path))) return; - const directoryExists = fileExists ? false : await this.isDirectory(directory); - if (fileExists || directoryExists) await this.setTarget(targets, path, fileExists, scope); - } - - private async addOpenCodeInstructionTargets( - targets: Map, - ): Promise { - await Promise.all( - AgentInstructionSyncLocations.openCodeConfigFiles.map((path) => - this.addOpenCodeConfigTargets(targets, join(this.paths.homeDir, ...path)), - ), - ); - } - - private async addOpenCodeConfigTargets( - targets: Map, - configPath: string, - ): Promise { - let parsed: unknown; - try { - parsed = JSON.parse(await this.readText(configPath)); - } catch { - return; - } - - if (!this.isRecord(parsed) || !Array.isArray(parsed.instructions)) return; - - await Promise.all( - parsed.instructions.map((instruction) => { - if (typeof instruction !== "string") return Promise.resolve(); - const instructionPath = this.resolveOpenCodeInstructionPath( - instruction, - dirname(configPath), - ); - return this.addExistingFile( - targets, - instructionPath, - this.instructionScope(instructionPath), - ); - }), - ); - } - - private resolveOpenCodeInstructionPath(path: string, configDir: string): string { - if (path === "~" || path.startsWith("~/")) return this.paths.resolve(path); - return isAbsolute(path) ? path : resolve(configDir, path); - } - - private instructionScope(path: string): AgentInstructionTarget["scope"] { - let current = dirname(path); - const home = resolve(this.paths.homeDir); - - while (true) { - if (existsSync(join(current, ".git")) || existsSync(join(current, "package.json"))) { - return "visible"; - } - - if (current === home) return "all"; - const parent = dirname(current); - if (parent === current) return "visible"; - current = parent; - } - } - - private isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); - } - - private async setTarget( - targets: Map, - path: string, - existed: boolean, - scope: AgentInstructionTarget["scope"], - ): Promise { - const key = existed ? await this.realPath(path) : path; - const current = targets.get(key); - const nextScope = current?.scope === "all" || scope === "all" ? "all" : "visible"; - if (!current || path < current.path) targets.set(key, { path, existed, scope: nextScope }); - else current.scope = nextScope; - } - - private async upsertManagedBlock( - target: AgentInstructionTarget, - block: string, - ): Promise { - const existing = target.existed ? await this.readText(target.path) : ""; - const nextBody = this.managedBlockPattern().test(existing) - ? existing.replace(this.managedBlockPattern(), block) - : this.appendBlock(existing, block); - const next = `${nextBody.trimEnd()}\n`; - - if (next === existing) return false; - - await this.writeText(target.path, next); - return true; - } - - /* v8 ignore next 15 */ - private async projectHasRegistry(): Promise { - try { - const config = JSON.parse(await readFile(this.paths.configPath, "utf8")) as { - baseRegistryDir?: string; - customRegistries?: string[]; - }; - const projectRoot = await this.safeRealPath(this.projectRoot()); - const prefix = `${projectRoot}/`; - const registries = [ - this.paths.resolve(config.baseRegistryDir ?? ""), - ...(config.customRegistries ?? []).map((path) => this.paths.resolve(path)), - ]; - const resolved = await Promise.all(registries.map((path) => this.safeRealPath(path))); - return resolved.some((path) => path === projectRoot || path.startsWith(prefix)); - } catch { - return false; - } - } - - /* v8 ignore next 12 */ - private async removeManagedBlock(target: AgentInstructionTarget): Promise { - if (!target.existed) return false; - const existing = await this.readText(target.path); - if (!this.managedBlockPattern().test(existing)) return false; - const next = existing - .replace(this.managedBlockPattern(), "") - .replace(/\n{3,}/g, "\n\n") - .trim(); - if (next === existing.trim()) return false; - await this.writeText(target.path, next ? `${next}\n` : ""); - return true; - } - - private async readText(path: string): Promise { - const bun = this.bunFileApi(); - /* v8 ignore next */ - if (bun) return bun.file(path).text(); - return readFile(path, "utf8"); - } - - private async writeText(path: string, content: string): Promise { - const bun = this.bunFileApi(); - /* v8 ignore next 4 */ - if (bun) { - await bun.write(path, content); - return; - } - await writeFile(path, content, "utf8"); - } - - private bunFileApi(): - | { - file(path: string): { text(): Promise }; - write(path: string, content: string): Promise; - } - | undefined { - const candidate = ( - globalThis as typeof globalThis & { - Bun?: { file?: unknown; write?: unknown }; - } - ).Bun; - /* v8 ignore next */ - if (typeof candidate?.file === "function" && typeof candidate.write === "function") - return candidate as never; - return undefined; - } - - private appendBlock(existing: string, block: string): string { - return [existing.trimEnd(), block].filter(Boolean).join("\n\n"); - } - - private managedBlockPattern(): RegExp { - return new RegExp(`${this.escapeRegExp(StartMarker)}[\\s\\S]*?${this.escapeRegExp(EndMarker)}`); - } - - private escapeRegExp(value: string): string { - return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - } - - private async isIgnored(path: string): Promise { - try { - return (await this.readText(path)).includes(IgnoreMarker); - } catch { - return false; - } - } - - private async realPath(path: string): Promise { - try { - return await realpath(path); - } catch { - return path; - } - } - - private async safeRealPath(path: string): Promise { - try { - return await realpath(path); - } catch { - /* v8 ignore next -- unresolved registry paths are compared in normalized form */ - return path; - } - } - - private async isFile(path: string): Promise { - try { - return (await stat(path)).isFile(); - } catch { - return false; - } - } - - private async isDirectory(path: string): Promise { - try { - return (await stat(path)).isDirectory(); - } catch { - return false; - } - } - - private projectDirectories(): string[] { - const root = this.projectRoot(); - const directories: string[] = []; - let current = this.cwd; - - while (true) { - directories.push(current); - if (current === root) return directories; - current = dirname(current); - } - } - - private projectRoot(): string { - const start = this.cwd; - let current = start; - let packageRoot: string | undefined; - - while (true) { - if (this.isGitRoot(current)) return current; - if (!packageRoot && this.isPackageRoot(current)) packageRoot = current; - - const parent = dirname(current); - if (parent === current) return packageRoot ?? start; - - current = parent; - } - } - - private isGitRoot(directory: string): boolean { - return existsSync(join(directory, ".git")); - } - - private isPackageRoot(directory: string): boolean { - return existsSync(join(directory, "package.json")); - } -} diff --git a/src/generated/runtime/runtime-support.ts b/src/generated/runtime/runtime-support.ts deleted file mode 100644 index a4dcf46..0000000 --- a/src/generated/runtime/runtime-support.ts +++ /dev/null @@ -1,604 +0,0 @@ -import { existsSync } from "node:fs"; -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { createRequire } from "node:module"; -import { dirname, join } from "node:path"; -import { defineService } from "../../define"; -import { RigPathsClass, type PathOptions } from "../../config/paths"; - -type BunFileApi = { - file(path: string): { - exists(): Promise; - text(): Promise; - }; - write(path: string, content: string): Promise; -}; - -const requireFromRuntimeSupport = createRequire(import.meta.url); - -function bunFileApi(_params: {}): BunFileApi | undefined { - const candidate = (globalThis as typeof globalThis & { Bun?: Partial }).Bun; - /* v8 ignore next 3 */ - if (typeof candidate?.file === "function" && typeof candidate.write === "function") { - return candidate as BunFileApi; - } - return undefined; -} - -const RuntimeSupportProductionDeps = { - createPaths(options: PathOptions) { - return new RigPathsClass(options); - }, - mkdir, - readFile, - writeFile, - exists: existsSync, - join, - dirname, - bunFileApi, - resolvePackage(specifier: string) { - return requireFromRuntimeSupport.resolve(`${specifier}/package.json`); - }, - resolveImport(specifier: string) { - const meta = import.meta as ImportMeta & { resolve?: (id: string) => string }; - /* v8 ignore next */ - if (!meta.resolve) return specifier; - return meta.resolve(specifier); - }, -}; - -export class RuntimeSupportService extends defineService({ - params: {} as PathOptions, - deps: RuntimeSupportProductionDeps, -}) { - private readonly paths = this.deps.createPaths(this.params); - - private async readTextIfExists(params: { path: string }): Promise { - const bun = this.deps.bunFileApi({}); - /* v8 ignore next 5 */ - if (bun) { - const file = bun.file(params.path); - if (!(await file.exists())) return undefined; - return await file.text(); - } - if (!this.deps.exists(params.path)) return undefined; - return await this.deps.readFile(params.path, "utf8"); - } - - private async writeTextIfChanged(params: { path: string; content: string }): Promise { - if ((await this.readTextIfExists({ path: params.path })) === params.content) return; - const bun = this.deps.bunFileApi({}); - /* v8 ignore next 4 */ - if (bun) { - await bun.write(params.path, params.content); - return; - } - await this.deps.writeFile(params.path, params.content, "utf8"); - } - - private async ensureRegistryTsconfig(params: { registryDir: string }): Promise { - await this.deps.mkdir(params.registryDir, { recursive: true }); - const tsconfigPath = this.deps.join(params.registryDir, "tsconfig.json"); - const content = runtimeRegistryTsconfigSource({ - paths: this.paths, - dirname: this.deps.dirname, - resolvePackage: this.deps.resolvePackage, - }); - const existing = await this.readTextIfExists({ path: tsconfigPath }); - if (existing !== undefined && !existing.startsWith("// Generated by Rig.")) return; - await this.writeTextIfChanged({ path: tsconfigPath, content }); - } - - public async ensure(params: { registryDirs: string[] }): Promise { - await this.deps.mkdir(this.paths.runtimeDir, { recursive: true }); - await Promise.all([ - this.writeTextIfChanged({ - path: this.paths.runtimeSdkPath, - content: runtimeSdkSource({ zodImport: this.deps.resolveImport("zod") }), - }), - this.writeTextIfChanged({ - path: this.paths.runtimeTypesPath, - content: runtimeTypesSource({}), - }), - this.writeTextIfChanged({ - path: this.paths.runtimeGlobalsPath, - content: runtimeGlobalsSource({}), - }), - ]); - await Promise.all( - params.registryDirs.map((registryDir) => this.ensureRegistryTsconfig({ registryDir })), - ); - } -} - -export type RuntimeSupportClass = { ensure(registryDirs?: string[]): Promise }; - -type RuntimeSupportConstructor = { - new (options?: PathOptions): RuntimeSupportClass; - readonly prototype: RuntimeSupportClass; -}; - -type RuntimeSupportAdapter = RuntimeSupportClass & { - readonly resource: RuntimeSupportService; -}; - -const RuntimeSupportClassAdapter = function constructRuntimeSupport( - this: RuntimeSupportAdapter, - options: PathOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new RuntimeSupportService({ params: options, deps: RuntimeSupportProductionDeps }), - }); -}; -Object.defineProperty(RuntimeSupportClassAdapter, "name", { value: "RuntimeSupportClass" }); -Object.defineProperty(RuntimeSupportClassAdapter.prototype, "ensure", { - configurable: true, - value: function ensure(this: RuntimeSupportAdapter, registryDirs: string[] = []) { - return this.resource.ensure({ registryDirs }); - }, - writable: true, -}); - -export const RuntimeSupportClass = - RuntimeSupportClassAdapter as unknown as RuntimeSupportConstructor; - -function runtimeSdkSource(params: { zodImport: string }): string { - return `// Generated by Rig. Safe to overwrite. -// @ts-nocheck -import { spawn } from "node:child_process"; -import { existsSync, statSync } from "node:fs"; -import { mkdir } from "node:fs/promises"; -import { homedir } from "node:os"; -import { dirname, isAbsolute, join, resolve } from "node:path"; -import type { z as zodNamespace } from "zod"; -import type { RigToolDefinition, RigToolFactory, RigToolKit } from "./types"; -// @ts-ignore file URL imports are emitted so generated tools can load Rig's bundled Zod. -import * as zodModule from ${JSON.stringify(params.zodImport)}; - -export const z: typeof zodNamespace = (zodModule as any).z ?? zodModule; - -class RigPathRuntimeClass { - home() { return homedir(); } - resolve(cwd, pathValue) { - if (pathValue === "~") return homedir(); - if (pathValue.startsWith("~/")) return join(homedir(), pathValue.slice(2)); - return isAbsolute(pathValue) ? pathValue : resolve(cwd, pathValue); - } - async ensureParent(pathValue) { await mkdir(dirname(pathValue), { recursive: true }); } - size(pathValue) { return existsSync(pathValue) ? statSync(pathValue).size : 0; } -} - -class RigArgsRuntimeClass { - args = []; - raw(...values) { this.args.push(...values); return this; } - flag(name, enabled = true) { if (enabled) this.args.push(name); return this; } - value(name, value) { if (value !== undefined && value !== null) this.args.push(name, String(value)); return this; } - values(name, values) { for (const value of values ?? []) this.args.push(name, value); return this; } - toArray() { return [...this.args]; } -} - -class RigBoundedOutputCollectorClass { - chunks = []; - capturedBytes = 0; - truncated = false; - constructor(maxBytes) { this.maxBytes = maxBytes; } - capture(buffer) { - const remaining = Math.max(0, this.maxBytes - this.capturedBytes); - if (remaining > 0) { - const captured = buffer.subarray(0, remaining); - this.chunks.push(captured); - this.capturedBytes += captured.byteLength; - } - if (buffer.byteLength > remaining) this.truncated = true; - } - result() { - const content = this.decodeUtf8Prefix(Buffer.concat(this.chunks, this.capturedBytes)); - return this.truncated ? content + "\\n[rig: output truncated]" : content; - } - decodeUtf8Prefix(buffer) { - const strictDecoder = new TextDecoder("utf-8", { fatal: true }); - for (let removed = 0; removed <= Math.min(3, buffer.byteLength); removed++) { - try { return strictDecoder.decode(buffer.subarray(0, buffer.byteLength - removed)); } - catch {} - } - return new TextDecoder().decode(buffer); - } -} - -class RigManagedProcessClass { - processGroup = process.platform !== "win32"; - timedOut = false; - constructor(child, command, timeoutMs) { - this.child = child; - this.command = command; - this.timeoutMs = timeoutMs; - } - async wait() { - this.child.once("error", (error) => { this.spawnError = error; }); - this.timeout = setTimeout(() => this.terminate(), this.timeoutMs); - try { - const exitCode = await new Promise((resolve) => this.child.once("close", (code) => resolve(code ?? 1))); - if (this.spawnError) throw new Error("Command could not start: " + this.command[0] + ": " + this.spawnError.message); - if (this.timedOut) throw new Error("Command timed out after " + this.timeoutMs + "ms."); - return exitCode; - } finally { - if (this.timeout) clearTimeout(this.timeout); - if (this.escalation) clearTimeout(this.escalation); - } - } - terminate() { - this.timedOut = true; - this.signal("SIGTERM"); - this.escalation = setTimeout(() => this.signal("SIGKILL"), 100); - } - signal(signal) { - try { - if (this.processGroup && this.child.pid) process.kill(-this.child.pid, signal); - else this.child.kill(signal); - } catch {} - } -} - -class RigShellRuntimeClass { - async $(strings, ...values) { return this.bash(this.renderTemplateCommand(strings, values)); } - async bash(command, options = {}) { return this.runProcess(["bash", "-lc", command], options, [command]); } - async exec(args, options = {}) { - if (!Array.isArray(args) || args.length === 0 || args.some((arg) => typeof arg !== "string" || arg.length === 0)) { - throw new Error("shell.exec expects a non-empty array of command arguments."); - } - return this.runProcess(args, options); - } - async json(args, options = {}) { - const result = await this.exec(args, options); - if (result.exitCode !== 0) throw new Error("Command failed before JSON could be parsed."); - return JSON.parse(result.stdout); - } - async runProcess(args, options = {}, reportedCommand = args) { - const timeoutMs = options.timeoutMs ?? 30000; - const maxOutputBytes = options.maxOutputBytes ?? 1048576; - const child = spawn(args[0], args.slice(1), { - cwd: options.cwd ?? process.cwd(), - env: { ...process.env, ...options.env }, - stdio: ["ignore", "pipe", "pipe"], - detached: process.platform !== "win32", - }); - const stdout = new RigBoundedOutputCollectorClass(maxOutputBytes); - const stderr = new RigBoundedOutputCollectorClass(maxOutputBytes); - child.stdout.on("data", (chunk) => stdout.capture(chunk)); - child.stderr.on("data", (chunk) => stderr.capture(chunk)); - const exitCode = await new RigManagedProcessClass(child, reportedCommand, timeoutMs).wait(); - return { command: reportedCommand, stdout: stdout.result(), stderr: stderr.result(), exitCode }; - } - renderTemplateCommand(strings, values) { - return strings.reduce((command, part, index) => command + part + (index < values.length ? this.renderTemplateValue(values[index]) : ""), ""); - } - renderTemplateValue(value) { - if (Array.isArray(value)) return value.map((item) => this.shellQuote(String(item))).join(" "); - return this.shellQuote(String(value)); - } - shellQuote(value) { - if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(value)) return value; - return "'" + value.replaceAll("'", "'\\''") + "'"; - } -} - -class RigCommandRunnerRuntimeClass { - async run(options) { - const id = options.tool ? options.tool + "." + options.command : options.command; - if (!/^[A-Za-z0-9_-]+[.][A-Za-z0-9_-]+$/.test(id)) throw new Error("Command id must use .: " + id); - if (options.input !== undefined && options.args?.length) throw new Error("Use input or args, not both."); - - const args = ["rig", "run", id]; - if (options.input !== undefined) args.push("--input", JSON.stringify(options.input)); - if (options.dryRun) args.push("--dry-run"); - args.push(...(options.args ?? [])); - - const result = await new RigShellRuntimeClass().exec(args); - if (result.exitCode !== 0) throw new Error("Rig tool command failed: " + id + "\\n" + result.stderr); - - const envelope = JSON.parse(result.stdout); - if (envelope.errors?.length) throw new Error("Rig tool command failed: " + id + "\\n" + JSON.stringify(envelope.errors, null, 2)); - return envelope.data; - } -} - -const rigShell = new RigShellRuntimeClass(); - -export const rig: RigToolKit = { - z, - defineTool: (definition) => typeof definition.commands === "function" - ? { ...definition, commands: definition.commands((command) => command) } - : definition, - defineCommand: (definition) => definition, - run: (options) => new RigCommandRunnerRuntimeClass().run(options), - $: (strings, ...values) => rigShell.$(strings, ...values), - args: () => new RigArgsRuntimeClass(), - paths: new RigPathRuntimeClass(), - shell: rigShell, -} as unknown as RigToolKit; - -function defineToolRuntime(value: T): T { - return typeof value === "function" ? value : rig.defineTool(value as RigToolDefinition) as T; -} - -export class RigToolClass { - define(value: T): T { - return defineToolRuntime(value); - } -} - -export const RigTool = new RigToolClass(); -`; -} - -function runtimeTypesSource(_params: {}): string { - return `// Generated by Rig. Safe to overwrite. -import type { Database } from "bun:sqlite"; -import type { z } from "zod"; - -export type RigSchema = z.ZodTypeAny; - -export type RigCollectionDefinition = z.ZodObject> = { - schema?: T; - generateId?: (data: z.input) => string; -}; - -export type RigCollectionEntry> = { - id: string; - data: T; - body: string; - createdAt: string; - updatedAt: string; -}; - -export type RigCollectionHandle> = { - readonly name: string; - readonly path: string; - create(entry: { id?: string; data: T; body?: string }): Promise>; - getEntry(id: string): Promise | null>; - update(id: string, patch: { data?: Partial; body?: string }): Promise>; - upsert(entry: { id: string; data: T; body?: string }): Promise<{ id: string; created: boolean }>; - remove(id: string): Promise; - list(options?: { where?: Record; sort?: string; limit?: number; offset?: number }): Promise<{ entries: RigCollectionEntry[]; total: number }>; - search(query: string, options?: { limit?: number }): Promise<{ entries: Array & { snippet: string; rank: number }> }>; - count(where?: Record): Promise; - getCollection(filter?: (entry: RigCollectionEntry) => boolean): Promise[]>; - clear(): Promise; -}; - -export type RigToolCollectionDefinitions = Record; - -export type RigToolCollectionHandles = { - [Name in keyof Collections]: Collections[Name] extends RigCollectionDefinition - ? RigCollectionHandle> - : RigCollectionHandle; -}; - -export type RigShellOptions = { - cwd?: string; - env?: Record; - timeoutMs?: number; - maxOutputBytes?: number; -}; - -export type RigShellResult = { - command: string[]; - stdout: string; - stderr: string; - exitCode: number; -}; - -export type RigShell = { - $(strings: TemplateStringsArray, ...values: unknown[]): Promise; - exec(args: string[], options?: RigShellOptions): Promise; - bash(command: string, options?: RigShellOptions): Promise; - json(args: string[], options?: RigShellOptions): Promise; -}; - -export type RigPathHelper = { - home(): string; - resolve(cwd: string, pathValue: string): string; - ensureParent(pathValue: string): Promise; - size(pathValue: string): number; -}; - -export type RigArgBuilder = { - raw(...values: string[]): RigArgBuilder; - flag(name: string, enabled?: boolean): RigArgBuilder; - value(name: string, value: string | number | boolean | undefined | null): RigArgBuilder; - values(name: string, values: string[] | undefined): RigArgBuilder; - toArray(): string[]; -}; - -export type RigRunOptions = { - tool?: string; - command: string; - input?: unknown; - args?: string[]; - dryRun?: boolean; -}; - -export type RigToolDatabase = Database & { - readonly path: string; - migrate(version: number, name: string, sql: string): void; -}; - -export type RigToolKvStore = { - readonly path: string; - get(key: string): T | undefined; - set(key: string, value: unknown): void; -}; - -export type RigCacheKey = readonly unknown[]; - -export type RigCacheQueryOptions = { - queryKey: RigCacheKey; - queryFn: () => T | Promise; - /** Cached values younger than this duration return immediately; missing or stale values refresh before returning. */ - staleTime?: number; -}; - -export type RigToolCache = { - readonly path: string; - query(options: RigCacheQueryOptions): Promise; - peek(queryKey: RigCacheKey): T | undefined; - set(queryKey: RigCacheKey, value: T): void; - invalidate(queryKey: RigCacheKey): void; - remove(queryKey: RigCacheKey): void; - clear(): void; -}; - -export type RigLogMethod = { - (message: string): void; - (bindings: Record, message?: string): void; - (error: Error, message?: string): void; -}; - -export type RigToolLogger = { - trace: RigLogMethod; - debug: RigLogMethod; - info: RigLogMethod; - warn: RigLogMethod; - error: RigLogMethod; - fatal: RigLogMethod; - child(bindings: Record): RigToolLogger; -}; - -export type RigToolRunContext< - Input, - Env = unknown, - Collections = Record, -> = { - input: Input; - env: Env; - processEnv: NodeJS.ProcessEnv; - cwd: string; - db: RigToolDatabase; - kv: RigToolKvStore; - cache: RigToolCache; - log: RigToolLogger; - rig: RigToolKit; - collections: Collections; -}; - -export type RigToolExample = { - title: string; - text: string; - input?: Input; - output?: Output; -}; - -export type RigCommandDefinition< - Input extends RigSchema = RigSchema, - Output extends RigSchema = RigSchema, - Env = unknown, - Collections = Record, -> = { - description: string; - input: Input; - output: Output; - examples?: RigToolExample, z.output>[]; - run: (ctx: RigToolRunContext, Env, Collections>) => z.input | Promise>; -}; - -export type RigToolCommandBuilder< - Env, - Collections, -> = ( - definition: RigCommandDefinition, -) => RigCommandDefinition; - -export type RigToolCommandMap = Record>; - -export type RigToolCommandsFactory< - Env, - Collections, - Commands extends RigToolCommandMap, -> = (command: RigToolCommandBuilder) => Commands; - -export type RigToolDefinition< - Env extends RigSchema = RigSchema, - Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, - Commands extends RigToolCommandMap = RigToolCommandMap, -> = { - name?: string; - description: string; - env?: Env; - setupDb?: (db: RigToolDatabase) => void | Promise; - collections?: Collections; - commands: - | Commands - | RigToolCommandsFactory, RigToolCollectionHandles, Commands>; -}; - -export type RigNormalizedToolDefinition< - Env extends RigSchema = RigSchema, - Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, - Commands extends RigToolCommandMap = RigToolCommandMap, -> = Omit, "commands"> & { - commands: Commands; -}; - -export type RigToolKit = { - z: typeof z; - defineTool< - Env extends RigSchema = RigSchema, - Collections extends RigToolCollectionDefinitions = RigToolCollectionDefinitions, - const Commands extends RigToolCommandMap = RigToolCommandMap, - >(definition: RigToolDefinition): RigNormalizedToolDefinition; - defineCommand(definition: RigCommandDefinition): RigCommandDefinition; - run(options: RigRunOptions): Promise; - $(strings: TemplateStringsArray, ...values: unknown[]): Promise; - args(): RigArgBuilder; - paths: RigPathHelper; - shell: RigShell; -}; - -export type RigToolFactory = (rig: RigToolKit) => RigToolDefinition | Promise>; -`; -} - -function runtimeGlobalsSource(_params: {}): string { - return `// Generated by Rig. Safe to overwrite. -import type { RigToolFactory as RigToolFactoryType, RigToolKit as RigToolKitType } from "./types"; - -declare global { - type RigToolFactory = RigToolFactoryType; - type RigToolKit = RigToolKitType; -} - -export {}; -`; -} - -function runtimeRegistryTsconfigSource(params: { - paths: RigPathsClass; - dirname: typeof dirname; - resolvePackage: (specifier: string) => string; -}): string { - return `// Generated by Rig. Safe to overwrite. -${JSON.stringify( - { - compilerOptions: { - target: "ES2022", - module: "ESNext", - moduleResolution: "Bundler", - strict: true, - noEmit: true, - skipLibCheck: true, - allowImportingTsExtensions: true, - jsx: "preserve", - lib: ["ES2023", "DOM"], - types: ["bun"], - typeRoots: [params.dirname(params.dirname(params.resolvePackage("@types/bun")))], - paths: { - zod: [join(params.dirname(params.resolvePackage("zod")), "index.d.ts")], - }, - }, - files: [params.paths.runtimeGlobalsPath, params.paths.runtimeTypesPath], - include: ["./*/index.rig.ts", "./*/index.rig.tsx"], - }, - null, - 2, -)} -`; -} diff --git a/src/generated/tool/tool-runtime-instruction-sync.ts b/src/generated/tool/tool-runtime-instruction-sync.ts deleted file mode 100644 index 497094e..0000000 --- a/src/generated/tool/tool-runtime-instruction-sync.ts +++ /dev/null @@ -1,274 +0,0 @@ -import { readFile, writeFile } from "node:fs/promises"; -import { defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { ToolDiscoveryServiceClass } from "../../registry/discover"; -import { CurrentRigToolApiVersion } from "../../tools/domain/tool-api"; - -type BunFileApi = { - file(path: string): { text(): Promise }; - write(path: string, content: string): Promise; -}; - -export type ToolRuntimeInstructionUpdate = { - name: string; - path: string; - changed: boolean; -}; - -export type ToolRuntimeInstructionSyncResult = { - tools: ToolRuntimeInstructionUpdate[]; -}; - -const StartMarker = "// rig:runtime-reference:start"; -const EndMarker = "// rig:runtime-reference:end"; -const ToolApiVersionMarker = "// rig:tool-api-version"; - -const RuntimeReference = `Generated by Rig. This block is refreshed automatically. - -Rig tool runtime: -- Tool API v2 uses a direct factory export: \`export default (rig: RigToolKit) => rig.defineTool({ ... });\`. -- Rig derives the tool name from its folder. The \`name\` property remains supported for v1 tools. -- \`rig.z\` is Zod. Docs: https://zod.dev/ -- \`rig.defineTool(definition)\` defines the tool. -- Define \`commands\` as \`(command) => ({ ... })\`, then define each command with the bound \`command({ description, input, output, run })\` helper. \`rig.defineCommand\` remains supported for v1 tools. -- Use \`rig.z.object({ ... })\` for input/output schemas. -- Tools run under Bun with fallback auto-install enabled, so tool files can import npm packages; add explicit package versions when reproducibility matters. Example: \`import sharp from "sharp@0.34.4";\`. -- \`rig.run({ command: "tool.command", input })\` runs another registered Rig command and returns its data. -- \`rig.$\` runs a Bun Shell tagged template command with escaped interpolations. Docs: https://bun.com/docs/runtime/shell -- \`rig.args()\` builds argv arrays with \`.raw()\`, \`.flag()\`, \`.value()\`, \`.values()\`, and \`.toArray()\`. -- \`rig.paths.home()\`, \`rig.paths.resolve(cwd, path)\`, \`rig.paths.ensureParent(path)\`, and \`rig.paths.size(path)\` cover common path work. -- Add \`env: rig.z.object({ KEY: rig.z.string() })\` to validate local secrets or settings and receive parsed values on \`context.env\`. -- Run \`rig env KEY=VALUE\` to write the tool \`.env\` file; run \`rig env remove KEY\` to remove a value. -- To add another registry for tool files, run \`rig registry create [path]\` (defaults to current directory). - -Tool logging: -- Use \`context.log.info(...)\`, \`context.log.warn(...)\`, or another Pino-style method for structured logs with a default \`tool:.\` prefix. -- Rig writes app and tool logs to \`~/rig/.logs\`, rolls files by size, and keeps logs for seven days by default. - -Tool key-value state: -- Use \`context.kv.set(key, value)\` and \`context.kv.get(key)\` for lightweight JSON state stored beside the tool entry file as \`kv.sqlite\`. - -Tool query cache: -- Use \`await context.cache.query({ queryKey, queryFn, staleTime })\` for JSON-compatible derived data stored beside the tool entry file as \`cache.sqlite\`. -- Query keys are non-empty JSON-compatible arrays. Missing and stale data wait for \`queryFn\` and return its refreshed value; fresh data returns immediately. -- Use \`context.cache.peek(queryKey)\`, \`.set(queryKey, value)\`, \`.invalidate(queryKey)\`, \`.remove(queryKey)\`, and \`.clear()\` for explicit cache control. - -Tool SQLite database: -- Add \`setupDb: (db) => { db.migrate(1, "create table", "create table ..."); }\` when a tool needs persistent state. -- Rig stores the database beside the tool entry file as \`index.sqlite\` and runs \`setupDb\` before every command. -- \`db\` is Bun SQLite with \`strict: true\`, WAL mode, and an extra \`db.migrate(version, name, sql)\` helper. Docs: https://bun.com/docs/runtime/sqlite - -Tool collections: -- Add \`collections: { name: { schema: rig.z.object({ ... }) } }\` to the tool definition to declare document stores. -- Each collection is a directory of \`.md\` files (beside the tool entry file) with validated YAML frontmatter and a markdown body. -- Access via \`context.collections.\` in command run functions. -- API: \`.create({ id, data, body })\`, \`.getEntry(id)\`, \`.update(id, { data, body })\`, \`.upsert({ id, data, body })\`, \`.remove(id)\` -- Query: \`.list({ where, sort, limit })\`, \`.search(query, { limit })\`, \`.count(where)\`, \`.getCollection(filterFn)\`, \`.clear()\` -- A hidden \`.index.sqlite\` with FTS5 is auto-managed for instant full-text search and structured queries on frontmatter fields. -- Files on disk are the source of truth; hand-edits are reconciled on next access. -- Optional \`generateId: (data) => string\` derives the filename from entry data; otherwise pass \`id\` explicitly. -- Schema-less collections (no \`schema\` key) are allowed; FTS still works, no validation on write. - -Command run context: -- \`context.input\` is already validated by the input schema. -- \`context.env\` is already validated by the tool env schema when defined. -- \`context.processEnv\` is the process environment. -- \`context.cwd\` is the current working directory. -- \`context.db\` is available when the tool defines \`setupDb\`. -- \`context.kv\` is always available for lightweight JSON key-value state. -- \`context.cache\` is always available for persistent query data with explicit foreground refreshes. -- \`context.log\` is a structured Pino logger for this tool command. -- \`context.rig\` is the toolkit (same as the factory \`rig\` arg, useful for nested tool calls). -- \`context.collections\` provides handles for each declared collection (empty object if none defined). -- Return a value that matches the output schema. -`; - -function escapeRegExp(params: { value: string }): string { - return params.value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); -} - -function managedBlockPattern(_params: {}): RegExp { - return new RegExp( - `${escapeRegExp({ value: StartMarker })}[\\s\\S]*?${escapeRegExp({ value: EndMarker })}\\n*`, - ); -} - -function sourceVersion(params: { source: string }): number { - const match = /^\/\/ rig:tool-api-version (\d+)\s*$/m.exec(params.source); - if (!match) return 1; - const version = Number.parseInt(match[1]!, 10); - return Number.isSafeInteger(version) && version > 0 ? version : 1; -} - -function renderRuntimePrefix(params: { version?: number }): string { - const version = params.version ?? CurrentRigToolApiVersion; - return [ - StartMarker, - `${ToolApiVersionMarker} ${version}`, - ...RuntimeReference.trimEnd() - .split("\n") - .map(function commentLine(line) { - return line ? `// ${line}` : "//"; - }), - EndMarker, - ].join("\n"); -} - -function upsertRuntimePrefix(params: { source: string }): string { - const version = sourceVersion(params); - const withoutExisting = params.source - .replace(managedBlockPattern({}), "") - .replace(/^\/\/ rig:tool-api-version \d+\s*$/gm, "") - .trimStart(); - const shebang = withoutExisting.match(/^(#!.*\n)/); - const prefix = renderRuntimePrefix({ version }); - const body = shebang ? withoutExisting.slice(shebang[0].length).trimStart() : withoutExisting; - const next = [prefix, body.trimEnd()].filter(Boolean).join("\n\n"); - return `${shebang?.[0] ?? ""}${next}\n`; -} - -export const ToolRuntimeInstructionSingleton = defineSingleton({ - params: {}, - deps: {}, - renderPrefix: renderRuntimePrefix, - upsertPrefix: upsertRuntimePrefix, -}); - -function bunFileApi(_params: {}): BunFileApi | undefined { - const candidate = (globalThis as typeof globalThis & { Bun?: Partial }).Bun; - /* v8 ignore next 3 */ - if (typeof candidate?.file === "function" && typeof candidate.write === "function") { - return candidate as BunFileApi; - } - return undefined; -} - -async function readText(params: { path: string }): Promise { - const bun = bunFileApi({}); - /* v8 ignore next */ - if (bun) return await bun.file(params.path).text(); - return await readFile(params.path, "utf8"); -} - -async function writeText(params: { path: string; content: string }): Promise { - const bun = bunFileApi({}); - /* v8 ignore next 4 */ - if (bun) { - await bun.write(params.path, params.content); - return; - } - await writeFile(params.path, params.content, "utf8"); -} - -type ToolRuntimeInstructionSyncDeps = { - discover: () => ReturnType; - readText: typeof readText; - writeText: typeof writeText; -}; - -function createToolRuntimeInstructionSyncDeps( - options: ConfigOptions, -): ToolRuntimeInstructionSyncDeps { - const discovery = new ToolDiscoveryServiceClass(options); - return { - discover() { - return discovery.discover(); - }, - readText, - writeText, - }; -} - -const ToolRuntimeInstructionSyncProductionDeps = createToolRuntimeInstructionSyncDeps({}); - -export class ToolRuntimeInstructionSyncService extends defineService({ - params: {}, - deps: ToolRuntimeInstructionSyncProductionDeps, -}) { - private async updateFile(params: { path: string }): Promise { - const existing = await this.deps.readText(params); - const next = ToolRuntimeInstructionSingleton.upsertPrefix({ source: existing }); - if (next === existing) return false; - await this.deps.writeText({ path: params.path, content: next }); - return true; - } - - public async sync(_params: {}): Promise { - const tools = await this.deps.discover(); - const updates = await Promise.all( - tools.map(async (tool) => { - return { - name: tool.name, - path: tool.toolPath, - changed: await this.updateFile({ path: tool.toolPath }), - }; - }), - ); - return { tools: updates }; - } - - public renderPrefix(params: { version?: number }): string { - return renderRuntimePrefix(params); - } - - public upsertPrefix(params: { source: string }): string { - return upsertRuntimePrefix(params); - } -} - -export type ToolRuntimeInstructionSyncServiceClass = { - sync(): Promise; - renderPrefix(version?: number): string; - upsertPrefix(source: string): string; -}; - -type ToolRuntimeInstructionSyncServiceConstructor = { - new (options?: ConfigOptions): ToolRuntimeInstructionSyncServiceClass; - readonly prototype: ToolRuntimeInstructionSyncServiceClass; -}; - -type ToolRuntimeInstructionSyncServiceAdapter = ToolRuntimeInstructionSyncServiceClass & { - readonly resource: ToolRuntimeInstructionSyncService; -}; - -const ToolRuntimeInstructionSyncServiceClassAdapter = - function constructToolRuntimeInstructionSyncService( - this: ToolRuntimeInstructionSyncServiceAdapter, - options: ConfigOptions = {}, - ): void { - Object.defineProperty(this, "resource", { - value: new ToolRuntimeInstructionSyncService({ - params: {}, - deps: createToolRuntimeInstructionSyncDeps(options), - }), - }); - }; -Object.defineProperty(ToolRuntimeInstructionSyncServiceClassAdapter, "name", { - value: "ToolRuntimeInstructionSyncServiceClass", -}); -Object.defineProperties(ToolRuntimeInstructionSyncServiceClassAdapter.prototype, { - sync: { - configurable: true, - value: function sync(this: ToolRuntimeInstructionSyncServiceAdapter) { - return this.resource.sync({}); - }, - writable: true, - }, - renderPrefix: { - configurable: true, - value: function renderPrefix(this: ToolRuntimeInstructionSyncServiceAdapter, version?: number) { - return this.resource.renderPrefix({ version }); - }, - writable: true, - }, - upsertPrefix: { - configurable: true, - value: function upsertPrefix(this: ToolRuntimeInstructionSyncServiceAdapter, source: string) { - return this.resource.upsertPrefix({ source }); - }, - writable: true, - }, -}); - -export const ToolRuntimeInstructionSyncServiceClass = - ToolRuntimeInstructionSyncServiceClassAdapter as unknown as ToolRuntimeInstructionSyncServiceConstructor; diff --git a/src/persistence/cache/tool-cache.ts b/src/persistence/cache/tool-cache.ts deleted file mode 100644 index dcf4f33..0000000 --- a/src/persistence/cache/tool-cache.ts +++ /dev/null @@ -1,423 +0,0 @@ -import type { Database } from "bun:sqlite"; -import { createHash } from "node:crypto"; -import { mkdirSync } from "node:fs"; -import { dirname, join } from "node:path"; -import { defineRepo, defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import type { - LoadedTool, - RigCacheKey, - RigCacheQueryOptions, - RigToolCache, - RigToolLogger, -} from "../../tools/types"; -import { BunSqliteModuleLoaderClass, type DatabaseConstructor } from "../sqlite/tool-database"; - -type CacheIdentity = { hash: string; json: string }; -type CacheRow = { - key_json: string; - value_json: string; - data_updated_at: number; - invalidated_at: number | null; -}; - -export type ManagedRigToolCache = RigToolCache & { close(): void }; - -function invalidCacheKeyValue(_params: {}): RigErrorClass { - return new RigErrorClass( - "INPUT_ERROR", - "context.cache query keys must contain only JSON-compatible values.", - ); -} - -function normalizeCacheKey(params: { - value: unknown; - parents: Set; - inArray: boolean; -}): unknown { - if ( - params.value === null || - typeof params.value === "string" || - typeof params.value === "boolean" - ) { - return params.value; - } - if (typeof params.value === "number") { - if (Number.isFinite(params.value)) return params.value; - throw invalidCacheKeyValue({}); - } - if (typeof params.value !== "object") { - if (params.value === undefined && !params.inArray) return undefined; - throw invalidCacheKeyValue({}); - } - if (params.parents.has(params.value)) { - throw new RigErrorClass("INPUT_ERROR", "context.cache query keys cannot contain cycles."); - } - - params.parents.add(params.value); - try { - if (Array.isArray(params.value)) { - return params.value.map(function normalizeArrayItem(item) { - return normalizeCacheKey({ value: item, parents: params.parents, inArray: true }); - }); - } - const prototype = Object.getPrototypeOf(params.value); - if (prototype !== Object.prototype && prototype !== null) throw invalidCacheKeyValue({}); - const normalized: Record = {}; - for (const key of Object.keys(params.value).toSorted()) { - const item = normalizeCacheKey({ - value: (params.value as Record)[key], - parents: params.parents, - inArray: false, - }); - if (item !== undefined) normalized[key] = item; - } - return normalized; - } finally { - params.parents.delete(params.value); - } -} - -function cacheKeyIdentity(params: { queryKey: RigCacheKey }): CacheIdentity { - if (!Array.isArray(params.queryKey) || params.queryKey.length === 0) { - throw new RigErrorClass("INPUT_ERROR", "context.cache query keys must be non-empty arrays."); - } - const json = JSON.stringify( - normalizeCacheKey({ value: params.queryKey, parents: new Set(), inArray: true }), - ); - return { hash: createHash("sha256").update(json).digest("hex"), json }; -} - -export const CacheKeyHasherSingleton = defineSingleton({ - params: {}, - deps: {}, - identity: cacheKeyIdentity, -}); - -function staleTime(params: { value: number | undefined }): number { - const value = params.value ?? 0; - if (typeof value !== "number" || Number.isNaN(value) || value < 0) { - throw new RigErrorClass( - "INPUT_ERROR", - "context.cache staleTime must be a non-negative number.", - ); - } - return value; -} - -function createSqliteToolCache(params: { - db: Database; - path: string; - now: () => number; -}): ManagedRigToolCache { - const inFlight = new Map>(); - - function read( - identity: CacheIdentity, - ): { data: T; updatedAt: number; invalidatedAt: number | null } | undefined { - const row = params.db - .query( - "select key_json, value_json, data_updated_at, invalidated_at from _rig_cache where key_hash = ?", - ) - .get(identity.hash) as CacheRow | null; - if (!row) return undefined; - if (row.key_json !== identity.json) { - throw new RigErrorClass( - "TOOL_INVALID", - "context.cache detected a query key hash collision.", - { - path: params.path, - }, - ); - } - params.db - .query("update _rig_cache set last_accessed_at = $lastAccessedAt where key_hash = $keyHash") - .run({ keyHash: identity.hash, lastAccessedAt: params.now() }); - return { - data: JSON.parse(row.value_json) as T, - updatedAt: row.data_updated_at, - invalidatedAt: row.invalidated_at, - }; - } - - function write(identity: CacheIdentity, value: unknown): void { - const valueJson = JSON.stringify(value); - if (valueJson === undefined) { - throw new RigErrorClass("INPUT_ERROR", "context.cache cannot store undefined values.", { - queryKey: identity.json, - }); - } - const timestamp = params.now(); - params.db - .query( - `insert into _rig_cache ( - key_hash, key_json, value_json, data_updated_at, invalidated_at, last_accessed_at - ) values ($keyHash, $keyJson, $valueJson, $dataUpdatedAt, null, $lastAccessedAt) - on conflict(key_hash) do update set - key_json = excluded.key_json, - value_json = excluded.value_json, - data_updated_at = excluded.data_updated_at, - invalidated_at = null, - last_accessed_at = excluded.last_accessed_at`, - ) - .run({ - keyHash: identity.hash, - keyJson: identity.json, - valueJson, - dataUpdatedAt: timestamp, - lastAccessedAt: timestamp, - }); - } - - function refresh(identity: CacheIdentity, queryFn: () => T | Promise): Promise { - const existing = inFlight.get(identity.hash); - if (existing) return existing as Promise; - const promise = Promise.resolve() - .then(queryFn) - .then(function storeValue(value) { - write(identity, value); - return value; - }); - inFlight.set(identity.hash, promise); - void promise.then( - function clearSuccess() { - return inFlight.delete(identity.hash); - }, - function clearFailure() { - return inFlight.delete(identity.hash); - }, - ); - return promise; - } - - return { - path: params.path, - async query(options: RigCacheQueryOptions): Promise { - const freshness = staleTime({ value: options.staleTime }); - const identity = CacheKeyHasherSingleton.identity({ queryKey: options.queryKey }); - const entry = read(identity); - if (!entry) return await refresh(identity, options.queryFn); - if (entry.invalidatedAt === null && params.now() - entry.updatedAt < freshness) { - return entry.data; - } - return await refresh(identity, options.queryFn); - }, - peek(queryKey: RigCacheKey): T | undefined { - return read(CacheKeyHasherSingleton.identity({ queryKey }))?.data; - }, - set(queryKey: RigCacheKey, value: T): void { - write(CacheKeyHasherSingleton.identity({ queryKey }), value); - }, - invalidate(queryKey: RigCacheKey): void { - const identity = CacheKeyHasherSingleton.identity({ queryKey }); - params.db - .query("update _rig_cache set invalidated_at = $invalidatedAt where key_hash = $keyHash") - .run({ keyHash: identity.hash, invalidatedAt: params.now() }); - }, - remove(queryKey: RigCacheKey): void { - const identity = CacheKeyHasherSingleton.identity({ queryKey }); - params.db.query("delete from _rig_cache where key_hash = ?").run(identity.hash); - }, - clear(): void { - params.db.run("delete from _rig_cache"); - }, - close(): void { - params.db.close(false); - }, - }; -} - -export const SqliteToolCacheSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createSqliteToolCache, -}); - -function createLazyToolCache(params: { - path: string; - create: () => ManagedRigToolCache; -}): ManagedRigToolCache { - let cache: ManagedRigToolCache | undefined; - function resource(): ManagedRigToolCache { - if (cache) return cache; - cache = params.create(); - return cache; - } - return { - path: params.path, - query(options: RigCacheQueryOptions): Promise { - return resource().query(options); - }, - peek(queryKey: RigCacheKey): T | undefined { - return resource().peek(queryKey); - }, - set(queryKey: RigCacheKey, value: T): void { - resource().set(queryKey, value); - }, - invalidate(queryKey: RigCacheKey): void { - resource().invalidate(queryKey); - }, - remove(queryKey: RigCacheKey): void { - resource().remove(queryKey); - }, - clear(): void { - resource().clear(); - }, - close(): void { - cache?.close(); - }, - }; -} - -export const LazyToolCacheSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createLazyToolCache, -}); - -function createUnavailableToolCache(params: { path: string }): ManagedRigToolCache { - function unavailable(): never { - throw new RigErrorClass( - "TOOL_INVALID", - "context.cache requires the Bun SQLite runtime. Run rig through its normal Bun bootstrap.", - { path: params.path }, - ); - } - return { - path: params.path, - query: unavailable, - peek: unavailable, - set: unavailable, - invalidate: unavailable, - remove: unavailable, - clear: unavailable, - close() {}, - }; -} - -export const UnavailableToolCacheSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createUnavailableToolCache, -}); - -type RigToolCacheFactoryDeps = { - sqliteAvailable: () => boolean; - loadDatabase: () => Promise; - mkdir: typeof mkdirSync; - dirname: typeof dirname; - join: typeof join; - now: () => number; -}; - -function createRigToolCacheFactoryDeps(): RigToolCacheFactoryDeps { - const sqlite = new BunSqliteModuleLoaderClass(); - return { - sqliteAvailable: sqlite.available.bind(sqlite), - loadDatabase: sqlite.database.bind(sqlite), - mkdir: mkdirSync, - dirname, - join, - now: Date.now, - }; -} - -const RigToolCacheFactoryProductionDeps = createRigToolCacheFactoryDeps(); - -export class RigToolCacheFactoryRepo extends defineRepo({ - params: {}, - deps: RigToolCacheFactoryProductionDeps, -}) { - public cachePathForToolPath(params: { toolPath: string }): string { - return this.deps.join(this.deps.dirname(params.toolPath), "cache.sqlite"); - } - - private open(params: { - cachePath: string; - Database: DatabaseConstructor | undefined; - }): ManagedRigToolCache { - if (!params.Database) { - return UnavailableToolCacheSingleton.create({ path: params.cachePath }); - } - this.deps.mkdir(this.deps.dirname(params.cachePath), { recursive: true }); - const db = new params.Database(params.cachePath, { create: true, strict: true }); - try { - db.run("PRAGMA journal_mode = WAL;"); - db.run(` - create table if not exists _rig_cache ( - key_hash text primary key, - key_json text not null, - value_json text not null, - data_updated_at integer not null, - invalidated_at integer, - last_accessed_at integer not null - ); - `); - return SqliteToolCacheSingleton.create({ - db, - path: params.cachePath, - now: this.deps.now, - }); - } catch (error) { - db.close(false); - throw error; - } - } - - public async create(params: { - toolPath: string; - log: RigToolLogger; - }): Promise { - void params.log; - const cachePath = this.cachePathForToolPath(params); - const Database = this.deps.sqliteAvailable() ? await this.deps.loadDatabase() : undefined; - return LazyToolCacheSingleton.create({ - path: cachePath, - create: () => this.open({ cachePath, Database }), - }); - } -} - -export const RigToolCacheFactory = new RigToolCacheFactoryRepo(); - -type ToolCacheServiceDeps = { - createCache: (params: { toolPath: string; log: RigToolLogger }) => Promise; - cachePathForToolPath: (params: { toolPath: string }) => string; -}; - -const ToolCacheServiceProductionDeps: ToolCacheServiceDeps = { - createCache(params) { - return RigToolCacheFactory.create(params); - }, - cachePathForToolPath(params) { - return RigToolCacheFactory.cachePathForToolPath(params); - }, -}; - -export class ToolCacheRepo extends defineRepo({ - params: {}, - deps: ToolCacheServiceProductionDeps, -}) { - public async setup(params: { - tool: LoadedTool; - log: RigToolLogger; - }): Promise { - return await this.deps.createCache({ toolPath: params.tool.path, log: params.log }); - } - - public cachePathForToolPath(params: { toolPath: string }): string { - return this.deps.cachePathForToolPath(params); - } -} - -export const ToolCache = new ToolCacheRepo(); - -export class ToolCacheServiceClass { - public setup(tool: LoadedTool, log: RigToolLogger): Promise { - return ToolCache.setup({ tool, log }); - } - - public cachePathForToolPath(toolPath: string): string { - return ToolCache.cachePathForToolPath({ toolPath }); - } -} diff --git a/src/persistence/kv/tool-kv.ts b/src/persistence/kv/tool-kv.ts deleted file mode 100644 index 14d00a8..0000000 --- a/src/persistence/kv/tool-kv.ts +++ /dev/null @@ -1,258 +0,0 @@ -import type { Database } from "bun:sqlite"; -import { mkdirSync } from "node:fs"; -import { dirname, join } from "node:path"; -import { defineRepo, defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import type { LoadedTool, RigToolKvStore } from "../../tools/types"; -import { BunSqliteModuleLoaderClass, type DatabaseConstructor } from "../sqlite/tool-database"; - -type KvRow = { value_json: string }; - -function validateKvKey(params: { key: string }): void { - if (typeof params.key !== "string" || params.key.length === 0) { - throw new RigErrorClass("INPUT_ERROR", "context.kv keys must be non-empty strings.", { - key: params.key, - }); - } -} - -export const SqliteToolKvStoreSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { db: Database; path: string; nowIso: () => string }): RigToolKvStore { - return { - path: params.path, - get(key: string): T | undefined { - validateKvKey({ key }); - const row = params.db - .query("select value_json from _rig_kv where key = ?") - .get(key) as KvRow | null; - if (!row) return undefined; - return JSON.parse(row.value_json) as T; - }, - set(key: string, value: unknown): void { - validateKvKey({ key }); - const valueJson = JSON.stringify(value); - if (valueJson === undefined) { - throw new RigErrorClass("INPUT_ERROR", "context.kv cannot store undefined values.", { - key, - }); - } - params.db - .query( - `insert into _rig_kv (key, value_json, updated_at) - values ($key, $valueJson, $updatedAt) - on conflict(key) do update set - value_json = excluded.value_json, - updated_at = excluded.updated_at`, - ) - .run({ key, valueJson, updatedAt: params.nowIso() }); - }, - }; - }, -}); - -export type ManagedRigToolKvStore = RigToolKvStore & { close(): void }; - -function createLazyToolKvStore(params: { - path: string; - create: () => RigToolKvStore; -}): ManagedRigToolKvStore { - let store: RigToolKvStore | undefined; - - function resource(): RigToolKvStore { - if (store) return store; - store = params.create(); - return store; - } - - return { - path: params.path, - get(key: string): T | undefined { - return resource().get(key); - }, - set(key: string, value: unknown): void { - resource().set(key, value); - }, - close(): void { - const closeable = store as { close?: (throwOnError?: boolean) => void } | undefined; - if (!closeable) return; - closeable.close?.(false); - }, - }; -} - -export const LazyToolKvStoreSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createLazyToolKvStore, -}); - -function createUnavailableToolKvStore(params: { path: string }): RigToolKvStore { - function unavailable(): never { - throw new RigErrorClass( - "TOOL_INVALID", - "context.kv requires the Bun SQLite runtime. Run rig through its normal Bun bootstrap.", - { path: params.path }, - ); - } - return { path: params.path, get: unavailable, set: unavailable }; -} - -export const UnavailableToolKvStoreSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createUnavailableToolKvStore, -}); - -type RigToolKvStoreFactoryDeps = { - sqliteAvailable: () => boolean; - loadDatabase: () => Promise; - mkdir: typeof mkdirSync; - dirname: typeof dirname; - join: typeof join; - nowIso: () => string; -}; - -function createRigToolKvStoreFactoryDeps(): RigToolKvStoreFactoryDeps { - const sqlite = new BunSqliteModuleLoaderClass(); - return { - sqliteAvailable: sqlite.available.bind(sqlite), - loadDatabase: sqlite.database.bind(sqlite), - mkdir: mkdirSync, - dirname, - join, - nowIso() { - return new Date().toISOString(); - }, - }; -} - -const RigToolKvStoreFactoryProductionDeps = createRigToolKvStoreFactoryDeps(); - -export class RigToolKvStoreFactoryRepo extends defineRepo({ - params: {}, - deps: RigToolKvStoreFactoryProductionDeps, -}) { - public kvPathForToolPath(params: { toolPath: string }): string { - return this.deps.join(this.deps.dirname(params.toolPath), "kv.sqlite"); - } - - private open(params: { - kvPath: string; - Database: DatabaseConstructor | undefined; - }): RigToolKvStore { - if (!params.Database) { - return UnavailableToolKvStoreSingleton.create({ path: params.kvPath }); - } - - this.deps.mkdir(this.deps.dirname(params.kvPath), { recursive: true }); - const db = new params.Database(params.kvPath, { create: true, strict: true }); - try { - db.run("PRAGMA journal_mode = WAL;"); - db.run(` - create table if not exists _rig_kv ( - key text primary key, - value_json text not null, - updated_at text not null - ); - `); - const store = SqliteToolKvStoreSingleton.create({ - db, - path: params.kvPath, - nowIso: this.deps.nowIso, - }); - return { - ...store, - close() { - db.close(false); - }, - } as RigToolKvStore; - } catch (error) { - db.close(false); - throw error; - } - } - - public async create(params: { toolPath: string }): Promise { - const kvPath = this.kvPathForToolPath(params); - const Database = this.deps.sqliteAvailable() ? await this.deps.loadDatabase() : undefined; - return LazyToolKvStoreSingleton.create({ - path: kvPath, - create: () => this.open({ kvPath, Database }), - }); - } -} - -export const RigToolKvStoreFactory = new RigToolKvStoreFactoryRepo(); - -type ToolKvStoreServiceDeps = { - createStore: (params: { toolPath: string }) => Promise; - kvPathForToolPath: (params: { toolPath: string }) => string; -}; - -const ToolKvStoreServiceProductionDeps: ToolKvStoreServiceDeps = { - createStore(params) { - return RigToolKvStoreFactory.create(params); - }, - kvPathForToolPath(params) { - return RigToolKvStoreFactory.kvPathForToolPath(params); - }, -}; - -export class ToolKvStoreRepo extends defineRepo({ - params: {}, - deps: ToolKvStoreServiceProductionDeps, -}) { - public async setup(params: { tool: LoadedTool }): Promise { - return await this.deps.createStore({ toolPath: params.tool.path }); - } - - public kvPathForToolPath(params: { toolPath: string }): string { - return this.deps.kvPathForToolPath(params); - } -} - -export type ToolKvStoreServiceClass = { - setup(tool: LoadedTool): Promise; - kvPathForToolPath(toolPath: string): string; -}; - -type ToolKvStoreServiceConstructor = { - new (): ToolKvStoreServiceClass; - readonly prototype: ToolKvStoreServiceClass; -}; - -type ToolKvStoreServiceAdapter = ToolKvStoreServiceClass & { - readonly resource: ToolKvStoreRepo; -}; - -const ToolKvStoreServiceClassAdapter = function constructToolKvStoreService( - this: ToolKvStoreServiceAdapter, -): void { - Object.defineProperty(this, "resource", { - value: new ToolKvStoreRepo(), - }); -}; -Object.defineProperty(ToolKvStoreServiceClassAdapter, "name", { - value: "ToolKvStoreServiceClass", -}); -Object.defineProperties(ToolKvStoreServiceClassAdapter.prototype, { - setup: { - configurable: true, - value: function setup(this: ToolKvStoreServiceAdapter, tool: LoadedTool) { - return this.resource.setup({ tool }); - }, - writable: true, - }, - kvPathForToolPath: { - configurable: true, - value: function kvPathForToolPath(this: ToolKvStoreServiceAdapter, toolPath: string) { - return this.resource.kvPathForToolPath({ toolPath }); - }, - writable: true, - }, -}); - -export const ToolKvStoreServiceClass = - ToolKvStoreServiceClassAdapter as unknown as ToolKvStoreServiceConstructor; diff --git a/src/persistence/sqlite/tool-database.ts b/src/persistence/sqlite/tool-database.ts deleted file mode 100644 index 7ff9708..0000000 --- a/src/persistence/sqlite/tool-database.ts +++ /dev/null @@ -1,371 +0,0 @@ -import type { Database } from "bun:sqlite"; -import { createHash } from "node:crypto"; -import { mkdir } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import { defineProvider, defineRepo, defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import type { LoadedTool, RigToolDatabase } from "../../tools/types"; - -type MigrationRow = { name: string; checksum: string }; - -function migrationChecksum(params: { version: number; name: string; sql: string }): string { - return createHash("sha256") - .update(`${params.version}\0${params.name}\0${params.sql}`) - .digest("hex"); -} - -export const RigDatabaseMigratorSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { db: Database; nowIso: () => string }) { - let lastVersion = 0; - - function ensureMetadata(_params: {}): void { - params.db.run(` - create table if not exists _rig_migrations ( - version integer primary key, - name text not null, - checksum text not null, - applied_at text not null - ); - `); - } - - function validate(validation: { version: number; name: string; sql: string }): void { - if (!Number.isInteger(validation.version) || validation.version <= 0) { - throw new RigErrorClass( - "TOOL_INVALID", - `Migration version must be a positive integer: ${validation.version}`, - ); - } - if (validation.version <= lastVersion) { - throw new RigErrorClass( - "TOOL_INVALID", - `Migration versions must be declared in ascending order: ${validation.version} after ${lastVersion}`, - ); - } - if (!validation.name.trim()) { - throw new RigErrorClass("TOOL_INVALID", "Migration name must not be empty."); - } - if (!validation.sql.trim()) { - throw new RigErrorClass( - "TOOL_INVALID", - `Migration ${validation.version} SQL must not be empty.`, - ); - } - lastVersion = validation.version; - } - - function existingMigration(existing: { version: number }): MigrationRow | undefined { - const row = params.db - .query("select name, checksum from _rig_migrations where version = ?") - .get(existing.version) as MigrationRow | null; - return row ?? undefined; - } - - function migrate(migration: { version: number; name: string; sql: string }): void { - validate(migration); - const checksum = migrationChecksum(migration); - const existing = existingMigration({ version: migration.version }); - if (existing) { - if (existing.name === migration.name && existing.checksum === checksum) return; - throw new RigErrorClass( - "TOOL_INVALID", - `Migration ${migration.version} has changed since it was applied.`, - { - version: migration.version, - expected: existing, - actual: { name: migration.name, checksum }, - }, - ); - } - - const apply = params.db.transaction(function applyMigration() { - params.db.run(migration.sql); - params.db - .query( - `insert into _rig_migrations (version, name, checksum, applied_at) - values ($version, $name, $checksum, $appliedAt)`, - ) - .run({ - version: migration.version, - name: migration.name, - checksum, - appliedAt: params.nowIso(), - }); - }); - apply(); - } - - return { ensureMetadata, migrate }; - }, -}); - -export type DatabaseConstructor = new ( - filename: string, - options?: { create?: boolean; strict?: boolean }, -) => Database; - -type BunSqliteModuleLoaderDeps = { - testDatabase: () => DatabaseConstructor | undefined; - hasBun: () => boolean; - nativeDatabase: () => Promise; -}; - -const BunSqliteModuleLoaderProductionDeps: BunSqliteModuleLoaderDeps = { - testDatabase() { - return (globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: DatabaseConstructor }) - .rigSqliteDatabaseForTests; - }, - hasBun() { - return Boolean((globalThis as typeof globalThis & { Bun?: unknown }).Bun); - }, - /* v8 ignore start */ - async nativeDatabase() { - const moduleValue = (await import("bun:sqlite")) as { Database: DatabaseConstructor }; - return moduleValue.Database; - }, - /* v8 ignore stop */ -}; - -export class BunSqliteModuleLoaderProvider extends defineProvider({ - params: {}, - deps: BunSqliteModuleLoaderProductionDeps, -}) { - public available(_params: {}): boolean { - return Boolean(this.deps.testDatabase() || this.deps.hasBun()); - } - - public async database(_params: {}): Promise { - const testDatabase = this.deps.testDatabase(); - /* v8 ignore next */ - if (!testDatabase) return await this.deps.nativeDatabase(); - return testDatabase; - } -} - -export const BunSqliteModuleLoader = new BunSqliteModuleLoaderProvider(); - -export type BunSqliteModuleLoaderClass = { - available(): boolean; - database(): Promise; -}; - -type BunSqliteModuleLoaderConstructor = { - new (): BunSqliteModuleLoaderClass; - readonly prototype: BunSqliteModuleLoaderClass; -}; - -type BunSqliteModuleLoaderAdapter = BunSqliteModuleLoaderClass & { - readonly resource: BunSqliteModuleLoaderProvider; -}; - -const BunSqliteModuleLoaderClassAdapter = function constructBunSqliteModuleLoader( - this: BunSqliteModuleLoaderAdapter, -): void { - Object.defineProperty(this, "resource", { value: BunSqliteModuleLoader }); -}; -Object.defineProperty(BunSqliteModuleLoaderClassAdapter, "name", { - value: "BunSqliteModuleLoaderClass", -}); -Object.defineProperties(BunSqliteModuleLoaderClassAdapter.prototype, { - available: { - configurable: true, - value: function available(this: BunSqliteModuleLoaderAdapter) { - return this.resource.available({}); - }, - writable: true, - }, - database: { - configurable: true, - value: function database(this: BunSqliteModuleLoaderAdapter) { - return this.resource.database({}); - }, - writable: true, - }, -}); - -export const BunSqliteModuleLoaderClass = - BunSqliteModuleLoaderClassAdapter as unknown as BunSqliteModuleLoaderConstructor; - -type RigToolDatabaseFactoryDeps = { - loadDatabase: () => Promise; - mkdir: typeof mkdir; - dirname: typeof dirname; - join: typeof join; - nowIso: () => string; -}; - -function createRigToolDatabaseFactoryDeps(): RigToolDatabaseFactoryDeps { - const sqlite = new BunSqliteModuleLoaderClass(); - return { - loadDatabase: sqlite.database.bind(sqlite), - mkdir, - dirname, - join, - nowIso() { - return new Date().toISOString(); - }, - }; -} - -const RigToolDatabaseFactoryProductionDeps = createRigToolDatabaseFactoryDeps(); - -export class RigToolDatabaseFactoryRepo extends defineRepo({ - params: {}, - deps: RigToolDatabaseFactoryProductionDeps, -}) { - public dbPathForToolPath(params: { toolPath: string }): string { - return this.deps.join(this.deps.dirname(params.toolPath), "index.sqlite"); - } - - public async create(params: { toolPath: string }): Promise { - const dbPath = this.dbPathForToolPath(params); - await this.deps.mkdir(this.deps.dirname(dbPath), { recursive: true }); - const Database = await this.deps.loadDatabase(); - const db = new Database(dbPath, { create: true, strict: true }) as RigToolDatabase; - try { - const migrator = RigDatabaseMigratorSingleton.create({ - db, - nowIso: this.deps.nowIso, - }); - Object.defineProperty(db, "path", { value: dbPath, enumerable: true }); - Object.defineProperty(db, "migrate", { - value(version: number, name: string, sql: string) { - return migrator.migrate({ version, name, sql }); - }, - enumerable: true, - }); - db.run("PRAGMA journal_mode = WAL;"); - migrator.ensureMetadata({}); - return db; - } catch (error) { - db.close(false); - throw error; - } - } -} - -export const RigToolDatabaseFactory = new RigToolDatabaseFactoryRepo(); - -function unavailableDatabase(params: { toolName: string }): RigToolDatabase { - return new Proxy({} as RigToolDatabase, { - get() { - throw new RigErrorClass( - "TOOL_INVALID", - `Tool ${params.toolName} must define setupDb before using context.db.`, - { tool: params.toolName }, - ); - }, - }); -} - -export const UnavailableToolDatabaseFactorySingleton = defineSingleton({ - params: {}, - deps: {}, - create: unavailableDatabase, -}); - -export type UnavailableToolDatabaseFactoryClass = { - create(toolName: string): RigToolDatabase; -}; - -type UnavailableToolDatabaseFactoryConstructor = { - new (): UnavailableToolDatabaseFactoryClass; - readonly prototype: UnavailableToolDatabaseFactoryClass; -}; - -const UnavailableToolDatabaseFactoryClassAdapter = - function constructUnavailableToolDatabaseFactory(): void {}; -Object.defineProperty(UnavailableToolDatabaseFactoryClassAdapter, "name", { - value: "UnavailableToolDatabaseFactoryClass", -}); -Object.defineProperty(UnavailableToolDatabaseFactoryClassAdapter.prototype, "create", { - configurable: true, - value: function create(toolName: string) { - return UnavailableToolDatabaseFactorySingleton.create({ toolName }); - }, - writable: true, -}); - -export const UnavailableToolDatabaseFactoryClass = - UnavailableToolDatabaseFactoryClassAdapter as unknown as UnavailableToolDatabaseFactoryConstructor; - -type ToolDatabaseServiceDeps = { - createDatabase: (params: { toolPath: string }) => Promise; - dbPathForToolPath: (params: { toolPath: string }) => string; -}; - -const ToolDatabaseServiceProductionDeps: ToolDatabaseServiceDeps = { - createDatabase(params) { - return RigToolDatabaseFactory.create(params); - }, - dbPathForToolPath(params) { - return RigToolDatabaseFactory.dbPathForToolPath(params); - }, -}; - -export class ToolDatabaseRepo extends defineRepo({ - params: {}, - deps: ToolDatabaseServiceProductionDeps, -}) { - public async setup(params: { tool: LoadedTool }): Promise { - if (!params.tool.definition.setupDb) return undefined; - const db = await this.deps.createDatabase({ toolPath: params.tool.path }); - try { - await params.tool.definition.setupDb(db); - return db; - } catch (error) { - db.close(false); - throw error; - } - } - - public dbPathForToolPath(params: { toolPath: string }): string { - return this.deps.dbPathForToolPath(params); - } -} - -export const ToolDatabase = new ToolDatabaseRepo(); - -export type ToolDatabaseServiceClass = { - setup(tool: LoadedTool): Promise; - dbPathForToolPath(toolPath: string): string; -}; - -type ToolDatabaseServiceConstructor = { - new (): ToolDatabaseServiceClass; - readonly prototype: ToolDatabaseServiceClass; -}; - -type ToolDatabaseServiceAdapter = ToolDatabaseServiceClass & { - readonly resource: ToolDatabaseRepo; -}; - -const ToolDatabaseServiceClassAdapter = function constructToolDatabaseService( - this: ToolDatabaseServiceAdapter, -): void { - Object.defineProperty(this, "resource", { value: ToolDatabase }); -}; -Object.defineProperty(ToolDatabaseServiceClassAdapter, "name", { - value: "ToolDatabaseServiceClass", -}); -Object.defineProperties(ToolDatabaseServiceClassAdapter.prototype, { - setup: { - configurable: true, - value: function setup(this: ToolDatabaseServiceAdapter, tool: LoadedTool) { - return this.resource.setup({ tool }); - }, - writable: true, - }, - dbPathForToolPath: { - configurable: true, - value: function dbPathForToolPath(this: ToolDatabaseServiceAdapter, toolPath: string) { - return this.resource.dbPathForToolPath({ toolPath }); - }, - writable: true, - }, -}); - -export const ToolDatabaseServiceClass = - ToolDatabaseServiceClassAdapter as unknown as ToolDatabaseServiceConstructor; diff --git a/src/providers/database/sqlite.ts b/src/providers/database/sqlite.ts new file mode 100644 index 0000000..2bac5c0 --- /dev/null +++ b/src/providers/database/sqlite.ts @@ -0,0 +1,48 @@ +import type { Database } from "bun:sqlite"; +import { Context, Effect, Layer } from "effect"; +import { RigError } from "../errors/rig-error"; + +export type DatabaseConstructor = new ( + filename: string, + options?: { create?: boolean; strict?: boolean }, +) => Database; + +export class SqlitePlatformService extends Context.Service< + SqlitePlatformService, + { + readonly available: Effect.Effect; + readonly load: Effect.Effect; + } +>()("@rendotdev/rig/providers/database/SqlitePlatformService", { + make: Effect.gen(function* () { + const toRigError = (error: unknown) => { + if (error instanceof RigError) return error; + return new RigError({ + code: "INTERNAL_ERROR", + message: error instanceof Error ? error.message : String(error), + }); + }; + const testDatabase = () => + (globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: DatabaseConstructor }) + .rigSqliteDatabaseForTests; + const available = Effect.sync(() => + Boolean(testDatabase() || typeof process.versions.bun === "string"), + ).pipe(Effect.withSpan("SqlitePlatformService.available")); + const load = Effect.gen(function* () { + const injected = testDatabase(); + if (injected) return injected; + return yield* Effect.tryPromise({ + /* v8 ignore start -- Bun's native module is exercised by packaged integration */ + try: async () => { + const moduleValue = (await import("bun:sqlite")) as { Database: DatabaseConstructor }; + return moduleValue.Database; + }, + /* v8 ignore stop */ + catch: toRigError, + }); + }).pipe(Effect.withSpan("SqlitePlatformService.load")); + return { available, load } as const; + }), +}) { + static readonly layer = Layer.effect(SqlitePlatformService, SqlitePlatformService.make); +} diff --git a/src/providers/errors/codes.ts b/src/providers/errors/codes.ts index 98ca616..9aa8cdc 100644 --- a/src/providers/errors/codes.ts +++ b/src/providers/errors/codes.ts @@ -1,18 +1,19 @@ -export const ErrorCodes = { - CONFIG_INVALID: "CONFIG_INVALID", - DUPLICATE_TOOL: "DUPLICATE_TOOL", - TOOL_NOT_FOUND: "TOOL_NOT_FOUND", - COMMAND_NOT_FOUND: "COMMAND_NOT_FOUND", - TOOL_INVALID: "TOOL_INVALID", - VALIDATION_ERROR: "VALIDATION_ERROR", - OUTPUT_VALIDATION_ERROR: "OUTPUT_VALIDATION_ERROR", - INPUT_ERROR: "INPUT_ERROR", - INTERNAL_ERROR: "INTERNAL_ERROR", - SHELL_ERROR: "SHELL_ERROR", - TOOL_RUN_ERROR: "TOOL_RUN_ERROR", - TYPECHECK_ERROR: "TYPECHECK_ERROR", - CRON_ERROR: "CRON_ERROR", - DEV_LINK_ERROR: "DEV_LINK_ERROR", -} as const; +import { Schema } from "effect"; -export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]; +export const ErrorCode = Schema.Literals([ + "CONFIG_INVALID", + "DUPLICATE_TOOL", + "TOOL_NOT_FOUND", + "COMMAND_NOT_FOUND", + "TOOL_INVALID", + "VALIDATION_ERROR", + "OUTPUT_VALIDATION_ERROR", + "INPUT_ERROR", + "INTERNAL_ERROR", + "SHELL_ERROR", + "TOOL_RUN_ERROR", + "TYPECHECK_ERROR", + "CRON_ERROR", + "DEV_LINK_ERROR", +]); +export type ErrorCode = typeof ErrorCode.Type; diff --git a/src/providers/errors/index.ts b/src/providers/errors/index.ts deleted file mode 100644 index fd8ec00..0000000 --- a/src/providers/errors/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./codes.ts"; -export * from "./rig-error.ts"; diff --git a/src/providers/errors/rig-error.test.ts b/src/providers/errors/rig-error.test.ts index 9959e87..a06bf08 100644 --- a/src/providers/errors/rig-error.test.ts +++ b/src/providers/errors/rig-error.test.ts @@ -1,101 +1,60 @@ +import { Effect, Schema } from "effect"; import { describe, expect, it } from "vite-plus/test"; -import { - RigError, - RigErrorClass, - RigErrorsClass, - RigErrorSingleton, - rigErrors, - type RigError as RigErrorInstance, -} from "./rig-error"; +import { RigError, RigErrorNormalizationService, rigErrorNormalizationLayer } from "./rig-error"; -describe("RigErrorSingleton", () => { - it("creates Rig errors through the production singleton", () => { - const details = { field: "name" }; - const productionError = RigErrorSingleton.create({ - code: "INPUT_ERROR", - message: "Invalid name.", - details, - }); - const builtError = RigErrorSingleton.create({ - code: "CONFIG_INVALID", - message: "Invalid config.", +class WrappedError extends Schema.TaggedErrorClass()("WrappedError", { + cause: Schema.Defect(), +}) {} + +const normalize = (cause: unknown) => + RigErrorNormalizationService.use((service) => service.normalize(cause)).pipe( + Effect.provide(rigErrorNormalizationLayer), + Effect.runPromise, + ); + +describe("RigError", () => { + it("is a schema-backed tagged error", () => { + const details = { command: "missing" }; + const error = new RigError({ + code: "COMMAND_NOT_FOUND", + message: "Missing command.", + details: details, }); - expect(productionError).toBeInstanceOf(Error); - expect(productionError).toBeInstanceOf(RigErrorClass); - expect(productionError).toMatchObject({ + expect(error).toBeInstanceOf(Error); + expect(error).toBeInstanceOf(RigError); + expect(error).toMatchObject({ + _tag: "RigError", name: "RigError", - code: "INPUT_ERROR", - message: "Invalid name.", + code: "COMMAND_NOT_FOUND", + message: "Missing command.", details, }); - expect(builtError).toMatchObject({ - name: "RigError", - code: "CONFIG_INVALID", - message: "Invalid config.", - details: undefined, - }); }); - it("preserves Rig error identity and normalizes other errors", () => { - const existing = RigErrorSingleton.create({ code: "TOOL_INVALID", message: "Invalid tool." }); - - expect(RigErrorSingleton.from({ error: existing })).toBe(existing); - expect(RigErrorSingleton.from({ error: new Error("Plain failure.") })).toMatchObject({ - name: "RigError", - code: "INTERNAL_ERROR", - message: "Plain failure.", - details: undefined, - }); - expect(RigErrorSingleton.from({ error: "String failure." })).toMatchObject({ - name: "RigError", - code: "INTERNAL_ERROR", - message: "String failure.", - details: undefined, + it("preserves a RigError nested inside typed Effect errors", async () => { + const expected = new RigError({ + code: "CONFIG_INVALID", + message: "Config is not valid JSON.", + details: { path: "rig.json" }, }); + + await expect(normalize(new WrappedError({ cause: expected }))).resolves.toBe(expected); }); -}); -describe("Rig error compatibility adapters", () => { - it("keeps RigErrorClass and RigError constructible without production classes", () => { - const details = { command: "missing" }; - const classError = new RigErrorClass("COMMAND_NOT_FOUND", "Missing command.", details); - const aliasError: RigErrorInstance = new RigError("INPUT_ERROR", "Invalid input."); + it("extracts a useful message from nested platform errors", async () => { + const error = await normalize(new WrappedError({ cause: new Error("write failed") })); - expect(RigError).toBe(RigErrorClass); - expect(RigErrorClass.name).toBe("RigErrorClass"); - expect(classError).toBeInstanceOf(Error); - expect(classError).toBeInstanceOf(RigErrorClass); - expect(aliasError).toBeInstanceOf(RigErrorClass); - expect(classError).toMatchObject({ - name: "RigError", - code: "COMMAND_NOT_FOUND", - message: "Missing command.", - details, - }); - expect(aliasError.details).toBeUndefined(); - expect(RigErrorClass.prototype.constructor).toBe(RigErrorClass); + expect(error).toMatchObject({ code: "INTERNAL_ERROR", message: "write failed" }); }); - it("keeps RigErrorsClass construction, prototype behavior, and rigErrors normalization", () => { - const errors = new RigErrorsClass(); - const existing = new RigErrorClass("CRON_ERROR", "Invalid schedule."); + it("uses a stable fallback for cyclic unknown errors", async () => { + const error: { cause?: unknown } = {}; + error.cause = error; - expect(errors).toBeInstanceOf(RigErrorsClass); - expect(RigErrorsClass.name).toBe("RigErrorsClass"); - expect(Object.hasOwn(RigErrorsClass.prototype, "from")).toBe(true); - expect(errors.from(existing)).toBe(existing); - expect(RigErrorsClass.prototype.from(existing)).toBe(existing); - expect(rigErrors.from(existing)).toBe(existing); - expect(rigErrors.from(new Error("Plain failure."))).toMatchObject({ - name: "RigError", - code: "INTERNAL_ERROR", - message: "Plain failure.", - }); - expect(rigErrors.from(42)).toMatchObject({ - name: "RigError", + await expect(normalize(error)).resolves.toMatchObject({ code: "INTERNAL_ERROR", - message: "42", + message: "Unknown Rig failure.", }); }); }); diff --git a/src/providers/errors/rig-error.ts b/src/providers/errors/rig-error.ts index c35ee99..d7b869d 100644 --- a/src/providers/errors/rig-error.ts +++ b/src/providers/errors/rig-error.ts @@ -1,86 +1,59 @@ -import { defineSingleton } from "../../define.ts"; -import type { ErrorCode } from "./codes"; +import { Context, Effect, Layer, Predicate, Schema } from "effect"; +import { ErrorCode } from "./codes"; -export type RigErrorClass = Error & { - code: ErrorCode; - details: unknown; -}; - -type RigErrorConstructor = { - new (code: ErrorCode, message: string, details?: unknown): RigErrorClass; - readonly prototype: RigErrorClass; -}; - -type RigErrorsConstructor = { - new (): RigErrorsClass; - readonly prototype: RigErrorsClass; -}; - -const rigErrorPrototype = Object.create(Error.prototype) as RigErrorClass; - -function createRigError(params: { - readonly code: ErrorCode; - readonly message: string; - readonly details?: unknown; -}): RigErrorClass { - const error = new Error(params.message) as RigErrorClass; - Object.setPrototypeOf(error, rigErrorPrototype); - error.name = "RigError"; - error.code = params.code; - error.details = params.details; - return error; -} - -export const RigErrorSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createRigError, - from(params: { readonly error: unknown }): RigErrorClass { - if ( - typeof params.error === "object" && - params.error !== null && - rigErrorPrototype.isPrototypeOf(params.error) - ) { - return params.error as RigErrorClass; - } - if (params.error instanceof Error) { - return createRigError({ code: "INTERNAL_ERROR", message: params.error.message }); - } - return createRigError({ code: "INTERNAL_ERROR", message: String(params.error) }); - }, -}); - -const RigErrorClassAdapter = function constructRigError( +export class RigError extends Schema.TaggedErrorClass()("RigError", { code: ErrorCode, - message: string, - details?: unknown, -): RigErrorClass { - return RigErrorSingleton.create({ code, message, details }); -}; -RigErrorClassAdapter.prototype = rigErrorPrototype; -Object.defineProperty(RigErrorClassAdapter, "name", { value: "RigErrorClass" }); -Object.defineProperty(rigErrorPrototype, "constructor", { - configurable: true, - value: RigErrorClassAdapter, - writable: true, -}); - -export const RigErrorClass = RigErrorClassAdapter as unknown as RigErrorConstructor; -export { RigErrorClass as RigError }; - -export type RigErrorsClass = { - from(error: unknown): RigErrorClass; -}; - -const RigErrorsClassAdapter = function constructRigErrors() {}; -Object.defineProperty(RigErrorsClassAdapter, "name", { value: "RigErrorsClass" }); -Object.defineProperty(RigErrorsClassAdapter.prototype, "from", { - configurable: true, - value: function from(error: unknown): RigErrorClass { - return RigErrorSingleton.from({ error }); - }, - writable: true, -}); + message: Schema.String, + details: Schema.optional(Schema.Unknown), +}) {} + +export class RigErrorNormalizationService extends Context.Service< + RigErrorNormalizationService, + { readonly normalize: (cause: unknown) => Effect.Effect } +>()("@rendotdev/rig/providers/RigErrorNormalizationService", { + make: Effect.gen(function* () { + const find = (cause: unknown, seen: Set): RigError | undefined => { + if (cause instanceof RigError) return cause; + const isNonEmptyString = typeof cause === "string" && cause.length > 0; + if (isNonEmptyString) { + return new RigError({ code: "INTERNAL_ERROR", message: cause }); + } + const isUnusableObject = !Predicate.isObject(cause) || seen.has(cause); + if (isUnusableObject) return undefined; + seen.add(cause); + if ("cause" in cause) { + const nested = find(cause.cause, seen); + if (nested) return nested; + } + const isErrorWithMessage = cause instanceof Error && cause.message.length > 0; + if (isErrorWithMessage) { + return new RigError({ code: "INTERNAL_ERROR", message: cause.message }); + } + const hasStringMessage = + "message" in cause && typeof cause.message === "string" && cause.message.length > 0; + if (hasStringMessage) { + return new RigError({ code: "INTERNAL_ERROR", message: String(cause.message) }); + } + return undefined; + }; + const normalize = Effect.fn("RigErrorNormalizationService.normalize")(function* ( + cause: unknown, + ) { + return ( + find(cause, new Set()) ?? + new RigError({ + code: "INTERNAL_ERROR", + message: "Unknown Rig failure.", + }) + ); + }); + return { normalize } as const; + }), +}) { + static readonly layer = Layer.effect( + RigErrorNormalizationService, + RigErrorNormalizationService.make, + ); +} -export const RigErrorsClass = RigErrorsClassAdapter as unknown as RigErrorsConstructor; -export const rigErrors = new RigErrorsClass(); +export const rigErrorNormalizationLayer = RigErrorNormalizationService.layer; diff --git a/src/providers/filesystem/atomic-file-writer.ts b/src/providers/filesystem/atomic-file-writer.ts new file mode 100644 index 0000000..74818f7 --- /dev/null +++ b/src/providers/filesystem/atomic-file-writer.ts @@ -0,0 +1,125 @@ +import { randomUUID } from "node:crypto"; +import { + mkdir as nodeMkdir, + rename as nodeRename, + rm as nodeRemove, + writeFile as nodeWriteFile, +} from "node:fs/promises"; +import { dirname as nodeDirname } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; + +export class AtomicFileWriterError extends Schema.TaggedErrorClass()( + "AtomicFileWriterError", + { + path: Schema.String, + cause: Schema.Defect(), + }, +) {} + +class AtomicFileWriterPlatformService extends Context.Service< + AtomicFileWriterPlatformService, + { + readonly mkdir: (path: string) => Effect.Effect; + readonly rename: ( + oldPath: string, + newPath: string, + ) => Effect.Effect; + readonly remove: (path: string) => Effect.Effect; + readonly writeFile: ( + path: string, + content: string, + ) => Effect.Effect; + readonly dirname: (path: string) => Effect.Effect; + readonly randomUuid: Effect.Effect; + readonly processPid: Effect.Effect; + } +>()("@rendotdev/rig/settings/AtomicFileWriterPlatformService", { + make: Effect.gen(function* () { + const mkdir = Effect.fn("AtomicFileWriterPlatformService.mkdir")(function* (path: string) { + yield* Effect.tryPromise({ + try: () => nodeMkdir(path, { recursive: true }), + catch: (cause) => new AtomicFileWriterError({ path, cause }), + }); + }); + const rename = Effect.fn("AtomicFileWriterPlatformService.rename")(function* ( + oldPath: string, + newPath: string, + ) { + yield* Effect.tryPromise({ + try: () => nodeRename(oldPath, newPath), + catch: (cause) => new AtomicFileWriterError({ path: newPath, cause }), + }); + }); + const remove = Effect.fn("AtomicFileWriterPlatformService.remove")(function* (path: string) { + yield* Effect.tryPromise({ + try: () => nodeRemove(path, { force: true }), + catch: (cause) => new AtomicFileWriterError({ path, cause }), + }).pipe(Effect.ignore); + }); + const writeFile = Effect.fn("AtomicFileWriterPlatformService.writeFile")(function* ( + path: string, + content: string, + ) { + yield* Effect.tryPromise({ + try: () => nodeWriteFile(path, content, "utf8"), + catch: (cause) => new AtomicFileWriterError({ path, cause }), + }); + }); + const dirname = Effect.fn("AtomicFileWriterPlatformService.dirname")(function* (path: string) { + return nodeDirname(path); + }); + const randomUuid = Effect.sync(randomUUID).pipe( + Effect.withSpan("AtomicFileWriterPlatformService.randomUuid"), + ); + const processPid = Effect.sync(() => process.pid).pipe( + Effect.withSpan("AtomicFileWriterPlatformService.processPid"), + ); + return { mkdir, rename, remove, writeFile, dirname, randomUuid, processPid } as const; + }), +}) { + static readonly layer = Layer.effect( + AtomicFileWriterPlatformService, + AtomicFileWriterPlatformService.make, + ); +} + +export class AtomicFileWriterService extends Context.Service< + AtomicFileWriterService, + { + readonly write: (path: string, content: string) => Effect.Effect; + } +>()("@rendotdev/rig/settings/AtomicFileWriterService", { + make: Effect.gen(function* () { + const platform = yield* AtomicFileWriterPlatformService; + + const write = Effect.fn("AtomicFileWriterService.write")(function* ( + path: string, + content: string, + ) { + const parentDirectory = yield* platform.dirname(path); + yield* platform.mkdir(parentDirectory).pipe( + /* v8 ignore next -- platform I/O fault translation is defensive */ + Effect.mapError((cause) => new AtomicFileWriterError({ path, cause })), + ); + + const temporaryPath = `${path}.tmp-${yield* platform.processPid}-${yield* platform.randomUuid}`; + yield* Effect.acquireUseRelease( + Effect.succeed(temporaryPath), + (target) => + platform.writeFile(target, content).pipe( + Effect.andThen(platform.rename(target, path)), + Effect.mapError((cause) => new AtomicFileWriterError({ path, cause })), + ), + (target) => platform.remove(target), + ); + }); + + return { write } as const; + }), +}) { + static readonly layer = Layer.effect(AtomicFileWriterService, AtomicFileWriterService.make); +} + +export const atomicFileWriterLayer = AtomicFileWriterService.layer.pipe( + Layer.provide(AtomicFileWriterPlatformService.layer), +); diff --git a/src/providers/filesystem/file-lock-effect.test.ts b/src/providers/filesystem/file-lock-effect.test.ts new file mode 100644 index 0000000..b2212c2 --- /dev/null +++ b/src/providers/filesystem/file-lock-effect.test.ts @@ -0,0 +1,26 @@ +/* oxlint-disable vitest/no-standalone-expect -- @effect/vitest owns this Effect-aware test. */ +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { it } from "@effect/vitest"; +import { Effect } from "effect"; +import { expect } from "vite-plus/test"; +import { fileLockLayer, FileLockService } from "./file-lock"; + +it.live("runs file operations through the service layer", () => + Effect.scoped( + Effect.gen(function* () { + const directory = yield* Effect.acquireRelease( + Effect.promise(() => mkdtemp(join(tmpdir(), "rig-file-lock-effect-"))), + (path) => Effect.promise(() => rm(path, { recursive: true, force: true })), + ); + const locks = yield* FileLockService; + const result = yield* locks.withLock({ + targetPath: join(directory, "state.json"), + operation: () => Effect.succeed("locked"), + }); + + expect(result).toBe("locked"); + }), + ).pipe(Effect.provide(fileLockLayer)), +); diff --git a/src/providers/filesystem/file-lock-platform.ts b/src/providers/filesystem/file-lock-platform.ts new file mode 100644 index 0000000..3373b0a --- /dev/null +++ b/src/providers/filesystem/file-lock-platform.ts @@ -0,0 +1,76 @@ +import { mkdir, readFile, rename as nodeRename, rm, stat, writeFile } from "node:fs/promises"; +import { Context, Effect, Layer, Schema } from "effect"; + +export class FileLockPlatformError extends Schema.TaggedErrorClass()( + "FileLockPlatformError", + { cause: Schema.Defect() }, +) {} + +export class FileLockPlatformService extends Context.Service< + FileLockPlatformService, + { + readonly makeDirectory: ( + path: string, + recursive: boolean, + ) => Effect.Effect; + readonly readText: (path: string) => Effect.Effect; + readonly rename: ( + oldPath: string, + newPath: string, + ) => Effect.Effect; + readonly removeDirectory: (path: string) => Effect.Effect; + readonly modifiedAt: (path: string) => Effect.Effect; + readonly writeText: ( + path: string, + content: string, + ) => Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockPlatformService", { + make: Effect.gen(function* () { + const platformError = (cause: unknown) => new FileLockPlatformError({ cause }); + const makeDirectory = Effect.fn("FileLockPlatformService.makeDirectory")(function* ( + path: string, + recursive: boolean, + ) { + yield* Effect.tryPromise({ + try: () => mkdir(path, recursive ? { recursive: true } : undefined), + catch: platformError, + }); + }); + const readText = Effect.fn("FileLockPlatformService.readText")(function* (path: string) { + return yield* Effect.tryPromise({ try: () => readFile(path, "utf8"), catch: platformError }); + }); + const rename = Effect.fn("FileLockPlatformService.rename")(function* ( + oldPath: string, + newPath: string, + ) { + yield* Effect.tryPromise({ try: () => nodeRename(oldPath, newPath), catch: platformError }); + }); + const removeDirectory = Effect.fn("FileLockPlatformService.removeDirectory")(function* ( + path: string, + ) { + yield* Effect.tryPromise({ + try: () => rm(path, { recursive: true, force: true }), + catch: platformError, + }); + }); + const modifiedAt = Effect.fn("FileLockPlatformService.modifiedAt")(function* (path: string) { + return (yield* Effect.tryPromise({ + try: () => stat(path), + catch: platformError, + })).mtimeMs; + }); + const writeText = Effect.fn("FileLockPlatformService.writeText")(function* ( + path: string, + content: string, + ) { + yield* Effect.tryPromise({ + try: () => writeFile(path, content, "utf8"), + catch: platformError, + }); + }); + return { makeDirectory, readText, rename, removeDirectory, modifiedAt, writeText } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockPlatformService, FileLockPlatformService.make); +} diff --git a/src/providers/filesystem/file-lock-runtime.ts b/src/providers/filesystem/file-lock-runtime.ts new file mode 100644 index 0000000..07d4a40 --- /dev/null +++ b/src/providers/filesystem/file-lock-runtime.ts @@ -0,0 +1,53 @@ +import { randomUUID } from "node:crypto"; +import { hostname } from "node:os"; +import { Context, Effect, Layer, Schema } from "effect"; + +export class FileLockRuntimeError extends Schema.TaggedErrorClass()( + "FileLockRuntimeError", + { cause: Schema.Defect() }, +) {} + +export class FileLockRuntimeService extends Context.Service< + FileLockRuntimeService, + { + readonly killProcess: ( + pid: number, + signal: number, + ) => Effect.Effect; + readonly sleep: (milliseconds: number) => Effect.Effect; + readonly randomUuid: Effect.Effect; + readonly hostName: Effect.Effect; + readonly processPid: Effect.Effect; + readonly now: Effect.Effect; + readonly timestamp: Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockRuntimeService", { + make: Effect.gen(function* () { + const killProcess = Effect.fn("FileLockRuntimeService.killProcess")(function* ( + pid: number, + signal: number, + ) { + yield* Effect.try({ + try: () => process.kill(pid, signal), + catch: (cause) => new FileLockRuntimeError({ cause }), + }); + }); + const sleep = Effect.fn("FileLockRuntimeService.sleep")(function* (milliseconds: number) { + yield* Effect.sleep(milliseconds); + }); + const randomUuid = Effect.sync(randomUUID).pipe( + Effect.withSpan("FileLockRuntimeService.randomUuid"), + ); + const hostName = Effect.sync(hostname).pipe(Effect.withSpan("FileLockRuntimeService.hostName")); + const processPid = Effect.sync(() => process.pid).pipe( + Effect.withSpan("FileLockRuntimeService.processPid"), + ); + const now = Effect.sync(Date.now).pipe(Effect.withSpan("FileLockRuntimeService.now")); + const timestamp = Effect.sync(() => new Date().toISOString()).pipe( + Effect.withSpan("FileLockRuntimeService.timestamp"), + ); + return { killProcess, sleep, randomUuid, hostName, processPid, now, timestamp } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockRuntimeService, FileLockRuntimeService.make); +} diff --git a/src/providers/filesystem/file-lock.test.ts b/src/providers/filesystem/file-lock.test.ts new file mode 100644 index 0000000..5d1520d --- /dev/null +++ b/src/providers/filesystem/file-lock.test.ts @@ -0,0 +1,115 @@ +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, readFile, readdir, rm, utimes, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect } from "effect"; +import { describe, expect, test } from "vite-plus/test"; +import { + AtomicFileWriterError, + atomicFileWriterLayer, + AtomicFileWriterService, +} from "./atomic-file-writer"; +import { fileLockLayer, FileLockService } from "./file-lock"; + +describe("FileLockService", () => { + test("runs an operation under a lock and releases the lease", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-file-lock-test-")); + const target = join(directory, "state.json"); + try { + const result = await Effect.gen(function* () { + const locks = yield* FileLockService; + return yield* locks.withLock({ + targetPath: target, + operation: () => Effect.succeed("done"), + }); + }).pipe(Effect.provide(fileLockLayer), Effect.runPromise); + + expect(result).toBe("done"); + expect(existsSync(`${target}.lock`)).toBe(false); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); + + test("releases the lease when the protected operation fails", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-file-lock-test-")); + const target = join(directory, "state.json"); + try { + const program = Effect.gen(function* () { + const locks = yield* FileLockService; + return yield* locks.withLock({ + targetPath: target, + operation: () => Effect.die(new Error("operation failed")), + }); + }).pipe(Effect.provide(fileLockLayer), Effect.runPromise); + + await expect(program).rejects.toThrow("operation failed"); + expect(existsSync(`${target}.lock`)).toBe(false); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); + + test("recovers an abandoned stale lock", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-file-lock-test-")); + const target = join(directory, "state.json"); + const lockPath = `${target}.lock`; + try { + await mkdir(lockPath, { recursive: true }); + await writeFile(join(lockPath, "owner.json"), "not json\n", "utf8"); + const old = new Date(0); + await utimes(lockPath, old, old); + + await Effect.gen(function* () { + const locks = yield* FileLockService; + yield* locks.withLock({ + targetPath: target, + options: { staleMs: 1 }, + operation: () => Effect.void, + }); + }).pipe(Effect.provide(fileLockLayer), Effect.runPromise); + + expect(existsSync(lockPath)).toBe(false); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); +}); + +describe("AtomicFileWriterService", () => { + test("atomically replaces files and creates parent directories", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-atomic-writer-test-")); + const target = join(directory, "nested", "state.json"); + try { + await Effect.gen(function* () { + const writer = yield* AtomicFileWriterService; + yield* writer.write(target, "first\n"); + yield* writer.write(target, "second\n"); + }).pipe(Effect.provide(atomicFileWriterLayer), Effect.runPromise); + + expect(await readFile(target, "utf8")).toBe("second\n"); + expect( + (await readdir(join(directory, "nested"))).filter((name) => name.includes(".tmp-")), + ).toEqual([]); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); + + test("cleans its temporary file when replacement fails", async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-atomic-writer-test-")); + const target = join(directory, "cannot-replace"); + try { + await mkdir(target); + const program = Effect.gen(function* () { + const writer = yield* AtomicFileWriterService; + yield* writer.write(target, "content\n"); + }).pipe(Effect.provide(atomicFileWriterLayer), Effect.runPromise); + + await expect(program).rejects.toBeInstanceOf(AtomicFileWriterError); + expect((await readdir(directory)).filter((name) => name.includes(".tmp-"))).toEqual([]); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }); +}); diff --git a/src/providers/filesystem/file-lock.ts b/src/providers/filesystem/file-lock.ts new file mode 100644 index 0000000..f968960 --- /dev/null +++ b/src/providers/filesystem/file-lock.ts @@ -0,0 +1,387 @@ +import { dirname, join } from "node:path"; +import { Context, Effect, Layer, Predicate, Schedule, Schema } from "effect"; +import { RigError } from "../errors/rig-error"; +import { FileLockPlatformService } from "./file-lock-platform"; +import { FileLockRuntimeService } from "./file-lock-runtime"; + +export type FileLockOptions = { timeoutMs?: number; staleMs?: number; retryMs?: number }; + +type LockOwner = { token: string; pid: number; hostname: string; acquiredAt: string }; + +const FileLockOperation = Schema.Literals(["acquire", "release", "operation"]); +type FileLockOperation = typeof FileLockOperation.Type; + +export class FileLockError extends Schema.TaggedErrorClass()("FileLockError", { + operation: FileLockOperation, + lockPath: Schema.String, + cause: Schema.Defect(), +}) {} + +class FileLockContentionError extends Schema.TaggedErrorClass()( + "FileLockContentionError", + { lockPath: Schema.String }, +) {} + +class FileLockConfigService extends Context.Service< + FileLockConfigService, + { + readonly timeoutMs: number; + readonly staleMs: number; + readonly retryMs: number; + } +>()("@rendotdev/rig/settings/FileLockConfigService", { + make: Effect.gen(function* () { + const timeoutMs = 5_000; + const staleMs = 30_000; + const retryMs = 20; + return { timeoutMs, staleMs, retryMs } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockConfigService, FileLockConfigService.make); +} + +class FileLockOwnerService extends Context.Service< + FileLockOwnerService, + { + readonly read: (lockPath: string) => Effect.Effect; + readonly tryAcquire: (lockPath: string) => Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockOwnerService", { + make: Effect.gen(function* () { + const platform = yield* FileLockPlatformService; + const runtime = yield* FileLockRuntimeService; + const hasCode = (error: unknown, code: string) => { + const cause = Predicate.hasProperty(error, "cause") ? error.cause : error; + return Predicate.hasProperty(cause, "code") && cause.code === code; + }; + const mapAcquireError = (lockPath: string) => (cause: unknown) => + new FileLockError({ operation: "acquire", lockPath, cause }); + const read = Effect.fn("FileLockOwnerService.read")(function* (lockPath: string) { + return yield* platform.readText(join(lockPath, "owner.json")).pipe( + Effect.flatMap((content) => + Effect.try({ + try: () => JSON.parse(content) as Partial, + catch: () => undefined, + }), + ), + Effect.map((owner) => + typeof owner.token === "string" && + typeof owner.pid === "number" && + typeof owner.hostname === "string" && + typeof owner.acquiredAt === "string" + ? (owner as LockOwner) + : undefined, + ), + Effect.orElseSucceed(() => undefined), + ); + }); + const tryAcquire = Effect.fn("FileLockOwnerService.tryAcquire")(function* (lockPath: string) { + const created = yield* platform.makeDirectory(lockPath, false).pipe( + Effect.as(true), + Effect.catch((error) => + hasCode(error, "EEXIST") + ? Effect.succeed(false) + : Effect.fail(mapAcquireError(lockPath)(error)), + ), + ); + if (!created) return undefined; + const owner: LockOwner = { + token: yield* runtime.randomUuid, + pid: yield* runtime.processPid, + hostname: yield* runtime.hostName, + acquiredAt: yield* runtime.timestamp, + }; + return yield* platform + .writeText(join(lockPath, "owner.json"), `${JSON.stringify(owner)}\n`) + .pipe( + Effect.as(owner), + Effect.tapError(() => platform.removeDirectory(lockPath)), + Effect.mapError(mapAcquireError(lockPath)), + ); + }); + return { read, tryAcquire } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockOwnerService, FileLockOwnerService.make); +} + +class FileLockOwnerLivenessService extends Context.Service< + FileLockOwnerLivenessService, + { readonly isAlive: (owner: LockOwner) => Effect.Effect } +>()("@rendotdev/rig/settings/FileLockOwnerLivenessService", { + make: Effect.gen(function* () { + const runtime = yield* FileLockRuntimeService; + const hasCode = (error: unknown, code: string) => { + const cause = Predicate.hasProperty(error, "cause") ? error.cause : error; + return Predicate.hasProperty(cause, "code") && cause.code === code; + }; + const isAlive = Effect.fn("FileLockOwnerLivenessService.isAlive")(function* (owner: LockOwner) { + if (owner.hostname !== (yield* runtime.hostName)) return false; + return yield* runtime.killProcess(owner.pid, 0).pipe( + Effect.as(true), + Effect.catch((error) => Effect.succeed(!hasCode(error, "ESRCH"))), + ); + }); + return { isAlive } as const; + }), +}) { + static readonly layer = Layer.effect( + FileLockOwnerLivenessService, + FileLockOwnerLivenessService.make, + ); +} + +class FileLockReleaseService extends Context.Service< + FileLockReleaseService, + { readonly release: (lockPath: string, owner: LockOwner) => Effect.Effect } +>()("@rendotdev/rig/settings/FileLockReleaseService", { + make: Effect.gen(function* () { + const platform = yield* FileLockPlatformService; + const hasCode = (error: unknown, code: string) => { + const cause = Predicate.hasProperty(error, "cause") ? error.cause : error; + return Predicate.hasProperty(cause, "code") && cause.code === code; + }; + const mapReleaseError = (lockPath: string) => (cause: unknown) => + new FileLockError({ operation: "release", lockPath, cause }); + const release = Effect.fn("FileLockReleaseService.release")(function* ( + lockPath: string, + owner: LockOwner, + ) { + const current = yield* platform.readText(join(lockPath, "owner.json")).pipe( + Effect.flatMap((content) => + Effect.try({ + try: () => JSON.parse(content) as { token?: unknown }, + catch: mapReleaseError(lockPath), + }), + ), + Effect.catch((error) => + hasCode(error, "ENOENT") ? Effect.void : Effect.fail(mapReleaseError(lockPath)(error)), + ), + ); + const ownerChanged = !current || current.token !== owner.token; + if (ownerChanged) return; + yield* platform.removeDirectory(lockPath).pipe(Effect.mapError(mapReleaseError(lockPath))); + }); + return { release } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockReleaseService, FileLockReleaseService.make); +} + +class FileLockRecoveryService extends Context.Service< + FileLockRecoveryService, + { + readonly tryAcquire: (lockPath: string) => Effect.Effect; + readonly recover: (lockPath: string, staleMs: number) => Effect.Effect; + readonly release: (lockPath: string, owner: LockOwner) => Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockRecoveryService", { + make: Effect.gen(function* () { + const platform = yield* FileLockPlatformService; + const runtime = yield* FileLockRuntimeService; + const owners = yield* FileLockOwnerService; + const liveness = yield* FileLockOwnerLivenessService; + const releases = yield* FileLockReleaseService; + const hasCode = (error: unknown, code: string) => { + const cause = Predicate.hasProperty(error, "cause") ? error.cause : error; + return Predicate.hasProperty(cause, "code") && cause.code === code; + }; + const mapAcquireError = (lockPath: string) => (cause: unknown) => + new FileLockError({ operation: "acquire", lockPath, cause }); + const tryAcquire = Effect.fn("FileLockRecoveryService.tryAcquire")(function* ( + lockPath: string, + ) { + return yield* owners.tryAcquire(lockPath); + }); + const recover = Effect.fn("FileLockRecoveryService.recover")(function* ( + lockPath: string, + staleMs: number, + ) { + const modifiedAt = yield* platform + .modifiedAt(lockPath) + .pipe( + Effect.catch((error) => + hasCode(error, "ENOENT") ? Effect.void : Effect.fail(mapAcquireError(lockPath)(error)), + ), + ); + const lockIsMissingOrFresh = + modifiedAt === undefined || (yield* runtime.now) - modifiedAt <= staleMs; + if (lockIsMissingOrFresh) return false; + const owner = yield* owners.read(lockPath); + const ownerIsAlive = owner && (yield* liveness.isAlive(owner)); + if (ownerIsAlive) return false; + const stalePath = `${lockPath}.stale-${yield* runtime.randomUuid}`; + const renamed = yield* platform.rename(lockPath, stalePath).pipe( + Effect.as(true), + Effect.catch((error) => + hasCode(error, "ENOENT") + ? Effect.succeed(false) + : Effect.fail(mapAcquireError(lockPath)(error)), + ), + ); + if (!renamed) return false; + yield* platform.removeDirectory(stalePath).pipe(Effect.mapError(mapAcquireError(lockPath))); + return true; + }); + const release = Effect.fn("FileLockRecoveryService.release")(function* ( + lockPath: string, + owner: LockOwner, + ) { + yield* releases.release(lockPath, owner); + }); + return { tryAcquire, recover, release } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockRecoveryService, FileLockRecoveryService.make); +} + +class FileLockLeaseService extends Context.Service< + FileLockLeaseService, + { + readonly acquire: (params: { + lockPath: string; + timeoutMs: number; + staleMs: number; + retryMs: number; + }) => Effect.Effect; + readonly release: (lockPath: string, owner: LockOwner) => Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockLeaseService", { + make: Effect.gen(function* () { + const platform = yield* FileLockPlatformService; + const runtime = yield* FileLockRuntimeService; + const recovery = yield* FileLockRecoveryService; + const timeoutError = (lockPath: string, timeoutMs: number) => + new FileLockError({ + operation: "acquire", + lockPath, + cause: new RigError({ + code: "CONFIG_INVALID", + message: `Timed out waiting for lock: ${lockPath}`, + details: { lockPath, timeoutMs }, + }), + }); + const acquire = Effect.fn("FileLockLeaseService.acquire")(function* (params: { + lockPath: string; + timeoutMs: number; + staleMs: number; + retryMs: number; + }) { + yield* platform + .makeDirectory(dirname(params.lockPath), true) + .pipe( + Effect.mapError( + (cause) => + new FileLockError({ operation: "acquire", lockPath: params.lockPath, cause }), + ), + ); + const startedAt = yield* runtime.now; + const attempt = Effect.gen(function* () { + const owner = yield* recovery.tryAcquire(params.lockPath); + if (owner) return owner; + if (yield* recovery.recover(params.lockPath, params.staleMs)) { + const recoveredOwner = yield* recovery.tryAcquire(params.lockPath); + if (recoveredOwner) return recoveredOwner; + } + if ((yield* runtime.now) - startedAt >= params.timeoutMs) { + return yield* timeoutError(params.lockPath, params.timeoutMs); + } + yield* runtime.sleep(params.retryMs); + return yield* new FileLockContentionError({ lockPath: params.lockPath }); + }); + const retries = Math.max(1, Math.ceil(params.timeoutMs / Math.max(1, params.retryMs)) + 1); + return yield* Effect.retry(attempt, { + schedule: Schedule.recurs(retries), + while: (error) => error instanceof FileLockContentionError, + }).pipe( + Effect.catch((error) => + error instanceof FileLockContentionError + ? Effect.fail(timeoutError(params.lockPath, params.timeoutMs)) + : Effect.fail(error), + ), + ); + }); + const release = Effect.fn("FileLockLeaseService.release")(function* ( + lockPath: string, + owner: LockOwner, + ) { + yield* recovery.release(lockPath, owner); + }); + return { acquire, release } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockLeaseService, FileLockLeaseService.make); +} + +export class FileLockService extends Context.Service< + FileLockService, + { + readonly withLock: (params: { + targetPath: string; + options?: FileLockOptions; + operation: () => Effect.Effect; + }) => Effect.Effect; + } +>()("@rendotdev/rig/settings/FileLockService", { + make: Effect.gen(function* () { + const defaults = yield* FileLockConfigService; + const leases = yield* FileLockLeaseService; + const withLock = Effect.fn("FileLockService.withLock")(function* < + Result, + Error, + Requirements, + >(params: { + targetPath: string; + options?: FileLockOptions; + operation: () => Effect.Effect; + }) { + const lockPath = `${params.targetPath}.lock`; + return yield* Effect.acquireUseRelease( + leases.acquire({ + lockPath, + timeoutMs: params.options?.timeoutMs ?? defaults.timeoutMs, + staleMs: params.options?.staleMs ?? defaults.staleMs, + retryMs: params.options?.retryMs ?? defaults.retryMs, + }), + params.operation, + (owner) => leases.release(lockPath, owner), + ); + }); + return { withLock } as const; + }), +}) { + static readonly layer = Layer.effect(FileLockService, FileLockService.make); +} + +const fileLockOwnerLayer = FileLockOwnerService.layer.pipe( + Layer.provide(Layer.merge(FileLockPlatformService.layer, FileLockRuntimeService.layer)), +); +const fileLockOwnerLivenessLayer = FileLockOwnerLivenessService.layer.pipe( + Layer.provide(FileLockRuntimeService.layer), +); +const fileLockReleaseLayer = FileLockReleaseService.layer.pipe( + Layer.provide(FileLockPlatformService.layer), +); +const fileLockRecoveryLayer = FileLockRecoveryService.layer.pipe( + Layer.provide( + Layer.mergeAll( + FileLockPlatformService.layer, + FileLockRuntimeService.layer, + fileLockOwnerLayer, + fileLockOwnerLivenessLayer, + fileLockReleaseLayer, + ), + ), +); +const fileLockLeaseLayer = FileLockLeaseService.layer.pipe( + Layer.provide( + Layer.mergeAll( + FileLockPlatformService.layer, + FileLockRuntimeService.layer, + fileLockRecoveryLayer, + ), + ), +); +export const fileLockLayer = FileLockService.layer.pipe( + Layer.provide(Layer.merge(FileLockConfigService.layer, fileLockLeaseLayer)), +); diff --git a/src/providers/logging/rig-logger.test.ts b/src/providers/logging/rig-logger.test.ts new file mode 100644 index 0000000..a544bb0 --- /dev/null +++ b/src/providers/logging/rig-logger.test.ts @@ -0,0 +1,225 @@ +import { spawn } from "node:child_process"; +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, readdir, readFile, rm, utimes, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import type { RigError } from "../errors/rig-error"; +import { RigLoggerConfigService, RigLoggerService, rigLogDestinationLayer } from "./rig-logger"; + +const homes: string[] = []; +const fixedNow = Date.parse("2026-07-01T00:00:00.000Z"); + +const createHome = async () => { + const home = await mkdtemp(join(tmpdir(), "rig-logger-")); + homes.push(home); + return home; +}; + +const createLoggerLayer = (params: { + enabled?: boolean; + level?: string; + logDir: string; + maxFileSizeBytes?: number; + retentionDays?: number; + nowEpochMs?: number; +}) => + RigLoggerService.layer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(RigLoggerConfigService, { + enabled: params.enabled ?? true, + level: params.level ?? "info", + logDir: params.logDir, + maxFileSizeBytes: params.maxFileSizeBytes ?? 5 * 1024 * 1024, + retentionDays: params.retentionDays ?? 7, + nowEpochMs: params.nowEpochMs ?? fixedNow, + }), + rigLogDestinationLayer, + ), + ), + ); + +const writeLogs = ( + layer: Layer.Layer, + operation: (service: RigLoggerService["Service"]) => Effect.Effect, +) => Effect.runPromise(RigLoggerService.use(operation).pipe(Effect.provide(layer))); + +afterEach(async () => { + await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); +}); + +describe("rig logger", () => { + test("does not create the log directory until logging is requested", async () => { + const home = await createHome(); + const logDir = join(home, "rig", ".logs"); + + await Effect.runPromise( + RigLoggerService.use(() => Effect.void).pipe(Effect.provide(createLoggerLayer({ logDir }))), + ); + + expect(existsSync(logDir)).toBe(false); + }); + + test("writes prefixed pino logs, rolls files, and removes expired files", async () => { + const home = await createHome(); + const logDir = join(home, "rig", ".logs"); + await mkdir(logDir, { recursive: true }); + await writeFile(join(logDir, "rig-old.log"), "old\n", "utf8"); + await utimes( + join(logDir, "rig-old.log"), + new Date("2026-06-01T00:00:00.000Z"), + new Date("2026-06-01T00:00:00.000Z"), + ); + const layer = createLoggerLayer({ + logDir, + level: "trace", + maxFileSizeBytes: 180, + }); + await writeLogs(layer, (service) => + Effect.gen(function* () { + const app = yield* service.app("test"); + const tool = yield* service.tool("sample", "echo"); + yield* Effect.sync(() => { + app.debug("Debug app line."); + app.info({ ok: true }, "App log line."); + tool.info({ key: "value" }, "Tool log line."); + tool.warn("Another tool log line that rolls the active log."); + tool.error(new Error("boom"), "Tool error line."); + }); + }), + ); + + const entries = (await readdir(logDir)).toSorted(); + expect(entries).not.toContain("rig-old.log"); + expect(entries).toContain("rig.log"); + expect(entries.some((entry) => entry.startsWith("rig-2026-07-01T00-00-00-000Z"))).toBe(true); + const output = ( + await Promise.all( + entries + .filter((entry) => entry.endsWith(".log")) + .map((entry) => readFile(join(logDir, entry), "utf8")), + ) + ).join("\n"); + expect(output).toContain('"prefix":"rig:test"'); + expect(output).toContain('"prefix":"tool:sample.echo"'); + expect(output).toContain('"type":"Error"'); + }); + + test("supports disabled and debug logging configurations", async () => { + const home = await createHome(); + const disabledDir = join(home, "disabled"); + await writeLogs(createLoggerLayer({ enabled: false, logDir: disabledDir }), (service) => + service.app("disabled").pipe( + Effect.tap((logger) => Effect.sync(() => logger.info("This should not be written."))), + Effect.asVoid, + ), + ); + expect(existsSync(disabledDir)).toBe(false); + + const debugDir = join(home, "debug"); + await writeLogs(createLoggerLayer({ logDir: debugDir, level: "debug" }), (service) => + service.app("env").pipe( + Effect.tap((logger) => Effect.sync(() => logger.debug("Configured debug line."))), + Effect.asVoid, + ), + ); + expect(await readFile(join(debugDir, "rig.log"), "utf8")).toContain("Configured debug line."); + }); + + test("recovers an abandoned rotation lock and keeps the active log", async () => { + const home = await createHome(); + const logDir = join(home, "logs"); + const lockPath = join(logDir, ".rig.log.rotation.lock"); + await mkdir(lockPath, { recursive: true }); + await writeFile(join(logDir, "rig.log"), "active\n", "utf8"); + const old = new Date(fixedNow - 60_000); + await utimes(lockPath, old, old); + await writeLogs(createLoggerLayer({ logDir }), (service) => + service.app("recovery").pipe( + Effect.tap((logger) => Effect.sync(() => logger.info("Recovered logging."))), + Effect.asVoid, + ), + ); + expect(existsSync(lockPath)).toBe(false); + expect(await readFile(join(logDir, "rig.log"), "utf8")).toContain("active\n"); + expect(await readFile(join(logDir, "rig.log"), "utf8")).toContain("Recovered logging."); + }); + + test("preserves every record while separate processes rotate the same log", async () => { + const home = await createHome(); + const logDir = join(home, "logs"); + const loggerUrl = new URL("./rig-logger.ts", import.meta.url).href; + const children = Array.from({ length: 4 }, (_, processIndex) => { + const source = ` + import { Effect, Layer } from "effect"; + import { RigLoggerConfigService, RigLoggerService, rigLogDestinationLayer } from ${JSON.stringify(loggerUrl)}; + const layer = RigLoggerService.layer.pipe(Layer.provide(Layer.merge( + Layer.succeed(RigLoggerConfigService, { + enabled: true, + level: "info", + logDir: ${JSON.stringify(logDir)}, + maxFileSizeBytes: 500, + retentionDays: 7, + nowEpochMs: Date.now(), + }), + rigLogDestinationLayer, + ))); + await Effect.runPromise(RigLoggerService.use((service) => Effect.gen(function* () { + const logger = yield* service.app("child"); + yield* Effect.sync(() => { + for (let line = 0; line < 20; line += 1) logger.info("record-${processIndex}-" + line); + }); + })).pipe(Effect.provide(layer))); + `; + return spawn("bun", ["-e", source], { stdio: "pipe" }); + }); + const exits = await Promise.all( + children.map( + (child) => + new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", resolve); + }), + ), + ); + expect(exits).toEqual([0, 0, 0, 0]); + const entries = (await readdir(logDir)).filter((entry) => entry.endsWith(".log")); + const output = ( + await Promise.all(entries.map((entry) => readFile(join(logDir, entry), "utf8"))) + ).join("\n"); + expect(new Set(output.match(/record-\d+-\d+/gu)).size).toBe(80); + expect(existsSync(join(logDir, ".rig.log.rotation.lock"))).toBe(false); + }); + + test("rechecks active size after a competing process rotates while waiting", async () => { + const home = await createHome(); + const logDir = join(home, "logs"); + const lockPath = join(logDir, ".rig.log.rotation.lock"); + const activePath = join(logDir, "rig.log"); + await mkdir(logDir, { recursive: true }); + await writeFile(activePath, "x".repeat(200), "utf8"); + await mkdir(lockPath); + const source = ` + import { rm, writeFile } from "node:fs/promises"; + await new Promise((resolve) => setTimeout(resolve, 100)); + await writeFile(${JSON.stringify(activePath)}, "", "utf8"); + await rm(${JSON.stringify(lockPath)}, { recursive: true, force: true }); + `; + const child = spawn("bun", ["-e", source], { stdio: "pipe" }); + await writeLogs(createLoggerLayer({ logDir, maxFileSizeBytes: 100 }), (service) => + service.app("race").pipe( + Effect.tap((logger) => Effect.sync(() => logger.info("Written after competing rotation."))), + Effect.asVoid, + ), + ); + const exit = await new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", resolve); + }); + expect(exit).toBe(0); + expect(await readFile(activePath, "utf8")).toContain("Written after competing rotation."); + expect(existsSync(lockPath)).toBe(false); + }); +}); diff --git a/src/providers/logging/rig-logger.ts b/src/providers/logging/rig-logger.ts new file mode 100644 index 0000000..a3523d9 --- /dev/null +++ b/src/providers/logging/rig-logger.ts @@ -0,0 +1,333 @@ +import { + appendFileSync, + existsSync, + mkdirSync, + readdirSync, + renameSync, + rmSync, + statSync, + unlinkSync, +} from "node:fs"; +import { join } from "node:path"; +import { Writable } from "node:stream"; +import { Context, Effect, Layer, Scope } from "effect"; +import pino, { type Logger } from "pino"; +import { RigError } from "../errors/rig-error"; +import { RigPathsConfigService, rigPathsConfigLayer } from "../paths/rig-paths"; + +const DEFAULT_MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024; +const DEFAULT_RETENTION_DAYS = 7; +const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; + +export type ResolvedRigLoggerConfig = Readonly<{ + enabled: boolean; + level: string; + logDir: string; + maxFileSizeBytes: number; + retentionDays: number; + nowEpochMs: number; +}>; + +export type LogRotationLock = Readonly<{ run: (operation: () => void) => boolean }>; +export type RigLogFile = Readonly<{ append: (buffers: Buffer[]) => void }>; + +export class RigLoggerConfigService extends Context.Service< + RigLoggerConfigService, + { + readonly enabled: boolean; + readonly level: string; + readonly logDir: string; + readonly maxFileSizeBytes: number; + readonly retentionDays: number; + readonly nowEpochMs: number; + } +>()("@rendotdev/rig/runtime/RigLoggerConfigService", { + make: Effect.gen(function* () { + const paths = yield* RigPathsConfigService; + const positiveNumber = (value: string | undefined, fallback: number): number => { + const parsed = value ? Number(value) : undefined; + return parsed && Number.isFinite(parsed) && parsed > 0 ? parsed : fallback; + }; + const enabled = process.env.RIG_LOG !== "0"; + const level = process.env.RIG_LOG_LEVEL ?? "info"; + const logDir = process.env.RIG_LOG_DIR ?? paths.logsDir; + const maxFileSizeBytes = positiveNumber( + process.env.RIG_LOG_MAX_BYTES, + DEFAULT_MAX_FILE_SIZE_BYTES, + ); + const retentionDays = positiveNumber( + process.env.RIG_LOG_RETENTION_DAYS, + DEFAULT_RETENTION_DAYS, + ); + const nowEpochMs = Date.now(); + return { + enabled, + level, + logDir, + maxFileSizeBytes, + retentionDays, + nowEpochMs, + } as const; + }), +}) { + static readonly layer = Layer.effect(RigLoggerConfigService, RigLoggerConfigService.make); +} + +export class LogRotationLockService extends Context.Service< + LogRotationLockService, + { + readonly create: (params: { + readonly logDir: string; + readonly nowEpochMs: number; + readonly waitTimeoutMs?: number; + readonly staleAfterMs?: number; + }) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/LogRotationLockService", { + make: Effect.gen(function* () { + const create = Effect.fn("LogRotationLockService.create")(function* (params: { + readonly logDir: string; + readonly nowEpochMs: number; + readonly waitTimeoutMs?: number; + readonly staleAfterMs?: number; + }) { + const lockPath = join(params.logDir, ".rig.log.rotation.lock"); + const waitBuffer = new Int32Array(new SharedArrayBuffer(4)); + const errorCode = (error: unknown): string | undefined => + typeof error === "object" && error !== null && "code" in error + ? String(error.code) + : undefined; + const tryAcquire = (): boolean => { + try { + mkdirSync(lockPath); + return true; + } catch (error) { + if (errorCode(error) === "EEXIST") return false; + throw error; + } + }; + const recoverStaleLock = () => { + try { + if (params.nowEpochMs - statSync(lockPath).mtimeMs <= (params.staleAfterMs ?? 30_000)) { + return; + } + const stalePath = `${lockPath}.stale-${process.pid}-${params.nowEpochMs}`; + renameSync(lockPath, stalePath); + rmSync(stalePath, { recursive: true, force: true }); + } catch (error) { + if (["ENOENT", "EEXIST"].includes(errorCode(error) ?? "")) return; + throw error; + } + }; + const run = (operation: () => void): boolean => { + const startedAt = Date.now(); + while (!tryAcquire()) { + recoverStaleLock(); + if (Date.now() - startedAt >= (params.waitTimeoutMs ?? 1_000)) return false; + Atomics.wait(waitBuffer, 0, 0, 10); + } + try { + operation(); + return true; + } finally { + rmSync(lockPath, { recursive: true, force: true }); + } + }; + return { run } as const; + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(LogRotationLockService, LogRotationLockService.make); +} + +export class RigLogFileService extends Context.Service< + RigLogFileService, + { + readonly create: (config: ResolvedRigLoggerConfig) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/RigLogFileService", { + make: Effect.gen(function* () { + const locks = yield* LogRotationLockService; + const create = Effect.fn("RigLogFileService.create")(function* ( + config: ResolvedRigLoggerConfig, + ) { + const lock = yield* locks.create({ + logDir: config.logDir, + nowEpochMs: config.nowEpochMs, + }); + return yield* Effect.try({ + try: () => { + const activePath = join(config.logDir, "rig.log"); + const size = () => { + try { + return statSync(activePath).size; + } catch { + return 0; + } + }; + const cleanup = () => { + const cutoff = config.nowEpochMs - config.retentionDays * MILLISECONDS_PER_DAY; + for (const entry of readdirSync(config.logDir)) { + const path = join(config.logDir, entry); + try { + const isExpiredArchive = + /^rig-.*\.log$/u.test(entry) && statSync(path).mtimeMs < cutoff; + if (isExpiredArchive) unlinkSync(path); + } catch { + continue; + } + } + }; + const archivePath = () => { + const timestamp = new Date(config.nowEpochMs).toISOString().replace(/[:.]/gu, "-"); + for (let index = 0; ; index += 1) { + const suffix = index === 0 ? "" : `-${index}`; + const candidate = join(config.logDir, `rig-${timestamp}${suffix}.log`); + if (!existsSync(candidate)) return candidate; + } + }; + mkdirSync(config.logDir, { recursive: true }); + lock.run(cleanup); + let activeSize = size(); + const rotate = (nextBytes: number) => { + lock.run(() => { + const lockedSize = size(); + const hasRoomInActiveLog = + lockedSize === 0 || lockedSize + nextBytes <= config.maxFileSizeBytes; + if (hasRoomInActiveLog) { + activeSize = lockedSize; + return; + } + renameSync(activePath, archivePath()); + activeSize = 0; + cleanup(); + }); + }; + const append = (buffers: Buffer[]) => { + for (const buffer of buffers) { + if (activeSize + buffer.byteLength > config.maxFileSizeBytes) { + rotate(buffer.byteLength); + } + appendFileSync(activePath, buffer); + activeSize += buffer.byteLength; + } + }; + return { append } as const; + }, + catch: (cause) => + new RigError({ code: "INTERNAL_ERROR", message: String(cause), details: cause }), + }); + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(RigLogFileService, RigLogFileService.make); +} + +export class RigLogDestinationService extends Context.Service< + RigLogDestinationService, + { + readonly create: (config: ResolvedRigLoggerConfig) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/RigLogDestinationService", { + make: Effect.gen(function* () { + const files = yield* RigLogFileService; + const create = Effect.fn("RigLogDestinationService.create")(function* ( + config: ResolvedRigLoggerConfig, + ) { + const file = yield* files.create(config); + return new Writable({ + write: (chunk, _encoding, callback) => { + try { + file.append([Buffer.from(chunk)]); + queueMicrotask(callback); + } catch (cause) { + callback(cause instanceof Error ? cause : new Error(String(cause))); + } + }, + writev: (chunks, callback) => { + try { + file.append(chunks.map((entry) => Buffer.from(entry.chunk))); + queueMicrotask(callback); + } catch (cause) { + callback(cause instanceof Error ? cause : new Error(String(cause))); + } + }, + }); + }); + return { create } as const; + }), +}) { + static readonly layer = Layer.effect(RigLogDestinationService, RigLogDestinationService.make); +} + +export class RigLoggerService extends Context.Service< + RigLoggerService, + { + readonly app: (component?: string) => Effect.Effect; + readonly tool: (tool: string, command: string) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/RigLoggerService", { + make: Effect.gen(function* () { + const config = yield* RigLoggerConfigService; + const destinations = yield* RigLogDestinationService; + const scope = yield* Scope.Scope; + const resource = yield* Effect.cached( + Effect.acquireRelease( + Effect.gen(function* () { + if (!config.enabled) return { logger: pino({ enabled: false }) } as const; + const destination = yield* destinations.create(config); + const logger = pino( + { + name: "rig", + level: config.level, + base: { app: "rig", pid: process.pid }, + timestamp: pino.stdTimeFunctions.isoTime, + serializers: { err: pino.stdSerializers.err }, + }, + destination, + ); + return { logger, destination } as const; + }), + (value) => + Effect.sync(() => { + value.logger.flush(); + if ("destination" in value) value.destination?.end(); + }), + ).pipe(Scope.provide(scope)), + ); + const app = Effect.fn("RigLoggerService.app")(function* (component = "app") { + const acquired = yield* resource; + return acquired.logger.child({ prefix: `rig:${component}`, component }); + }); + const tool = Effect.fn("RigLoggerService.tool")(function* (toolName: string, command: string) { + const acquired = yield* resource; + return acquired.logger.child({ + prefix: `tool:${toolName}.${command}`, + component: "tool", + tool: toolName, + command, + }); + }); + return { app, tool } as const; + }), +}) { + static readonly layer = Layer.effect(RigLoggerService, RigLoggerService.make); +} + +export const rigLoggerConfigLayer = RigLoggerConfigService.layer.pipe( + Layer.provide(rigPathsConfigLayer), +); + +export const rigLogFileLayer = RigLogFileService.layer.pipe( + Layer.provide(LogRotationLockService.layer), +); + +export const rigLogDestinationLayer = RigLogDestinationService.layer.pipe( + Layer.provide(rigLogFileLayer), +); + +export const rigLoggerLayer = RigLoggerService.layer.pipe( + Layer.provide(Layer.merge(rigLoggerConfigLayer, rigLogDestinationLayer)), +); diff --git a/src/providers/output/output-truncator.test.ts b/src/providers/output/output-truncator.test.ts new file mode 100644 index 0000000..b8c2cb0 --- /dev/null +++ b/src/providers/output/output-truncator.test.ts @@ -0,0 +1,93 @@ +import { Effect, Layer } from "effect"; +import { describe, expect, test } from "vite-plus/test"; +import { + DEFAULT_TRUNCATION_MAX_BYTES, + DEFAULT_TRUNCATION_MAX_LINES, + OutputTruncatorConfigService, + OutputTruncatorPlatformService, + OutputTruncatorService, + outputTruncatorLayer, +} from "./output-truncator"; + +describe("output truncation", () => { + test("uses the documented defaults", () => { + expect(DEFAULT_TRUNCATION_MAX_BYTES).toBe(50 * 1024); + expect(DEFAULT_TRUNCATION_MAX_LINES).toBe(2000); + }); + + test("returns data unchanged within limits", async () => { + await expect( + Effect.runPromise( + OutputTruncatorService.use((service) => service.truncate({ ok: true })).pipe( + Effect.provide(outputTruncatorLayer), + ), + ), + ).resolves.toEqual({ ok: true }); + await expect( + Effect.runPromise( + OutputTruncatorService.use((service) => service.truncate(undefined)).pipe( + Effect.provide(outputTruncatorLayer), + ), + ), + ).resolves.toBeUndefined(); + }); + + test("writes full output and returns truncation metadata", async () => { + const writes: string[] = []; + const layer = OutputTruncatorService.layer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(OutputTruncatorConfigService, { + limits: { maxBytes: 10, maxLines: 2 }, + }), + Layer.succeed(OutputTruncatorPlatformService, { + writeFullOutput: Effect.fn("OutputTruncatorPlatformService.writeFullOutput")( + function* () { + const path = "/tmp/rig-test-output/data.json"; + writes.push(path); + return path; + }, + ), + }), + ), + ), + ); + await expect( + Effect.runPromise( + OutputTruncatorService.use((service) => + service.truncate({ text: "a\nb\nc\nd and more" }), + ).pipe(Effect.provide(layer)), + ), + ).resolves.toMatchObject({ + truncated: true, + strategy: "head", + previewFormat: "partial-json", + fullOutputFormat: "json", + maxBytes: 10, + maxLines: 2, + }); + expect(writes).toEqual(["/tmp/rig-test-output/data.json"]); + }); + + test("exposes text and byte formatting through the service", () => { + const run = ( + operation: (service: OutputTruncatorService["Service"]) => Effect.Effect, + ) => + Effect.runSync( + OutputTruncatorService.use(operation).pipe(Effect.provide(outputTruncatorLayer)), + ); + expect( + run((service) => service.truncateText("", { maxBytes: 10, maxLines: 10 })), + ).toMatchObject({ + totalBytes: 0, + totalLines: 0, + truncated: false, + }); + expect( + run((service) => service.truncateText("abcdef", { maxBytes: 3, maxLines: 10 })), + ).toMatchObject({ content: "abc", truncated: true }); + expect(run((service) => service.formatByteSize(500))).toBe("500B"); + expect(run((service) => service.formatByteSize(1536))).toBe("1.5KB"); + expect(run((service) => service.formatByteSize(3 * 1024 * 1024))).toBe("3.0MB"); + }); +}); diff --git a/src/providers/output/output-truncator.ts b/src/providers/output/output-truncator.ts new file mode 100644 index 0000000..b04f274 --- /dev/null +++ b/src/providers/output/output-truncator.ts @@ -0,0 +1,186 @@ +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Context, Effect, Layer, Schema } from "effect"; + +export const DEFAULT_TRUNCATION_MAX_BYTES = 50 * 1024; +export const DEFAULT_TRUNCATION_MAX_LINES = 2000; + +export type TextTruncationResult = { + content: string; + truncated: boolean; + totalBytes: number; + totalLines: number; + outputBytes: number; + outputLines: number; +}; + +export type OutputTruncationOptions = { maxBytes?: number; maxLines?: number }; + +export class OutputTruncationError extends Schema.TaggedErrorClass()( + "OutputTruncationError", + { cause: Schema.Defect() }, +) {} + +export class OutputTruncatorConfigService extends Context.Service< + OutputTruncatorConfigService, + { + readonly limits: Required; + } +>()("@rendotdev/rig/runtime/OutputTruncatorConfigService", { + make: Effect.succeed({ + limits: { + maxBytes: DEFAULT_TRUNCATION_MAX_BYTES, + maxLines: DEFAULT_TRUNCATION_MAX_LINES, + }, + }), +}) { + static readonly layer = Layer.effect( + OutputTruncatorConfigService, + OutputTruncatorConfigService.make, + ); +} + +export class OutputTruncatorPlatformService extends Context.Service< + OutputTruncatorPlatformService, + { + readonly writeFullOutput: (serialized: string) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/OutputTruncatorPlatformService", { + make: Effect.gen(function* () { + const writeFullOutput = Effect.fn("OutputTruncatorPlatformService.writeFullOutput")(function* ( + serialized: string, + ) { + return yield* Effect.tryPromise({ + try: async () => { + const directory = await mkdtemp(join(tmpdir(), "rig-output-")); + try { + const path = join(directory, "data.json"); + /* v8 ignore next -- Node fallback supports generated consumers without Bun globals */ + const canWriteWithBun = typeof Bun !== "undefined" && typeof Bun.write === "function"; + /* v8 ignore next -- Node fallback supports generated consumers without Bun globals */ + if (canWriteWithBun) { + await Bun.write(path, `${serialized}\n`); + } else { + /* v8 ignore next */ + await writeFile(path, `${serialized}\n`, "utf8"); + } + return path; + } catch (cause) { + /* v8 ignore next -- cleanup after a platform write failure is defensive */ + await rm(directory, { recursive: true, force: true }); + /* v8 ignore next */ + throw cause; + } + }, + /* v8 ignore next -- platform I/O fault translation is defensive */ + catch: (cause) => new OutputTruncationError({ cause }), + }); + }); + return { writeFullOutput } as const; + }), +}) { + static readonly layer = Layer.effect( + OutputTruncatorPlatformService, + OutputTruncatorPlatformService.make, + ); +} + +export class OutputTruncatorService extends Context.Service< + OutputTruncatorService, + { + readonly truncate: (data: unknown) => Effect.Effect; + readonly truncateText: ( + value: string, + options: Required, + ) => Effect.Effect; + readonly formatByteSize: (bytes: number) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/OutputTruncatorService", { + make: Effect.gen(function* () { + const config = yield* OutputTruncatorConfigService; + const platform = yield* OutputTruncatorPlatformService; + + const computeTextTruncation = ( + value: string, + options: Required, + ): TextTruncationResult => { + const encoder = new TextEncoder(); + const decoder = new TextDecoder(); + const totalBytes = encoder.encode(value).byteLength; + const totalLines = value.length === 0 ? 0 : value.split("\n").length; + let content = value; + if (totalLines > options.maxLines) { + content = content.split("\n").slice(0, options.maxLines).join("\n"); + } + if (encoder.encode(content).byteLength > options.maxBytes) { + content = decoder.decode(encoder.encode(content).slice(0, options.maxBytes)); + } + const outputBytes = encoder.encode(content).byteLength; + const outputLines = content.length === 0 ? 0 : content.split("\n").length; + return { + content, + truncated: outputBytes < totalBytes || outputLines < totalLines, + totalBytes, + totalLines, + outputBytes, + outputLines, + }; + }; + + const renderByteSize = (bytes: number): string => { + if (bytes < 1024) return `${bytes}B`; + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`; + return `${(bytes / (1024 * 1024)).toFixed(1)}MB`; + }; + + const truncate = Effect.fn("OutputTruncatorService.truncate")(function* (data: unknown) { + const serialized = yield* Effect.try({ + try: () => JSON.stringify(data, null, 2) ?? "null", + catch: (cause) => new OutputTruncationError({ cause }), + }); + const result = computeTextTruncation(serialized, config.limits); + if (!result.truncated) return data; + const fullOutputPath = yield* platform.writeFullOutput(serialized); + return { + truncated: true, + strategy: "head", + preview: result.content, + previewFormat: "partial-json", + fullOutputPath, + fullOutputFormat: "json", + ...config.limits, + totalBytes: result.totalBytes, + totalLines: result.totalLines, + shownBytes: result.outputBytes, + shownLines: result.outputLines, + omittedBytes: result.totalBytes - result.outputBytes, + omittedLines: result.totalLines - result.outputLines, + message: `Output truncated: showing ${result.outputLines} of ${result.totalLines} lines (${renderByteSize(result.outputBytes)} of ${renderByteSize(result.totalBytes)}). Full output saved to: ${fullOutputPath}`, + }; + }); + + const truncateText = Effect.fn("OutputTruncatorService.truncateText")(function* ( + value: string, + options: Required, + ) { + return computeTextTruncation(value, options); + }); + + const formatByteSize = Effect.fn("OutputTruncatorService.formatByteSize")(function* ( + bytes: number, + ) { + return renderByteSize(bytes); + }); + + return { truncate, truncateText, formatByteSize } as const; + }), +}) { + static readonly layer = Layer.effect(OutputTruncatorService, OutputTruncatorService.make); +} + +export const outputTruncatorLayer = OutputTruncatorService.layer.pipe( + Layer.provide( + Layer.merge(OutputTruncatorConfigService.layer, OutputTruncatorPlatformService.layer), + ), +); diff --git a/src/providers/package/package-root.test.ts b/src/providers/package/package-root.test.ts new file mode 100644 index 0000000..2283862 --- /dev/null +++ b/src/providers/package/package-root.test.ts @@ -0,0 +1,147 @@ +import { posix } from "node:path"; +import { fileURLToPath } from "node:url"; +import { Effect, Layer } from "effect"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { RigError } from "../errors/rig-error"; +import { + type PackageRootDependencies, + PackageRootPlatformService, + PackageRootService, +} from "./package-root"; + +function createHarness() { + const state = { + environmentRoot: undefined as string | undefined, + argvEntrypoint: undefined as string | undefined, + execPath: "/runtime/bin/node", + }; + const existingPaths = new Set(); + const realpaths = new Map(); + const getEnvironmentRoot = vi.fn(() => state.environmentRoot); + const getArgvEntrypoint = vi.fn(() => state.argvEntrypoint); + const getExecPath = vi.fn(() => state.execPath); + const dependencies: PackageRootDependencies = { + getEnvironmentRoot, + getArgvEntrypoint, + getExecPath, + exists: (pathValue) => existingPaths.has(pathValue), + realpath: (pathValue) => { + const realpath = realpaths.get(pathValue); + if (realpath) return realpath; + throw new Error(`Missing path: ${pathValue}`); + }, + basename: posix.basename, + dirname: posix.dirname, + join: posix.join, + resolve: posix.resolve, + fileURLToPath, + }; + const layer = PackageRootService.layer.pipe( + Layer.provide( + Layer.succeed(PackageRootPlatformService, { + environmentRoot: Effect.sync(dependencies.getEnvironmentRoot), + argvEntrypoint: Effect.sync(dependencies.getArgvEntrypoint), + execPath: Effect.sync(dependencies.getExecPath), + exists: (pathValue) => Effect.sync(() => dependencies.exists(pathValue)), + realpath: (pathValue) => + Effect.try({ + try: () => dependencies.realpath(pathValue), + catch: (cause) => + new RigError({ + code: "INTERNAL_ERROR", + message: cause instanceof Error ? cause.message : String(cause), + details: { cause }, + }), + }), + basename: (pathValue) => Effect.sync(() => dependencies.basename(pathValue)), + dirname: (pathValue) => Effect.sync(() => dependencies.dirname(pathValue)), + join: (parts) => Effect.sync(() => dependencies.join(...parts)), + resolve: (pathValue) => Effect.sync(() => dependencies.resolve(pathValue)), + fileUrlToPath: (metaUrl) => Effect.sync(() => dependencies.fileURLToPath(metaUrl)), + }), + ), + ); + const run = ( + operation: (service: PackageRootService["Service"]) => Effect.Effect, + ) => Effect.runSync(PackageRootService.use(operation).pipe(Effect.provide(layer))); + return { + run, + state, + existingPaths, + realpaths, + getEnvironmentRoot, + getArgvEntrypoint, + }; +} + +describe("PackageRootService", () => { + it("preserves environment, argv, executable, and Bun lookup precedence", () => { + const harness = createHarness(); + harness.state.environmentRoot = "/configured/../configured-root"; + expect(harness.run((service) => service.find("file:///module.ts"))).toBe("/configured-root"); + expect(harness.getArgvEntrypoint).not.toHaveBeenCalled(); + + harness.state.environmentRoot = undefined; + harness.state.argvEntrypoint = "/workspace/src/cli.ts"; + harness.realpaths.set("/workspace/src/cli.ts", "/workspace/src/cli.ts"); + expect(harness.run((service) => service.find("file:///module.ts"))).toBe("/workspace"); + + harness.state.argvEntrypoint = "/missing/cli.ts"; + harness.state.execPath = "/installed/dist/rig.mjs"; + harness.realpaths.set("/installed/dist/rig.mjs", "/installed/dist/rig.mjs"); + expect(harness.run((service) => service.find("file:///module.ts"))).toBe("/installed"); + + harness.state.execPath = "/bun/bin/bun"; + expect(harness.run((service) => service.find("file:///$bunfs/root"))).toBe("/bun/bin"); + expect(harness.getEnvironmentRoot).toHaveBeenCalledTimes(4); + }); + + it("reads dynamic process and environment dependencies for every find call", () => { + const harness = createHarness(); + harness.state.environmentRoot = "/first"; + expect(harness.run((service) => service.find("file:///module.ts"))).toBe("/first"); + harness.state.environmentRoot = "/second"; + expect(harness.run((service) => service.find("file:///module.ts"))).toBe("/second"); + expect(harness.getEnvironmentRoot).toHaveBeenCalledTimes(2); + }); + + it("resolves entrypoints through realpaths and safe absolute fallbacks", () => { + const harness = createHarness(); + expect(harness.run((service) => service.fromEntrypoint(undefined))).toBeUndefined(); + harness.realpaths.set("/link", "/repo/src/cli.ts"); + expect(harness.run((service) => service.fromEntrypoint("/link"))).toBe("/repo"); + harness.realpaths.set("/dist-link", "/repo/dist/rig.mjs"); + expect(harness.run((service) => service.fromEntrypoint("/dist-link"))).toBe("/repo"); + harness.realpaths.set("/other", "/repo/bin/rig"); + expect(harness.run((service) => service.fromEntrypoint("/other"))).toBeUndefined(); + harness.existingPaths.add("/repo/src/fallback.ts"); + expect(harness.run((service) => service.fromEntrypoint("/repo/src/fallback.ts"))).toBe("/repo"); + expect(harness.run((service) => service.fromEntrypoint("/missing"))).toBeUndefined(); + }); + + it("finds package files from modules and preserves the final fallback", () => { + const harness = createHarness(); + harness.state.argvEntrypoint = "/missing/cli.ts"; + harness.state.execPath = "/missing/node"; + harness.existingPaths.add("/workspace/package.json"); + expect( + harness.run((service) => service.fromModule("file:///workspace/src/deep/module.ts")), + ).toBe("/workspace"); + expect(harness.run((service) => service.find("file:///workspace/src/deep/module.ts"))).toBe( + "/workspace", + ); + expect( + harness.run((service) => + service.packageFile("file:///workspace/src/deep/module.ts", "dist", "rig.mjs"), + ), + ).toBe("/workspace/dist/rig.mjs"); + harness.existingPaths.clear(); + expect(harness.run((service) => service.fromModule("file:///module.ts"))).toBeUndefined(); + expect( + harness.run((service) => service.fromModule("file:///a/b/c/d/e/f/g/h/i/j/module.ts")), + ).toBeUndefined(); + expect(harness.run((service) => service.find("file:///a/b/c/d/e/f/g/h/i/j/module.ts"))).toBe( + "/a/b/c/d/e/f/g/h", + ); + }); +}); diff --git a/src/providers/package/package-root.ts b/src/providers/package/package-root.ts new file mode 100644 index 0000000..abd438e --- /dev/null +++ b/src/providers/package/package-root.ts @@ -0,0 +1,175 @@ +import { existsSync, realpathSync } from "node:fs"; +import { + basename as pathBasename, + dirname as pathDirname, + join as pathJoin, + resolve as pathResolve, +} from "node:path"; +import { fileURLToPath } from "node:url"; +import { Context, Effect, Layer, Option } from "effect"; + +export type PackageRootDependencies = Readonly<{ + getEnvironmentRoot: () => string | undefined; + getArgvEntrypoint: () => string | undefined; + getExecPath: () => string; + exists: (pathValue: string) => boolean; + realpath: (pathValue: string) => string; + basename: (pathValue: string) => string; + dirname: (pathValue: string) => string; + join: (...parts: string[]) => string; + resolve: (pathValue: string) => string; + fileURLToPath: (metaUrl: string) => string; +}>; + +export class PackageRootPlatformService extends Context.Service< + PackageRootPlatformService, + { + readonly environmentRoot: Effect.Effect; + readonly argvEntrypoint: Effect.Effect; + readonly execPath: Effect.Effect; + readonly exists: (pathValue: string) => Effect.Effect; + readonly realpath: (pathValue: string) => Effect.Effect; + readonly basename: (pathValue: string) => Effect.Effect; + readonly dirname: (pathValue: string) => Effect.Effect; + readonly join: (parts: string[]) => Effect.Effect; + readonly resolve: (pathValue: string) => Effect.Effect; + readonly fileUrlToPath: (metaUrl: string) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/PackageRootPlatformService", { + make: Effect.gen(function* () { + const environmentRoot = Effect.sync(() => process.env.RIG_PACKAGE_ROOT).pipe( + Effect.withSpan("PackageRootPlatformService.environmentRoot"), + ); + const argvEntrypoint = Effect.sync(() => process.argv[1]).pipe( + Effect.withSpan("PackageRootPlatformService.argvEntrypoint"), + ); + const execPath = Effect.sync(() => process.execPath).pipe( + Effect.withSpan("PackageRootPlatformService.execPath"), + ); + const exists = Effect.fn("PackageRootPlatformService.exists")(function* (pathValue: string) { + return existsSync(pathValue); + }); + const realpath = Effect.fn("PackageRootPlatformService.realpath")(function* ( + pathValue: string, + ) { + return yield* Effect.try(() => realpathSync(pathValue)); + }); + const basename = Effect.fn("PackageRootPlatformService.basename")(function* ( + pathValue: string, + ) { + return pathBasename(pathValue); + }); + const dirname = Effect.fn("PackageRootPlatformService.dirname")(function* (pathValue: string) { + return pathDirname(pathValue); + }); + const join = Effect.fn("PackageRootPlatformService.join")(function* (parts: string[]) { + return pathJoin(...parts); + }); + const resolve = Effect.fn("PackageRootPlatformService.resolve")(function* (pathValue: string) { + return pathResolve(pathValue); + }); + const fileUrlToPath = Effect.fn("PackageRootPlatformService.fileUrlToPath")(function* ( + metaUrl: string, + ) { + return fileURLToPath(metaUrl); + }); + return { + environmentRoot, + argvEntrypoint, + execPath, + exists, + realpath, + basename, + dirname, + join, + resolve, + fileUrlToPath, + } as const; + }), +}) { + static readonly layer = Layer.effect(PackageRootPlatformService, PackageRootPlatformService.make); +} + +export class PackageRootService extends Context.Service< + PackageRootService, + { + readonly fromEntrypoint: (entrypoint: string | undefined) => Effect.Effect; + readonly fromModule: (metaUrl: string) => Effect.Effect; + readonly find: (metaUrl: string) => Effect.Effect; + readonly packageFile: (metaUrl: string, ...parts: string[]) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/PackageRootService", { + make: Effect.gen(function* () { + const platform = yield* PackageRootPlatformService; + + const isBunBinary = (metaUrl: string) => + metaUrl.includes("$bunfs") || metaUrl.includes("~BUN") || metaUrl.includes("%7EBUN"); + + const safeRealpath = Effect.fn("PackageRootService.safeRealpath")(function* ( + pathValue: string, + ) { + const resolved = yield* Effect.option(platform.realpath(pathValue)); + if (Option.isSome(resolved)) return resolved.value; + const absolute = yield* platform.resolve(pathValue); + /* v8 ignore next */ + return (yield* platform.exists(absolute)) ? absolute : undefined; + }); + + const fromEntrypoint = Effect.fn("PackageRootService.fromEntrypoint")(function* ( + entrypoint: string | undefined, + ) { + if (!entrypoint) return undefined; + const resolved = yield* safeRealpath(entrypoint); + if (!resolved) return undefined; + const parent = yield* platform.dirname(resolved); + const parentName = yield* platform.basename(parent); + return parentName === "src" || parentName === "dist" + ? yield* platform.dirname(parent) + : undefined; + }); + + const fromModule = Effect.fn("PackageRootService.fromModule")(function* (metaUrl: string) { + let current = yield* platform.dirname(yield* platform.fileUrlToPath(metaUrl)); + for (let depth = 0; depth < 8; depth += 1) { + if (yield* platform.exists(yield* platform.join([current, "package.json"]))) return current; + const parent = yield* platform.dirname(current); + if (parent === current) return undefined; + current = parent; + } + return undefined; + }); + + const find = Effect.fn("PackageRootService.find")(function* (metaUrl: string) { + const environmentRoot = yield* platform.environmentRoot; + if (environmentRoot) return yield* platform.resolve(environmentRoot); + const argvRoot = yield* fromEntrypoint(yield* platform.argvEntrypoint); + if (argvRoot) return argvRoot; + const executable = yield* platform.execPath; + const execRoot = yield* fromEntrypoint(executable); + if (execRoot) return execRoot; + if (isBunBinary(metaUrl)) return yield* platform.dirname(executable); + const moduleRoot = yield* fromModule(metaUrl); + if (moduleRoot) return moduleRoot; + return yield* platform.join([ + yield* platform.dirname(yield* platform.fileUrlToPath(metaUrl)), + "..", + "..", + ]); + }); + + const packageFile = Effect.fn("PackageRootService.packageFile")(function* ( + metaUrl: string, + ...parts: string[] + ) { + return yield* platform.join([yield* find(metaUrl), ...parts]); + }); + + return { fromEntrypoint, fromModule, find, packageFile } as const; + }), +}) { + static readonly layer = Layer.effect(PackageRootService, PackageRootService.make); +} + +export const packageRootLayer = PackageRootService.layer.pipe( + Layer.provide(PackageRootPlatformService.layer), +); diff --git a/src/providers/paths/rig-paths.test.ts b/src/providers/paths/rig-paths.test.ts new file mode 100644 index 0000000..e9b68de --- /dev/null +++ b/src/providers/paths/rig-paths.test.ts @@ -0,0 +1,124 @@ +import { posix } from "node:path"; +import { Effect, Layer } from "effect"; +import { describe, expect, test, vi } from "vite-plus/test"; +import { + RigPathsConfigService, + rigPathsConfigLiveLayer, + RigPathsOptionsConfigService, + type RigPathsPlatform, + RigPathsPlatformService, + RigPathsService, +} from "./rig-paths"; + +describe("Rig paths services", () => { + test("builds every path from explicit configuration and dependencies", async () => { + const homedir = vi.fn<() => string>(() => "/fallback-home"); + const cwd = vi.fn<() => string>(() => "/working"); + const platform: RigPathsPlatform = { + homeDirectory: Effect.sync(homedir), + workingDirectory: Effect.sync(cwd), + dirname: (pathValue) => Effect.succeed(posix.dirname(pathValue)), + isAbsolute: (pathValue) => Effect.succeed(posix.isAbsolute(pathValue)), + join: (parts) => Effect.succeed(posix.join(...parts)), + resolve: (parts) => Effect.succeed(posix.resolve(...parts)), + }; + const optionsLayer = Layer.succeed(RigPathsOptionsConfigService, { + homeDir: "configured-home", + }); + const platformLayer = Layer.succeed(RigPathsPlatformService, platform); + const configLayer = RigPathsConfigService.layer.pipe( + Layer.provide(Layer.merge(optionsLayer, platformLayer)), + ); + const pathsLayer = RigPathsService.layer.pipe( + Layer.provide(Layer.merge(platformLayer, configLayer)), + ); + + const result = await Effect.gen(function* () { + const values = yield* RigPathsConfigService; + const paths = yield* RigPathsService; + return { + values, + tilde: yield* paths.expandTilde("~/tools"), + relative: yield* paths.resolve("relative/file.ts"), + absolute: yield* paths.resolve("/absolute/file.ts"), + worker: yield* paths.cronWorkerPath("daily"), + parent: yield* paths.parentDir("/working/file.ts"), + }; + }).pipe(Effect.provide(Layer.merge(configLayer, pathsLayer)), Effect.runPromise); + + expect(result.values.homeDir).toBe("/working/configured-home"); + expect(result.values.rigDir).toBe("/working/configured-home/rig"); + expect(result.values.legacyRigDir).toBe("/working/configured-home/.rig"); + expect(result.values.configPath).toBe("/working/configured-home/rig/rig.json"); + expect(result.values.runtimeDir).toBe("/working/configured-home/rig/runtime"); + expect(result.values.runtimeSdkPath).toBe("/working/configured-home/rig/runtime/sdk.ts"); + expect(result.values.runtimeTypesPath).toBe("/working/configured-home/rig/runtime/types.d.ts"); + expect(result.values.runtimeGlobalsPath).toBe( + "/working/configured-home/rig/runtime/globals.d.ts", + ); + expect(result.values.runtimeToolTsconfigPath).toBe( + "/working/configured-home/rig/runtime/tsconfig.tools.json", + ); + expect(result.values.cronDir).toBe("/working/configured-home/rig/cron"); + expect(result.values.logsDir).toBe("/working/configured-home/rig/.logs"); + expect(result.values.updateCheckCachePath).toBe( + "/working/configured-home/rig/update-check.json", + ); + expect(result.values.toolMetadataCachePath).toBe( + "/working/configured-home/rig/tool-metadata.json", + ); + expect(result.values.migrationPromptStatePath).toBe( + "/working/configured-home/rig/migration-prompts.json", + ); + expect(result.values.defaultBaseRegistryDir).toBe("~/rig/tools"); + expect(result.values.legacyDefaultBaseRegistryDir).toBe("~/.rig/tools"); + expect(result.tilde).toBe("/working/configured-home/tools"); + expect(result.relative).toBe("/working/relative/file.ts"); + expect(result.absolute).toBe("/absolute/file.ts"); + expect(result.worker).toBe("/working/configured-home/rig/cron/daily.ts"); + expect(result.parent).toBe("/working"); + expect(homedir).not.toHaveBeenCalled(); + expect(cwd).toHaveBeenCalled(); + }); + + test("uses the platform home directory when no override is configured", async () => { + const homedir = vi.fn<() => string>(() => "/fallback-home"); + const optionsLayer = Layer.succeed(RigPathsOptionsConfigService, { + homeDir: undefined, + }); + const platformLayer = Layer.succeed(RigPathsPlatformService, { + homeDirectory: Effect.sync(homedir), + workingDirectory: Effect.succeed("/working"), + dirname: (pathValue) => Effect.succeed(posix.dirname(pathValue)), + isAbsolute: (pathValue) => Effect.succeed(posix.isAbsolute(pathValue)), + join: (parts) => Effect.succeed(posix.join(...parts)), + resolve: (parts) => Effect.succeed(posix.resolve(...parts)), + }); + + const values = await Effect.service(RigPathsConfigService).pipe( + Effect.provide( + RigPathsConfigService.layer.pipe(Layer.provide(Layer.merge(optionsLayer, platformLayer))), + ), + Effect.runPromise, + ); + + expect(values.homeDir).toBe("/fallback-home"); + expect(values.rigDir).toBe("/fallback-home/rig"); + expect(homedir).toHaveBeenCalledOnce(); + }); + + test("uses RIG_HOME in the production options layer", async () => { + vi.stubEnv("RIG_HOME", "/environment-home"); + try { + const values = await Effect.service(RigPathsConfigService).pipe( + Effect.provide(rigPathsConfigLiveLayer), + Effect.runPromise, + ); + + expect(values.homeDir).toBe("/environment-home"); + expect(values.configPath).toBe("/environment-home/rig/rig.json"); + } finally { + vi.unstubAllEnvs(); + } + }); +}); diff --git a/src/providers/paths/rig-paths.ts b/src/providers/paths/rig-paths.ts new file mode 100644 index 0000000..1ac6abf --- /dev/null +++ b/src/providers/paths/rig-paths.ts @@ -0,0 +1,196 @@ +import { homedir } from "node:os"; +import { + dirname as pathDirname, + isAbsolute as pathIsAbsolute, + join as pathJoin, + resolve as pathResolve, +} from "node:path"; +import { Context, Effect, Layer } from "effect"; + +export type PathOptions = { homeDir?: string }; +export type RigPathsPlatform = Readonly<{ + readonly homeDirectory: Effect.Effect; + readonly workingDirectory: Effect.Effect; + readonly dirname: (pathValue: string) => Effect.Effect; + readonly isAbsolute: (pathValue: string) => Effect.Effect; + readonly join: (parts: readonly string[]) => Effect.Effect; + readonly resolve: (parts: readonly string[]) => Effect.Effect; +}>; + +export class RigPathsOptionsConfigService extends Context.Service< + RigPathsOptionsConfigService, + { readonly homeDir?: string } +>()("@rendotdev/rig/settings/RigPathsOptionsConfigService", { + make: Effect.gen(function* () { + const homeDir = process.env.RIG_HOME; + return { homeDir } as const; + }), +}) { + static readonly layer = Layer.effect( + RigPathsOptionsConfigService, + RigPathsOptionsConfigService.make, + ); +} + +export class RigPathsPlatformService extends Context.Service< + RigPathsPlatformService, + { + readonly homeDirectory: Effect.Effect; + readonly workingDirectory: Effect.Effect; + readonly dirname: (pathValue: string) => Effect.Effect; + readonly isAbsolute: (pathValue: string) => Effect.Effect; + readonly join: (parts: readonly string[]) => Effect.Effect; + readonly resolve: (parts: readonly string[]) => Effect.Effect; + } +>()("@rendotdev/rig/settings/RigPathsPlatformService", { + make: Effect.gen(function* () { + const homeDirectory = Effect.sync(homedir).pipe( + Effect.withSpan("RigPathsPlatformService.homeDirectory"), + ); + const workingDirectory = Effect.sync(() => process.cwd()).pipe( + Effect.withSpan("RigPathsPlatformService.workingDirectory"), + ); + const dirname = Effect.fn("RigPathsPlatformService.dirname")(function* (pathValue: string) { + return pathDirname(pathValue); + }); + const isAbsolute = Effect.fn("RigPathsPlatformService.isAbsolute")(function* ( + pathValue: string, + ) { + return pathIsAbsolute(pathValue); + }); + const join = Effect.fn("RigPathsPlatformService.join")(function* (parts: readonly string[]) { + return pathJoin(...parts); + }); + const resolve = Effect.fn("RigPathsPlatformService.resolve")(function* ( + parts: readonly string[], + ) { + return pathResolve(...parts); + }); + return { homeDirectory, workingDirectory, dirname, isAbsolute, join, resolve } as const; + }), +}) { + static readonly layer = Layer.effect(RigPathsPlatformService, RigPathsPlatformService.make); +} + +export class RigPathsConfigService extends Context.Service< + RigPathsConfigService, + { + readonly homeDir: string; + readonly rigDir: string; + readonly legacyRigDir: string; + readonly configPath: string; + readonly runtimeDir: string; + readonly runtimeSdkPath: string; + readonly runtimeTypesPath: string; + readonly runtimeGlobalsPath: string; + readonly runtimeToolTsconfigPath: string; + readonly cronDir: string; + readonly logsDir: string; + readonly updateCheckCachePath: string; + readonly toolMetadataCachePath: string; + readonly migrationPromptStatePath: string; + readonly defaultBaseRegistryDir: string; + readonly legacyDefaultBaseRegistryDir: string; + } +>()("@rendotdev/rig/settings/RigPathsConfigService", { + make: Effect.gen(function* () { + const options = yield* RigPathsOptionsConfigService; + const platform = yield* RigPathsPlatformService; + const homeDir = options.homeDir + ? yield* platform.resolve([yield* platform.workingDirectory, options.homeDir]) + : yield* platform.homeDirectory; + const rigDir = yield* platform.join([homeDir, "rig"]); + const legacyRigDir = yield* platform.join([homeDir, ".rig"]); + const runtimeDir = yield* platform.join([rigDir, "runtime"]); + const configPath = yield* platform.join([rigDir, "rig.json"]); + const runtimeSdkPath = yield* platform.join([runtimeDir, "sdk.ts"]); + const runtimeTypesPath = yield* platform.join([runtimeDir, "types.d.ts"]); + const runtimeGlobalsPath = yield* platform.join([runtimeDir, "globals.d.ts"]); + const runtimeToolTsconfigPath = yield* platform.join([runtimeDir, "tsconfig.tools.json"]); + const cronDir = yield* platform.join([rigDir, "cron"]); + const logsDir = yield* platform.join([rigDir, ".logs"]); + const updateCheckCachePath = yield* platform.join([rigDir, "update-check.json"]); + const toolMetadataCachePath = yield* platform.join([rigDir, "tool-metadata.json"]); + const migrationPromptStatePath = yield* platform.join([rigDir, "migration-prompts.json"]); + const defaultBaseRegistryDir = "~/rig/tools"; + const legacyDefaultBaseRegistryDir = "~/.rig/tools"; + return { + homeDir, + rigDir, + legacyRigDir, + configPath, + runtimeDir, + runtimeSdkPath, + runtimeTypesPath, + runtimeGlobalsPath, + runtimeToolTsconfigPath, + cronDir, + logsDir, + updateCheckCachePath, + toolMetadataCachePath, + migrationPromptStatePath, + defaultBaseRegistryDir, + legacyDefaultBaseRegistryDir, + } as const; + }), +}) { + static readonly layer = Layer.effect(RigPathsConfigService, RigPathsConfigService.make); +} + +export class RigPathsService extends Context.Service< + RigPathsService, + { + readonly expandTilde: (pathValue: string) => Effect.Effect; + readonly resolve: (pathValue: string) => Effect.Effect; + readonly cronWorkerPath: (name: string) => Effect.Effect; + readonly parentDir: (pathValue: string) => Effect.Effect; + } +>()("@rendotdev/rig/settings/RigPathsService", { + make: Effect.gen(function* () { + const values = yield* RigPathsConfigService; + const platform = yield* RigPathsPlatformService; + const expand = Effect.fn("RigPathsService.expand")(function* (pathValue: string) { + if (pathValue === "~") return values.homeDir; + return pathValue.startsWith("~/") + ? yield* platform.join([values.homeDir, pathValue.slice(2)]) + : pathValue; + }); + const expandTilde = Effect.fn("RigPathsService.expandTilde")(function* (pathValue: string) { + return yield* expand(pathValue); + }); + const resolve = Effect.fn("RigPathsService.resolve")(function* (pathValue: string) { + const expanded = yield* expand(pathValue); + return (yield* platform.isAbsolute(expanded)) + ? yield* platform.resolve([expanded]) + : yield* platform.resolve([yield* platform.workingDirectory, expanded]); + }); + const cronWorkerPath = Effect.fn("RigPathsService.cronWorkerPath")(function* (name: string) { + return yield* platform.join([values.cronDir, `${name}.ts`]); + }); + const parentDir = Effect.fn("RigPathsService.parentDir")(function* (pathValue: string) { + return yield* platform.dirname(pathValue); + }); + return { expandTilde, resolve, cronWorkerPath, parentDir } as const; + }), +}) { + static readonly layer = Layer.effect(RigPathsService, RigPathsService.make); +} + +const rigPathsLiveDependenciesLayer = Layer.merge( + RigPathsOptionsConfigService.layer, + RigPathsPlatformService.layer, +); +export const rigPathsConfigLayer: Layer.Layer< + RigPathsConfigService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = RigPathsConfigService.layer; +export const rigPathsConfigLiveLayer: Layer.Layer = rigPathsConfigLayer.pipe( + Layer.provide(rigPathsLiveDependenciesLayer), +); +const rigPathsServiceLayer = RigPathsService.layer.pipe(Layer.provide(rigPathsConfigLayer)); +export const rigPathsLayer: Layer.Layer< + RigPathsConfigService | RigPathsService, + never, + RigPathsOptionsConfigService | RigPathsPlatformService +> = Layer.merge(rigPathsConfigLayer, rigPathsServiceLayer); diff --git a/src/providers/process/managed-process.test.ts b/src/providers/process/managed-process.test.ts new file mode 100644 index 0000000..e8d37c5 --- /dev/null +++ b/src/providers/process/managed-process.test.ts @@ -0,0 +1,72 @@ +/* oxlint-disable vitest/no-standalone-expect -- @effect/vitest owns this Effect-aware test. */ +import type { ChildProcess } from "node:child_process"; +import { EventEmitter } from "node:events"; +import { it } from "@effect/vitest"; +import { Effect, Fiber, Layer } from "effect"; +import { expect, vi } from "vite-plus/test"; +import { + ManagedProcessConfigService, + ManagedProcessService, + managedProcessLayer, +} from "./managed-process"; + +it("resolves with the child exit code", () => + Effect.gen(function* () { + const child = Object.assign(new EventEmitter(), { + kill: vi.fn(() => true), + }) as unknown as ChildProcess; + const result = yield* ManagedProcessService.use((service) => + service.wait({ child, command: ["example"], timeoutMs: 1_000 }), + ).pipe( + Effect.tap(() => Effect.void), + Effect.provide(managedProcessLayer), + Effect.forkChild, + ); + yield* Effect.yieldNow; + child.emit("close", 7); + expect(yield* Fiber.join(result)).toEqual({ + exitCode: 7, + exitCodeKnown: true, + signal: null, + }); + })); + +it("terminates and escalates an interrupted child", () => + Effect.gen(function* () { + const child = new EventEmitter() as ChildProcess; + Object.defineProperty(child, "pid", { value: 42 }); + child.kill = vi.fn(() => true); + const layer = ManagedProcessService.layer.pipe( + Layer.provide( + Layer.succeed(ManagedProcessConfigService, { + processGroup: false, + terminationGraceMs: 5, + }), + ), + ); + const fiber = yield* ManagedProcessService.use((service) => + service.wait({ child, command: ["example"], timeoutMs: 1_000 }), + ).pipe(Effect.provide(layer), Effect.forkChild); + yield* Effect.yieldNow; + yield* Fiber.interrupt(fiber); + yield* Effect.sleep("20 millis"); + expect(child.kill).toHaveBeenNthCalledWith(1, "SIGTERM"); + expect(child.kill).toHaveBeenNthCalledWith(2, "SIGKILL"); + })); + +it("keeps a useful error message when a process times out", () => + Effect.gen(function* () { + const child = Object.assign(new EventEmitter(), { + kill: vi.fn(() => true), + }) as unknown as ChildProcess; + const fiber = yield* ManagedProcessService.use((service) => + service.wait({ child, command: ["example"], timeoutMs: 1 }), + ).pipe(Effect.provide(managedProcessLayer), Effect.forkChild); + yield* Effect.sleep("5 millis"); + child.emit("close", null, "SIGTERM"); + const error = yield* Fiber.join(fiber).pipe(Effect.flip); + + expect(error.reason).toBe("timeout"); + expect(error.cause).toBeInstanceOf(Error); + expect((error.cause as Error).message).toBe("Command timed out after 1ms."); + })); diff --git a/src/providers/process/managed-process.ts b/src/providers/process/managed-process.ts new file mode 100644 index 0000000..730d919 --- /dev/null +++ b/src/providers/process/managed-process.ts @@ -0,0 +1,139 @@ +import type { ChildProcess } from "node:child_process"; +import { Context, Effect, Layer, Schema } from "effect"; + +const TERMINATION_GRACE_MS = 100; + +export type ManagedProcessResult = Readonly<{ + exitCode: number; + exitCodeKnown: boolean; + signal: NodeJS.Signals | null; +}>; + +export class ManagedProcessError extends Schema.TaggedErrorClass()( + "ManagedProcessError", + { + command: Schema.Array(Schema.String), + reason: Schema.Literals(["spawn", "timeout"]), + cause: Schema.optional(Schema.Defect()), + }, +) {} + +export class ManagedProcessConfigService extends Context.Service< + ManagedProcessConfigService, + { + readonly processGroup: boolean; + readonly terminationGraceMs: number; + } +>()("@rendotdev/rig/runtime/ManagedProcessConfigService", { + make: Effect.succeed({ + processGroup: process.platform !== "win32", + terminationGraceMs: TERMINATION_GRACE_MS, + }), +}) { + static readonly layer = Layer.effect( + ManagedProcessConfigService, + ManagedProcessConfigService.make, + ); +} + +export class ManagedProcessService extends Context.Service< + ManagedProcessService, + { + readonly wait: (params: { + readonly child: ChildProcess; + readonly command: string[]; + readonly timeoutMs: number; + }) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/ManagedProcessService", { + make: Effect.gen(function* () { + const config = yield* ManagedProcessConfigService; + + const wait = Effect.fn("ManagedProcessService.wait")(function* (params: { + readonly child: ChildProcess; + readonly command: string[]; + readonly timeoutMs: number; + }) { + return yield* Effect.callback((resume) => { + let closed = false, + timedOut = false; + let spawnCause: unknown; + let timeout: ReturnType | undefined; + let escalation: ReturnType | undefined; + + const signal = (value: NodeJS.Signals) => { + try { + const canSignalProcessGroup = config.processGroup && params.child.pid; + if (canSignalProcessGroup) process.kill(-params.child.pid, value); + else params.child.kill(value); + } catch { + return; + } + }; + const clearTimers = () => { + if (timeout) clearTimeout(timeout); + if (escalation) clearTimeout(escalation); + }; + const terminate = (fromTimeout: boolean) => { + if (closed) return; + timedOut = fromTimeout; + signal("SIGTERM"); + escalation = setTimeout(() => signal("SIGKILL"), config.terminationGraceMs); + }; + const onError = (cause: unknown) => void (spawnCause = cause); + const onClose = (code: number | null, signalValue: NodeJS.Signals | null) => { + closed = true; + clearTimers(); + if (spawnCause) { + resume( + Effect.fail( + new ManagedProcessError({ + command: params.command, + reason: "spawn", + cause: spawnCause, + }), + ), + ); + } else if (timedOut) { + resume( + Effect.fail( + new ManagedProcessError({ + command: params.command, + reason: "timeout", + cause: new Error(`Command timed out after ${params.timeoutMs}ms.`), + }), + ), + ); + } else { + resume( + Effect.succeed({ + exitCode: code ?? 1, + exitCodeKnown: code !== null, + signal: signalValue, + }), + ); + } + }; + + params.child.once("error", onError); + params.child.once("close", onClose); + timeout = setTimeout(() => terminate(true), params.timeoutMs); + + return Effect.sync(() => { + params.child.removeListener("error", onError); + params.child.removeListener("close", onClose); + if (timeout) clearTimeout(timeout); + terminate(false); + }); + }); + }); + + return { wait } as const; + }), +}) { + static readonly layer = Layer.effect(ManagedProcessService, ManagedProcessService.make); +} + +export const managedProcessLayer = ManagedProcessService.layer.pipe( + Layer.provide(ManagedProcessConfigService.layer), +); diff --git a/src/providers/process/rig-shell-service.test.ts b/src/providers/process/rig-shell-service.test.ts new file mode 100644 index 0000000..91b58ea --- /dev/null +++ b/src/providers/process/rig-shell-service.test.ts @@ -0,0 +1,131 @@ +/* oxlint-disable vitest/no-standalone-expect -- @effect/vitest owns this Effect-aware test. */ +import type { ChildProcess } from "node:child_process"; +import { EventEmitter } from "node:events"; +import { existsSync } from "node:fs"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Readable } from "node:stream"; +import { it } from "@effect/vitest"; +import { Effect, Fiber, Layer } from "effect"; +import { expect, vi } from "vite-plus/test"; +import { ManagedProcessConfigService, ManagedProcessService } from "./managed-process"; +import { + RigShellConfigService, + RigShellPlatformService, + RigShellService, + ShellOutputService, + ShellProcessService, + ShellTemplateService, + rigShellLayer, +} from "./rig-shell-service"; + +const templateStrings = (value: string): TemplateStringsArray => + Object.assign([value], { raw: [value] }) as unknown as TemplateStringsArray; + +it.live("interrupts the complete child process group", () => + Effect.scoped( + Effect.gen(function* () { + const directory = yield* Effect.acquireRelease( + Effect.promise(() => mkdtemp(join(tmpdir(), "rig-shell-interrupt-"))), + (path) => Effect.promise(() => rm(path, { recursive: true, force: true })), + ); + const marker = join(directory, "should-not-exist"); + const fiber = yield* Effect.forkChild( + RigShellService.use((service) => + service.bash(`(sleep 0.2; touch ${marker}) & wait`, { timeoutMs: 5_000 }), + ).pipe(Effect.provide(rigShellLayer)), + ); + yield* Effect.sleep("50 millis"); + yield* Fiber.interrupt(fiber); + yield* Effect.sleep("300 millis"); + expect(existsSync(marker)).toBe(false); + }), + ), +); + +it.live("exposes every shell operation through the service layer", () => + Effect.gen(function* () { + const shell = yield* RigShellService; + const execResult = yield* shell.exec(["printf", "exec"]); + const bashResult = yield* shell.bash("printf bash"); + const templateResult = yield* shell.template(templateStrings("printf template"), []); + const jsonResult = yield* shell.json(["printf", '{"ready":true}']); + expect(execResult.stdout).toBe("exec"); + expect(bashResult.stdout).toBe("bash"); + expect(templateResult.stdout).toBe("template"); + expect(jsonResult).toEqual({ ready: true }); + }).pipe(Effect.provide(rigShellLayer)), +); + +it.live("retags template execution failures", () => + Effect.gen(function* () { + const processLayer = ShellProcessService.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(RigShellConfigService, { + defaults: { cwd: join(tmpdir(), "rig-missing-shell-directory") }, + }), + RigShellPlatformService.layer, + ManagedProcessService.layer.pipe(Layer.provide(ManagedProcessConfigService.layer)), + ShellOutputService.layer, + ), + ), + ); + const layer = RigShellService.layer.pipe( + Layer.provide(Layer.merge(ShellTemplateService.layer, processLayer)), + ); + const error = yield* RigShellService.use((service) => + service.template(templateStrings("printf unavailable"), []), + ).pipe(Effect.provide(layer), Effect.flip); + expect(error.operation).toBe("template"); + }), +); + +it("cancels a process when its effect is interrupted", () => + Effect.gen(function* () { + const stdout = new EventEmitter() as Readable; + const stderr = new EventEmitter() as Readable; + const child = Object.assign(new EventEmitter(), { + pid: 42, + stdout, + stderr, + kill: vi.fn(() => true), + }) as unknown as ChildProcess & { stdout: Readable; stderr: Readable }; + const platformLayer = Layer.succeed(RigShellPlatformService, { + environment: Effect.succeed({}), + currentDirectory: Effect.succeed("/tmp"), + detached: Effect.succeed(false), + spawn: Effect.fn("RigShellPlatformService.spawn")(function* () { + return child; + }), + }); + const managedLayer = ManagedProcessService.layer.pipe( + Layer.provide( + Layer.succeed(ManagedProcessConfigService, { + processGroup: false, + terminationGraceMs: 5, + }), + ), + ); + const processLayer = ShellProcessService.layer.pipe( + Layer.provide( + Layer.mergeAll( + RigShellConfigService.layer, + platformLayer, + managedLayer, + ShellOutputService.layer, + ), + ), + ); + const layer = RigShellService.layer.pipe( + Layer.provide(Layer.merge(ShellTemplateService.layer, processLayer)), + ); + const fiber = yield* RigShellService.use((service) => service.exec(["example"])).pipe( + Effect.provide(layer), + Effect.forkChild, + ); + yield* Effect.yieldNow; + yield* Fiber.interrupt(fiber); + expect(child.kill).toHaveBeenCalledWith("SIGTERM"); + })); diff --git a/src/providers/process/rig-shell-service.ts b/src/providers/process/rig-shell-service.ts new file mode 100644 index 0000000..e69bb75 --- /dev/null +++ b/src/providers/process/rig-shell-service.ts @@ -0,0 +1,341 @@ +import { spawn as nodeSpawn, type ChildProcess } from "node:child_process"; +import type { Readable } from "node:stream"; +import { Context, Effect, Layer, Schema } from "effect"; +import { ManagedProcessService, managedProcessLayer } from "./managed-process"; + +const DEFAULT_TIMEOUT_MS = 30_000; +const DEFAULT_MAX_OUTPUT_BYTES = 1_048_576; +const TRUNCATION_MARKER = "\n[rig: output truncated]"; + +export type ShellOptions = { + readonly cwd?: string; + readonly env?: Record; + readonly timeoutMs?: number; + readonly maxOutputBytes?: number; +}; + +type ShellResult = { + readonly command: string[]; + readonly stdout: string; + readonly stderr: string; + readonly exitCode: number; +}; + +export type RigShellChildProcess = ChildProcess & { + readonly stdout: Readable; + readonly stderr: Readable; +}; +export type RigShellProcessResult = ShellResult & { + readonly exitCodeKnown: boolean; + readonly signal?: NodeJS.Signals; +}; + +const RigShellOperation = Schema.Literals(["exec", "bash", "template", "json"]); +type RigShellOperation = typeof RigShellOperation.Type; + +export class RigShellError extends Schema.TaggedErrorClass()("RigShellError", { + operation: RigShellOperation, + command: Schema.Array(Schema.String), + cause: Schema.Defect(), +}) {} + +export class RigShellConfigService extends Context.Service< + RigShellConfigService, + { + readonly defaults: ShellOptions; + } +>()("@rendotdev/rig/runtime/RigShellConfigService", { + make: Effect.succeed({ defaults: {} }), +}) { + static readonly layer = Layer.effect(RigShellConfigService, RigShellConfigService.make); +} + +export class RigShellPlatformService extends Context.Service< + RigShellPlatformService, + { + readonly environment: Effect.Effect; + readonly currentDirectory: Effect.Effect; + readonly detached: Effect.Effect; + readonly spawn: (params: { + readonly command: string; + readonly arguments: string[]; + readonly cwd: string; + readonly environment: NodeJS.ProcessEnv; + readonly detached: boolean; + }) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/RigShellPlatformService", { + make: Effect.gen(function* () { + const environment = Effect.sync(() => process.env).pipe( + Effect.withSpan("RigShellPlatformService.environment"), + ); + const currentDirectory = Effect.sync(() => process.cwd()).pipe( + Effect.withSpan("RigShellPlatformService.currentDirectory"), + ); + const detached = Effect.sync(() => process.platform !== "win32").pipe( + Effect.withSpan("RigShellPlatformService.detached"), + ); + const spawn = Effect.fn("RigShellPlatformService.spawn")(function* (params: { + readonly command: string; + readonly arguments: string[]; + readonly cwd: string; + readonly environment: NodeJS.ProcessEnv; + readonly detached: boolean; + }) { + return yield* Effect.try(() => + nodeSpawn(params.command, params.arguments, { + cwd: params.cwd, + env: params.environment, + stdio: ["ignore", "pipe", "pipe"], + detached: params.detached, + }), + ); + }); + return { environment, currentDirectory, detached, spawn } as const; + }), +}) { + static readonly layer = Layer.effect(RigShellPlatformService, RigShellPlatformService.make); +} + +export class ShellTemplateService extends Context.Service< + ShellTemplateService, + { + readonly render: (strings: TemplateStringsArray, values: unknown[]) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/ShellTemplateService", { + make: Effect.gen(function* () { + const quote = (value: string): string => { + if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(value)) return value; + return `'${value.replaceAll("'", "'\\''")}'`; + }; + const renderValue = (value: unknown): string => + Array.isArray(value) + ? value.map((item) => quote(String(item))).join(" ") + : quote(String(value)); + const render = Effect.fn("ShellTemplateService.render")(function* ( + strings: TemplateStringsArray, + values: unknown[], + ) { + return strings.reduce((command, part, index) => { + const value = index < values.length ? renderValue(values[index]) : ""; + return `${command}${part}${value}`; + }, ""); + }); + return { render } as const; + }), +}) { + static readonly layer = Layer.effect(ShellTemplateService, ShellTemplateService.make); +} + +export class ShellOutputService extends Context.Service< + ShellOutputService, + { + readonly observe: ( + stream: Readable, + maxBytes: number, + ) => Effect.Effect<{ readonly complete: Effect.Effect }>; + } +>()("@rendotdev/rig/runtime/ShellOutputService", { + make: Effect.gen(function* () { + const decode = (buffer: Buffer): string => { + const decoder = new TextDecoder("utf-8", { fatal: true }); + for (let removed = 0; removed <= Math.min(3, buffer.byteLength); removed += 1) { + try { + return decoder.decode(buffer.subarray(0, buffer.byteLength - removed)); + } catch { + continue; + } + } + return new TextDecoder().decode(buffer); + }; + const observe = Effect.fn("ShellOutputService.observe")(function* ( + stream: Readable, + maxBytes: number, + ) { + const chunks: Buffer[] = []; + let capturedBytes = 0; + let truncated = false; + const capture = (buffer: Buffer) => { + const remaining = Math.max(0, maxBytes - capturedBytes); + if (remaining > 0) { + const captured = buffer.subarray(0, remaining); + chunks.push(captured); + capturedBytes += captured.byteLength; + } + if (buffer.byteLength > remaining) truncated = true; + }; + yield* Effect.sync(() => stream.on("data", capture)); + const complete = Effect.sync(() => { + const content = decode(Buffer.concat(chunks, capturedBytes)); + return truncated ? `${content}${TRUNCATION_MARKER}` : content; + }).pipe(Effect.withSpan("ShellOutputService.complete")); + return { complete } as const; + }); + return { observe } as const; + }), +}) { + static readonly layer = Layer.effect(ShellOutputService, ShellOutputService.make); +} + +export class ShellProcessService extends Context.Service< + ShellProcessService, + { + readonly run: (params: { + readonly args: string[]; + readonly options?: ShellOptions; + readonly reportedCommand?: string[]; + }) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/ShellProcessService", { + make: Effect.gen(function* () { + const config = yield* RigShellConfigService; + const platform = yield* RigShellPlatformService; + const managedProcess = yield* ManagedProcessService; + const output = yield* ShellOutputService; + const run = Effect.fn("ShellProcessService.run")(function* (params: { + readonly args: string[]; + readonly options?: ShellOptions; + readonly reportedCommand?: string[]; + }) { + const options = params.options ?? {}; + const reportedCommand = params.reportedCommand ?? params.args; + const timeoutMs = options.timeoutMs ?? config.defaults.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const maxBytes = + options.maxOutputBytes ?? config.defaults.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; + const child = yield* platform + .spawn({ + command: params.args[0]!, + arguments: params.args.slice(1), + cwd: options.cwd ?? config.defaults.cwd ?? (yield* platform.currentDirectory), + environment: { + ...(yield* platform.environment), + ...config.defaults.env, + ...options.env, + }, + detached: yield* platform.detached, + }) + .pipe( + Effect.mapError( + (cause) => new RigShellError({ operation: "exec", command: reportedCommand, cause }), + ), + ); + const stdout = yield* output.observe(child.stdout, maxBytes); + const stderr = yield* output.observe(child.stderr, maxBytes); + const processResult = yield* managedProcess + .wait({ child, command: reportedCommand, timeoutMs }) + .pipe( + Effect.mapError( + (cause) => new RigShellError({ operation: "exec", command: reportedCommand, cause }), + ), + ); + return { + command: reportedCommand, + stdout: yield* stdout.complete, + stderr: yield* stderr.complete, + exitCode: processResult.exitCode, + exitCodeKnown: processResult.exitCodeKnown, + ...(processResult.signal ? { signal: processResult.signal } : {}), + }; + }); + return { run } as const; + }), +}) { + static readonly layer = Layer.effect(ShellProcessService, ShellProcessService.make); +} + +export class RigShellService extends Context.Service< + RigShellService, + { + readonly exec: ( + args: string[], + options?: ShellOptions, + ) => Effect.Effect; + readonly bash: ( + command: string, + options?: ShellOptions, + ) => Effect.Effect; + readonly template: ( + strings: TemplateStringsArray, + values: unknown[], + ) => Effect.Effect; + readonly json: ( + args: string[], + options?: ShellOptions, + ) => Effect.Effect; + } +>()("@rendotdev/rig/runtime/RigShellService", { + make: Effect.gen(function* () { + const processService = yield* ShellProcessService; + const templateService = yield* ShellTemplateService; + const validate = (args: string[]): RigShellError | undefined => + !Array.isArray(args) || + args.length === 0 || + args.some((argument) => typeof argument !== "string" || argument.length === 0) + ? new RigShellError({ + operation: "exec", + command: args, + cause: "shell.exec expects a non-empty array of command arguments.", + }) + : undefined; + const exec = Effect.fn("RigShellService.exec")(function* ( + args: string[], + options?: ShellOptions, + ) { + const error = validate(args); + if (error) return yield* error; + return yield* processService.run({ args, options }); + }); + const bash = Effect.fn("RigShellService.bash")(function* ( + command: string, + options?: ShellOptions, + ) { + return yield* processService.run({ + args: ["bash", "-lc", command], + options, + reportedCommand: [command], + }); + }); + const template = Effect.fn("RigShellService.template")(function* ( + strings: TemplateStringsArray, + values: unknown[], + ) { + const command = yield* templateService.render(strings, values); + return yield* bash(command).pipe( + Effect.mapError( + (error) => new RigShellError({ operation: "template", command: [command], cause: error }), + ), + ); + }); + const json = Effect.fn("RigShellService.json")(function* ( + args: string[], + options?: ShellOptions, + ) { + const result = yield* exec(args, options); + if (result.exitCode !== 0) { + return yield* new RigShellError({ operation: "json", command: args, cause: result }); + } + return yield* Effect.try({ + try: () => JSON.parse(result.stdout) as unknown, + catch: (cause) => new RigShellError({ operation: "json", command: args, cause }), + }); + }); + return { exec, bash, template, json } as const; + }), +}) { + static readonly layer = Layer.effect(RigShellService, RigShellService.make); +} + +const shellProcessLayer = ShellProcessService.layer.pipe( + Layer.provide( + Layer.mergeAll( + RigShellConfigService.layer, + RigShellPlatformService.layer, + managedProcessLayer, + ShellOutputService.layer, + ), + ), +); + +export const rigShellLayer = RigShellService.layer.pipe( + Layer.provide(Layer.merge(ShellTemplateService.layer, shellProcessLayer)), +); diff --git a/src/registry/application/discover-tools.ts b/src/registry/application/discover-tools.ts deleted file mode 100644 index c35f37c..0000000 --- a/src/registry/application/discover-tools.ts +++ /dev/null @@ -1,286 +0,0 @@ -import { existsSync, lstatSync, statSync } from "node:fs"; -import { readdir } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import { defineService } from "../../define"; -import { RigConfigStoreClass, type ConfigOptions, type RegistryEntry } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import { RigErrorClass } from "../../errors/RigError"; - -export type RegistryKind = "base" | "custom"; - -export const RigToolEntryFiles = ["index.rig.ts", "index.rig.tsx"] as const; - -export type ToolDiscoveryOptions = { - visibleFromPath?: string; -}; - -export type DiscoveredTool = { - name: string; - registryKind: RegistryKind; - registryPath: string; - toolDir: string; - toolPath: string; -}; - -type ToolDiscoveryConfigStore = Pick; -type ToolDiscoveryPaths = Pick; - -type ToolDiscoveryServiceDeps = { - createConfigStore: (options: ConfigOptions) => ToolDiscoveryConfigStore; - createPaths: (options: ConfigOptions) => ToolDiscoveryPaths; - exists: typeof existsSync; - lstat: typeof lstatSync; - stat: typeof statSync; - readdir: typeof readdir; - dirname: typeof dirname; - join: typeof join; -}; - -function visibleRegistryEntries(params: { - entries: RegistryEntry[]; - visibleFromPath?: string; -}): RegistryEntry[] { - if (!params.visibleFromPath) return params.entries; - return params.entries; -} - -const ToolDiscoveryServiceProductionDeps: ToolDiscoveryServiceDeps = { - createConfigStore(options) { - return new RigConfigStoreClass(options); - }, - createPaths(options) { - return new RigPathsClass(options); - }, - exists: existsSync, - lstat: lstatSync, - stat: statSync, - readdir, - dirname, - join, -}; - -export class ToolDiscoveryService extends defineService({ - params: {} as ConfigOptions, - deps: ToolDiscoveryServiceProductionDeps, -}) { - private readonly configStore = this.deps.createConfigStore(this.params); - private readonly paths = this.deps.createPaths(this.params); - - /* v8 ignore start */ - private visibilityStartDirectory(params: { pathValue: string }): string { - const absolute = this.paths.resolve(params.pathValue); - try { - const pathStat = this.deps.stat(absolute); - if (pathStat.isDirectory()) return absolute; - } catch { - // Missing instruction files still scope to their parent directory. - } - return this.deps.dirname(absolute); - } - - public projectRootFor(params: { pathValue: string }): string { - let current = this.visibilityStartDirectory(params); - let packageRoot: string | undefined; - while (true) { - if (this.deps.exists(this.deps.join(current, ".git"))) return current; - if (!packageRoot && this.deps.exists(this.deps.join(current, "package.json"))) { - packageRoot = current; - } - const parent = this.deps.dirname(current); - if (parent === current) return packageRoot ?? this.visibilityStartDirectory(params); - current = parent; - } - } - /* v8 ignore stop */ - - private rejectLegacyEntry(params: { toolName: string; toolDir: string }): void { - const legacyPath = this.deps.join(params.toolDir, "tool.ts"); - if (!this.deps.exists(legacyPath)) return; - throw new RigErrorClass( - "TOOL_INVALID", - `Tool ${params.toolName} must use index.rig.ts or index.rig.tsx.`, - { toolDir: params.toolDir, found: legacyPath, expected: RigToolEntryFiles }, - ); - } - - private discoverNamedTool(params: { - entry: RegistryEntry; - name: string; - }): DiscoveredTool | undefined { - const toolDir = this.deps.join(params.entry.path, params.name); - try { - if (!this.deps.lstat(toolDir).isDirectory()) return undefined; - } catch { - return undefined; - } - const toolPaths = RigToolEntryFiles.map((file) => this.deps.join(toolDir, file)).filter( - (path) => this.deps.exists(path), - ); - if (toolPaths.length > 1) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${params.name} has multiple Rig entry files.`, { - toolDir, - found: toolPaths, - expected: RigToolEntryFiles, - }); - } - if (toolPaths.length === 0) { - this.rejectLegacyEntry({ toolName: params.name, toolDir }); - return undefined; - } - return { - name: params.name, - registryKind: params.entry.kind, - registryPath: params.entry.path, - toolDir, - toolPath: toolPaths[0]!, - }; - } - - public async discoverRegistry(params: { entry: RegistryEntry }): Promise { - if (!this.deps.exists(params.entry.path)) return []; - const children = await this.deps.readdir(params.entry.path, { withFileTypes: true }); - return children.flatMap((child) => { - if (!child.isDirectory()) return []; - const toolDir = this.deps.join(params.entry.path, child.name); - const toolPaths = RigToolEntryFiles.map((file) => this.deps.join(toolDir, file)).filter( - (path) => this.deps.exists(path), - ); - if (toolPaths.length > 1) { - throw new RigErrorClass( - "TOOL_INVALID", - `Tool ${child.name} has multiple Rig entry files.`, - { toolDir, found: toolPaths, expected: RigToolEntryFiles }, - ); - } - if (toolPaths.length === 0) { - this.rejectLegacyEntry({ toolName: child.name, toolDir }); - return []; - } - return [ - { - name: child.name, - registryKind: params.entry.kind, - registryPath: params.entry.path, - toolDir, - toolPath: toolPaths[0]!, - }, - ]; - }); - } - - public async discover(params: ToolDiscoveryOptions = {}): Promise { - const rigConfig = await this.configStore.ensure(); - const entries = visibleRegistryEntries({ - entries: this.configStore.registryEntries(rigConfig), - visibleFromPath: params.visibleFromPath, - }); - const discoveredByRegistry = await Promise.all( - entries.map((entry) => this.discoverRegistry({ entry })), - ); - const tools = new Map(); - for (const tool of discoveredByRegistry.flat()) { - const existing = tools.get(tool.name); - if (existing) { - throw new RigErrorClass("DUPLICATE_TOOL", `Duplicate tool name: ${tool.name}`, { - name: tool.name, - paths: [existing.toolPath, tool.toolPath], - }); - } - tools.set(tool.name, tool); - } - return [...tools.values()].toSorted((left, right) => left.name.localeCompare(right.name)); - } - - public async find(params: { name: string }): Promise { - if (!/^[A-Za-z0-9_-]+$/.test(params.name)) { - throw new RigErrorClass("TOOL_NOT_FOUND", `Tool not found: ${params.name}`, { - name: params.name, - }); - } - const rigConfig = await this.configStore.ensure(); - const discovered = this.configStore - .registryEntries(rigConfig) - .map((entry) => this.discoverNamedTool({ entry, name: params.name })) - .filter((tool): tool is DiscoveredTool => tool !== undefined); - if (discovered.length > 1) { - throw new RigErrorClass("DUPLICATE_TOOL", `Duplicate tool name: ${params.name}`, { - name: params.name, - paths: discovered.map((tool) => tool.toolPath), - }); - } - const tool = discovered[0]; - if (!tool) { - throw new RigErrorClass("TOOL_NOT_FOUND", `Tool not found: ${params.name}`, { - name: params.name, - }); - } - return tool; - } -} - -export type ToolDiscoveryServiceClass = { - discover(options?: ToolDiscoveryOptions): Promise; - find(name: string): Promise; - projectRootFor(pathValue: string): string; -}; - -type ToolDiscoveryServiceConstructor = { - new (options?: ConfigOptions): ToolDiscoveryServiceClass; - readonly prototype: ToolDiscoveryServiceClass; -}; - -type ToolDiscoveryServiceAdapter = ToolDiscoveryServiceClass & { - readonly resource: ToolDiscoveryService; - discoverRegistry(entry: RegistryEntry): Promise; -}; - -const ToolDiscoveryServiceClassAdapter = function constructToolDiscoveryService( - this: ToolDiscoveryServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolDiscoveryService({ - params: options, - deps: ToolDiscoveryServiceProductionDeps, - }), - }); -}; -Object.defineProperty(ToolDiscoveryServiceClassAdapter, "name", { - value: "ToolDiscoveryServiceClass", -}); -Object.defineProperties(ToolDiscoveryServiceClassAdapter.prototype, { - discover: { - configurable: true, - value: function discover( - this: ToolDiscoveryServiceAdapter, - options: ToolDiscoveryOptions = {}, - ) { - return this.resource.discover(options); - }, - writable: true, - }, - find: { - configurable: true, - value: function find(this: ToolDiscoveryServiceAdapter, name: string) { - return this.resource.find({ name }); - }, - writable: true, - }, - projectRootFor: { - configurable: true, - value: function projectRootFor(this: ToolDiscoveryServiceAdapter, pathValue: string) { - return this.resource.projectRootFor({ pathValue }); - }, - writable: true, - }, - discoverRegistry: { - configurable: true, - value: function discoverRegistry(this: ToolDiscoveryServiceAdapter, entry: RegistryEntry) { - return this.resource.discoverRegistry({ entry }); - }, - writable: true, - }, -}); - -export const ToolDiscoveryServiceClass = - ToolDiscoveryServiceClassAdapter as unknown as ToolDiscoveryServiceConstructor; diff --git a/src/registry/application/registry-config.ts b/src/registry/application/registry-config.ts deleted file mode 100644 index 914168a..0000000 --- a/src/registry/application/registry-config.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { mkdir } from "node:fs/promises"; -import { defineService } from "../../define"; -import { RigConfigStoreClass, type ConfigOptions } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import { RigErrorClass } from "../../errors/RigError"; - -type RegistryConfigStore = Pick; -type RegistryPaths = Pick; - -type RegistryConfigServiceDeps = { - createConfigStore: (options: ConfigOptions) => RegistryConfigStore; - createPaths: (options: ConfigOptions) => RegistryPaths; - mkdir: typeof mkdir; -}; - -const RegistryConfigServiceProductionDeps: RegistryConfigServiceDeps = { - createConfigStore(options) { - return new RigConfigStoreClass(options); - }, - createPaths(options) { - return new RigPathsClass(options); - }, - mkdir, -}; - -export class RegistryConfigService extends defineService({ - params: {} as ConfigOptions, - deps: RegistryConfigServiceProductionDeps, -}) { - private readonly configStore = this.deps.createConfigStore(this.params); - private readonly paths = this.deps.createPaths(this.params); - - public async list(_params: {}) { - const rigConfig = await this.configStore.ensure(); - return { - baseRegistryDir: this.paths.resolve(rigConfig.baseRegistryDir), - customRegistries: rigConfig.customRegistries.map((pathValue) => - this.paths.resolve(pathValue), - ), - registries: this.configStore.registryEntries(rigConfig), - }; - } - - public async add(params: { pathValue: string }) { - await this.configStore.ensure(); - const target = this.paths.resolve(params.pathValue); - await this.configStore.update(async (rigConfig) => { - const existing = rigConfig.customRegistries.map((entry) => this.paths.resolve(entry)); - const base = this.paths.resolve(rigConfig.baseRegistryDir); - - if (target === base) { - throw new RigErrorClass("CONFIG_INVALID", "The base registry is already configured.", { - path: target, - }); - } - - if (existing.includes(target)) return rigConfig; - await this.deps.mkdir(target, { recursive: true }); - return { - ...rigConfig, - customRegistries: [...rigConfig.customRegistries, params.pathValue], - }; - }); - - return await this.list({}); - } - - public async remove(params: { pathValue: string }) { - await this.configStore.ensure(); - const target = this.paths.resolve(params.pathValue); - await this.configStore.update((rigConfig) => { - const next = rigConfig.customRegistries.filter( - (entry) => this.paths.resolve(entry) !== target, - ); - - if (next.length === rigConfig.customRegistries.length) { - throw new RigErrorClass( - "CONFIG_INVALID", - `Registry is not configured: ${params.pathValue}`, - { path: target }, - ); - } - - return { ...rigConfig, customRegistries: next }; - }); - return await this.list({}); - } -} - -export type RegistryConfigServiceClass = { - list(): ReturnType; - add(pathValue: string): ReturnType; - remove(pathValue: string): ReturnType; -}; - -type RegistryConfigServiceConstructor = { - new (options?: ConfigOptions): RegistryConfigServiceClass; - readonly prototype: RegistryConfigServiceClass; -}; - -type RegistryConfigServiceAdapter = RegistryConfigServiceClass & { - readonly resource: RegistryConfigService; -}; - -const RegistryConfigServiceClassAdapter = function constructRegistryConfigService( - this: RegistryConfigServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new RegistryConfigService({ - params: options, - deps: RegistryConfigServiceProductionDeps, - }), - }); -}; -Object.defineProperty(RegistryConfigServiceClassAdapter, "name", { - value: "RegistryConfigServiceClass", -}); -Object.defineProperties(RegistryConfigServiceClassAdapter.prototype, { - list: { - configurable: true, - value: function list(this: RegistryConfigServiceAdapter) { - return this.resource.list({}); - }, - writable: true, - }, - add: { - configurable: true, - value: function add(this: RegistryConfigServiceAdapter, pathValue: string) { - return this.resource.add({ pathValue }); - }, - writable: true, - }, - remove: { - configurable: true, - value: function remove(this: RegistryConfigServiceAdapter, pathValue: string) { - return this.resource.remove({ pathValue }); - }, - writable: true, - }, -}); - -export const RegistryConfigServiceClass = - RegistryConfigServiceClassAdapter as unknown as RegistryConfigServiceConstructor; diff --git a/src/registry/discover.ts b/src/registry/discover.ts deleted file mode 100644 index d4d99da..0000000 --- a/src/registry/discover.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./application/discover-tools"; diff --git a/src/registry/registry.ts b/src/registry/registry.ts deleted file mode 100644 index f085a0f..0000000 --- a/src/registry/registry.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./application/registry-config"; diff --git a/src/runtime/envelope.ts b/src/runtime/envelope.ts deleted file mode 100644 index c448363..0000000 --- a/src/runtime/envelope.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./output/envelope"; diff --git a/src/runtime/logger.test.ts b/src/runtime/logger.test.ts deleted file mode 100644 index 21a935d..0000000 --- a/src/runtime/logger.test.ts +++ /dev/null @@ -1,302 +0,0 @@ -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { existsSync } from "node:fs"; -import { spawn } from "node:child_process"; -import { mkdir, mkdtemp, readdir, readFile, rm, utimes, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { LogRotationLockClass, type LogRotationLockDeps, RigLoggerFactoryClass } from "./logger"; - -class LoggerWorkspaceStore { - private readonly homes: string[] = []; - - async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-logger-")); - this.homes.push(home); - return home; - } - - async cleanup(): Promise { - await Promise.all( - this.homes.splice(0).map((home) => rm(home, { recursive: true, force: true })), - ); - } -} - -class LogRotationLockDepsFakeClass implements LogRotationLockDeps { - readonly madeDirectories: string[] = []; - readonly removedPaths: string[] = []; - readonly renamedPaths: Array<[string, string]> = []; - readonly waits: number[] = []; - makeDirectoryErrors: unknown[] = []; - readStatusErrors: unknown[] = []; - nowValues: number[] = [0]; - mtimeMs = 0; - - now(): number { - return this.nowValues.length > 1 ? (this.nowValues.shift() ?? 0) : (this.nowValues[0] ?? 0); - } - - wait(_buffer: Int32Array, _index: number, _value: number, timeout: number): string { - this.waits.push(timeout); - return "ok"; - } - - makeDirectory(path: string): void { - this.madeDirectories.push(path); - const error = this.makeDirectoryErrors.shift(); - if (error !== undefined) throw error; - } - - readStatus(_path: string): { mtimeMs: number } { - const error = this.readStatusErrors.shift(); - if (error !== undefined) throw error; - return { mtimeMs: this.mtimeMs }; - } - - rename(from: string, to: string): void { - this.renamedPaths.push([from, to]); - } - - remove(path: string): void { - this.removedPaths.push(path); - } -} - -class ChildProcessExitReaderClass { - async read(child: ReturnType): Promise { - return await new Promise((resolve, reject) => { - child.once("error", reject); - child.once("close", resolve); - }); - } -} - -const workspaces = new LoggerWorkspaceStore(); -const fixedLogNow = () => Date.parse("2026-07-01T00:00:00.000Z"); - -afterEach(async () => { - await workspaces.cleanup(); -}); - -describe("rig logger", () => { - test("bounds rotation-lock waiting and surfaces unexpected acquisition failures", () => { - const timeoutDeps = new LogRotationLockDepsFakeClass(); - timeoutDeps.makeDirectoryErrors = [{ code: "EEXIST" }, { code: "EEXIST" }]; - timeoutDeps.nowValues = [0, 0, 20]; - const timeoutLock = new LogRotationLockClass( - { logDir: "/logs", waitTimeoutMs: 20, staleAfterMs: 30_000 }, - timeoutDeps, - ); - expect(timeoutLock.run(() => undefined)).toBe(false); - expect(timeoutDeps.waits).toEqual([]); - - for (const error of ["permission denied", { code: 13 }]) { - const failureDeps = new LogRotationLockDepsFakeClass(); - failureDeps.makeDirectoryErrors = [error]; - const failureLock = new LogRotationLockClass({ logDir: "/logs" }, failureDeps); - let thrown: unknown; - try { - failureLock.run(() => undefined); - } catch (failure) { - thrown = failure; - } - expect(thrown).toBe(error); - } - }); - - test("recovers stale locks and tolerates competing recovery races", () => { - const staleDeps = new LogRotationLockDepsFakeClass(); - staleDeps.makeDirectoryErrors = [{ code: "EEXIST" }]; - staleDeps.nowValues = [40_000]; - const staleLock = new LogRotationLockClass({ logDir: "/logs" }, staleDeps); - expect(staleLock.run(() => undefined)).toBe(true); - expect(staleDeps.renamedPaths).toHaveLength(1); - expect(staleDeps.removedPaths).toHaveLength(2); - - for (const code of ["ENOENT", "EEXIST"]) { - const raceDeps = new LogRotationLockDepsFakeClass(); - raceDeps.makeDirectoryErrors = [{ code: "EEXIST" }]; - raceDeps.readStatusErrors = [{ code }]; - const raceLock = new LogRotationLockClass({ logDir: "/logs" }, raceDeps); - expect(raceLock.run(() => undefined)).toBe(true); - expect(raceDeps.waits).toEqual([10]); - } - - const failureDeps = new LogRotationLockDepsFakeClass(); - failureDeps.makeDirectoryErrors = [{ code: "EEXIST" }]; - failureDeps.readStatusErrors = [new Error("status failed")]; - const failureLock = new LogRotationLockClass({ logDir: "/logs" }, failureDeps); - expect(() => failureLock.run(() => undefined)).toThrow("status failed"); - }); - - test("writes prefixed pino logs, rolls files, and removes expired files", async () => { - const home = await workspaces.create(); - const logDir = join(home, "rig", ".logs"); - await mkdir(logDir, { recursive: true }); - await writeFile(join(logDir, "rig-old.log"), "old\n", "utf8"); - await utimes( - join(logDir, "rig-old.log"), - new Date("2026-06-01T00:00:00.000Z"), - new Date("2026-06-01T00:00:00.000Z"), - ); - - const factory = new RigLoggerFactoryClass({ - homeDir: home, - level: "trace", - maxFileSizeBytes: 180, - retentionDays: 7, - now: fixedLogNow, - }); - factory.app().debug("Debug app line."); - factory.app("test").info({ ok: true }, "App log line."); - factory.tool("sample", "echo").info({ key: "value" }, "Tool log line."); - factory.tool("sample", "echo").warn("Another tool log line that rolls the active log."); - factory.tool("sample", "echo").error(new Error("boom"), "Tool error line."); - - new RigLoggerFactoryClass({ - homeDir: home, - level: "info", - maxFileSizeBytes: 180, - retentionDays: 7, - now: fixedLogNow, - }) - .app("second") - .info("Second factory line."); - await new Promise((resolve) => queueMicrotask(resolve)); - - const entries = (await readdir(logDir)).toSorted(); - expect(entries).not.toContain("rig-old.log"); - expect(entries).toContain("rig.log"); - expect(entries.some((entry) => entry.startsWith("rig-2026-07-01T00-00-00-000Z"))).toBe(true); - expect(entries.some((entry) => entry.includes("-1.log"))).toBe(true); - - const output = ( - await Promise.all(entries.map((entry) => readFile(join(logDir, entry), "utf8"))) - ).join("\n"); - expect(output).toContain('"prefix":"rig:test"'); - expect(output).toContain('"prefix":"tool:sample.echo"'); - expect(output).toContain("Tool log line."); - expect(output).toContain('"type":"Error"'); - }); - - test("supports disabled logging and environment configuration", async () => { - const disabledHome = await workspaces.create(); - new RigLoggerFactoryClass({ - homeDir: disabledHome, - env: { RIG_LOG: "0" } as NodeJS.ProcessEnv, - }) - .app("disabled") - .info("This should not be written."); - expect(existsSync(join(disabledHome, "rig", ".logs"))).toBe(false); - - const envHome = await workspaces.create(); - const logDir = join(envHome, "custom-logs"); - new RigLoggerFactoryClass({ - homeDir: envHome, - env: { - RIG_LOG_DIR: logDir, - RIG_LOG_LEVEL: "debug", - RIG_LOG_MAX_BYTES: "1000", - RIG_LOG_RETENTION_DAYS: "7", - } as NodeJS.ProcessEnv, - }) - .app("env") - .debug("Environment configured debug line."); - expect(await readFile(join(logDir, "rig.log"), "utf8")).toContain( - "Environment configured debug line.", - ); - - const fallbackHome = await workspaces.create(); - const fallbackLogger = new RigLoggerFactoryClass({ - homeDir: fallbackHome, - env: { - RIG_LOG_MAX_BYTES: "not-a-number", - RIG_LOG_RETENTION_DAYS: "not-a-number", - } as NodeJS.ProcessEnv, - }).app("fallback"); - fallbackLogger.info("Fallback configured line."); - fallbackLogger.info("Second buffered line."); - fallbackLogger.info("Third buffered line."); - await new Promise((resolve) => queueMicrotask(resolve)); - expect(await readFile(join(fallbackHome, "rig", ".logs", "rig.log"), "utf8")).toContain( - "Fallback configured line.", - ); - }); - - test("keeps the active log during retention and recovers an abandoned rotation lock", async () => { - const home = await workspaces.create(); - const logDir = join(home, "logs"); - const lockPath = join(logDir, ".rig.log.rotation.lock"); - await mkdir(lockPath, { recursive: true }); - await writeFile(join(logDir, "rig.log"), "active\n", "utf8"); - const old = new Date(Date.now() - 60_000); - await utimes(lockPath, old, old); - await utimes(join(logDir, "rig.log"), old, old); - - new RigLoggerFactoryClass({ logDir }).app("recovery").info("Recovered logging."); - - expect(existsSync(lockPath)).toBe(false); - expect(await readFile(join(logDir, "rig.log"), "utf8")).toContain("active\n"); - expect(await readFile(join(logDir, "rig.log"), "utf8")).toContain("Recovered logging."); - }); - - test("preserves every record while separate processes rotate the same log", async () => { - const home = await workspaces.create(); - const logDir = join(home, "logs"); - const loggerUrl = new URL("./logger.ts", import.meta.url).href; - const children = Array.from({ length: 4 }, (_, processIndex) => { - const source = ` - import { RigLoggerFactoryClass } from ${JSON.stringify(loggerUrl)}; - const logger = new RigLoggerFactoryClass({ - logDir: ${JSON.stringify(logDir)}, - maxFileSizeBytes: 500, - retentionDays: 7, - }).app("child"); - for (let line = 0; line < 20; line++) logger.info("record-${processIndex}-" + line); - `; - return spawn("bun", ["-e", source], { stdio: "pipe" }); - }); - - const exits = await Promise.all( - children.map( - (child) => - new Promise((resolve, reject) => { - child.once("error", reject); - child.once("close", resolve); - }), - ), - ); - expect(exits).toEqual([0, 0, 0, 0]); - - const entries = (await readdir(logDir)).filter((entry) => entry.endsWith(".log")); - const output = ( - await Promise.all(entries.map((entry) => readFile(join(logDir, entry), "utf8"))) - ).join("\n"); - const records = new Set(output.match(/record-\d+-\d+/g)); - expect(records.size).toBe(80); - expect(existsSync(join(logDir, ".rig.log.rotation.lock"))).toBe(false); - }); - - test("rechecks active size after a competing process rotates while waiting", async () => { - const home = await workspaces.create(); - const logDir = join(home, "logs"); - const lockPath = join(logDir, ".rig.log.rotation.lock"); - const activePath = join(logDir, "rig.log"); - const logger = new RigLoggerFactoryClass({ logDir, maxFileSizeBytes: 100 }).app("race"); - await writeFile(activePath, "x".repeat(200), "utf8"); - await mkdir(lockPath); - - const source = ` - import { rm, writeFile } from "node:fs/promises"; - await new Promise((resolve) => setTimeout(resolve, 100)); - await writeFile(${JSON.stringify(activePath)}, "", "utf8"); - await rm(${JSON.stringify(lockPath)}, { recursive: true, force: true }); - `; - const child = spawn("bun", ["-e", source], { stdio: "pipe" }); - logger.info("Written after competing rotation."); - - expect(await new ChildProcessExitReaderClass().read(child)).toBe(0); - expect(await readFile(activePath, "utf8")).toContain("Written after competing rotation."); - expect(existsSync(lockPath)).toBe(false); - }); -}); diff --git a/src/runtime/logger.ts b/src/runtime/logger.ts deleted file mode 100644 index 517b28b..0000000 --- a/src/runtime/logger.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./logging/logger"; diff --git a/src/runtime/logging/logger.ts b/src/runtime/logging/logger.ts deleted file mode 100644 index 0797724..0000000 --- a/src/runtime/logging/logger.ts +++ /dev/null @@ -1,435 +0,0 @@ -import { - appendFileSync, - existsSync, - mkdirSync, - readdirSync, - renameSync, - rmSync, - statSync, - unlinkSync, -} from "node:fs"; -import { join } from "node:path"; -import { Writable } from "node:stream"; -import pino, { type Logger } from "pino"; -import { defineRuntime, defineService, defineSingleton } from "../../define"; -import { RigPathsClass, type PathOptions } from "../../config/paths"; - -export type RigLoggerOptions = PathOptions & { - env?: NodeJS.ProcessEnv; - level?: string; - logDir?: string; - maxFileSizeBytes?: number; - retentionDays?: number; - now?: () => number; -}; - -type ResolvedRigLoggerOptions = { - enabled: boolean; - level: string; - logDir: string; - maxFileSizeBytes: number; - retentionDays: number; - now: () => number; -}; - -const DefaultMaxFileSizeBytes = 5 * 1024 * 1024; -const DefaultRetentionDays = 7; -const MillisecondsPerDay = 24 * 60 * 60 * 1000; - -function positiveNumber(params: { - optionValue: number | undefined; - envValue: string | undefined; - fallback: number; -}): number { - const value = params.optionValue ?? (params.envValue ? Number(params.envValue) : undefined); - return value && Number.isFinite(value) && value > 0 ? value : params.fallback; -} - -function resolveLoggerOptions(params: { options: RigLoggerOptions }): ResolvedRigLoggerOptions { - const env = params.options.env ?? process.env; - const paths = new RigPathsClass(params.options); - return { - enabled: env.RIG_LOG !== "0", - level: params.options.level ?? env.RIG_LOG_LEVEL ?? "info", - logDir: params.options.logDir ?? env.RIG_LOG_DIR ?? paths.logsDir, - maxFileSizeBytes: positiveNumber({ - optionValue: params.options.maxFileSizeBytes, - envValue: env.RIG_LOG_MAX_BYTES, - fallback: DefaultMaxFileSizeBytes, - }), - retentionDays: positiveNumber({ - optionValue: params.options.retentionDays, - envValue: env.RIG_LOG_RETENTION_DAYS, - fallback: DefaultRetentionDays, - }), - now: params.options.now ?? Date.now, - }; -} - -export const RigLoggerOptionResolverSingleton = defineSingleton({ - params: {}, - deps: {}, - resolve: resolveLoggerOptions, -}); - -export type LogRotationLockParams = { - logDir: string; - waitTimeoutMs?: number; - staleAfterMs?: number; -}; - -export type LogRotationLockDeps = { - now: () => number; - wait: (buffer: Int32Array, index: number, value: number, timeout: number) => string; - makeDirectory: (path: string) => void; - readStatus: (path: string) => { mtimeMs: number }; - rename: (from: string, to: string) => void; - remove: (path: string) => void; -}; - -const logRotationLockDeps: LogRotationLockDeps = { - now: Date.now, - wait(buffer, index, value, timeout) { - return Atomics.wait(buffer, index, value, timeout); - }, - makeDirectory(path) { - mkdirSync(path); - }, - readStatus(path) { - return statSync(path); - }, - rename(from, to) { - renameSync(from, to); - }, - remove(path) { - rmSync(path, { recursive: true, force: true }); - }, -}; - -function errorCode(params: { error: unknown }): string | undefined { - if (typeof params.error !== "object" || params.error === null || !("code" in params.error)) { - return undefined; - } - return typeof params.error.code === "string" ? params.error.code : undefined; -} - -export class LogRotationLockRuntime extends defineRuntime({ - params: { logDir: "" } as LogRotationLockParams, - deps: logRotationLockDeps, -}) { - private readonly lockPath = join(this.params.logDir, ".rig.log.rotation.lock"); - private readonly waitBuffer = new Int32Array(new SharedArrayBuffer(4)); - - private tryAcquire(_params: {}): boolean { - try { - this.deps.makeDirectory(this.lockPath); - return true; - } catch (error) { - if (errorCode({ error }) === "EEXIST") return false; - throw error; - } - } - - private recoverStaleLock(_params: {}): void { - try { - if ( - this.deps.now() - this.deps.readStatus(this.lockPath).mtimeMs <= - (this.params.staleAfterMs ?? 30_000) - ) { - return; - } - const stalePath = `${this.lockPath}.stale-${process.pid}-${this.deps.now()}`; - this.deps.rename(this.lockPath, stalePath); - this.deps.remove(stalePath); - } catch (error) { - if (["ENOENT", "EEXIST"].includes(errorCode({ error }) ?? "")) return; - throw error; - } - } - - public run(params: { operation: () => void }): boolean { - const startedAt = this.deps.now(); - while (!this.tryAcquire({})) { - this.recoverStaleLock({}); - if (this.deps.now() - startedAt >= (this.params.waitTimeoutMs ?? 1_000)) return false; - this.deps.wait(this.waitBuffer, 0, 0, 10); - } - - try { - params.operation(); - return true; - } finally { - this.deps.remove(this.lockPath); - } - } -} - -export type LogRotationLockClass = { run(operation: () => void): boolean }; - -type LogRotationLockConstructor = { - new (params: LogRotationLockParams, deps?: LogRotationLockDeps): LogRotationLockClass; - readonly prototype: LogRotationLockClass; -}; - -type LogRotationLockAdapter = LogRotationLockClass & { - readonly resource: LogRotationLockRuntime; -}; - -const LogRotationLockClassAdapter = function constructLogRotationLock( - this: LogRotationLockAdapter, - params: LogRotationLockParams, - deps: LogRotationLockDeps = logRotationLockDeps, -): void { - Object.defineProperty(this, "resource", { - value: new LogRotationLockRuntime({ params, deps }), - }); -}; -Object.defineProperty(LogRotationLockClassAdapter, "name", { value: "LogRotationLockClass" }); -Object.defineProperty(LogRotationLockClassAdapter.prototype, "run", { - configurable: true, - value: function run(this: LogRotationLockAdapter, operation: () => void) { - return this.resource.run({ operation }); - }, - writable: true, -}); - -export const LogRotationLockClass = - LogRotationLockClassAdapter as unknown as LogRotationLockConstructor; - -function logFileSize(params: { path: string }): number { - try { - return statSync(params.path).size; - } catch { - return 0; - } -} - -function createRollingLogDestination(options: ResolvedRigLoggerOptions): Writable { - const activeFileName = "rig.log"; - const rotationLock = new LogRotationLockClass({ logDir: options.logDir }); - let activeSize = 0; - - function activePath(): string { - return join(options.logDir, activeFileName); - } - - function logEntries(): string[] { - try { - return readdirSync(options.logDir).filter(function isArchive(entry) { - return entry.startsWith("rig-") && entry.endsWith(".log"); - }); - } catch { - /* v8 ignore next */ - return []; - } - } - - function cleanupExpiredLogs(): void { - const cutoff = options.now() - options.retentionDays * MillisecondsPerDay; - for (const entry of logEntries()) { - const path = join(options.logDir, entry); - try { - if (statSync(path).mtimeMs < cutoff) unlinkSync(path); - } catch { - /* v8 ignore next */ - // Log cleanup should never prevent the requested Rig command from running. - } - } - } - - function archivePath(): string { - const timestamp = new Date(options.now()).toISOString().replace(/[:.]/g, "-"); - for (let index = 0; ; index++) { - const suffix = index === 0 ? "" : `-${index}`; - const candidate = join(options.logDir, `rig-${timestamp}${suffix}.log`); - if (!existsSync(candidate)) return candidate; - } - } - - function rotateIfNeeded(nextBytes: number): void { - rotationLock.run(function rotate() { - const lockedSize = logFileSize({ path: activePath() }); - if (lockedSize === 0 || lockedSize + nextBytes <= options.maxFileSizeBytes) { - activeSize = lockedSize; - return; - } - renameSync(activePath(), archivePath()); - activeSize = 0; - cleanupExpiredLogs(); - }); - } - - function appendBuffers(buffers: Buffer[]): void { - let pending: Buffer[] = []; - let pendingBytes = 0; - function flush() { - if (pendingBytes === 0) return; - appendFileSync( - activePath(), - pending.length === 1 ? pending[0]! : Buffer.concat(pending, pendingBytes), - ); - activeSize += pendingBytes; - pending = []; - pendingBytes = 0; - } - - for (const buffer of buffers) { - if (activeSize + pendingBytes + buffer.byteLength > options.maxFileSizeBytes) { - flush(); - rotateIfNeeded(buffer.byteLength); - } - pending.push(buffer); - pendingBytes += buffer.byteLength; - } - flush(); - } - - mkdirSync(options.logDir, { recursive: true }); - rotationLock.run(cleanupExpiredLogs); - activeSize = logFileSize({ path: activePath() }); - - return new Writable({ - write(chunk, _encoding, callback) { - try { - appendBuffers([Buffer.from(chunk)]); - queueMicrotask(callback); - } catch (error) { - /* v8 ignore next */ - callback(error instanceof Error ? error : new Error(String(error))); - } - }, - writev(chunks, callback) { - try { - appendBuffers(chunks.map((entry) => Buffer.from(entry.chunk))); - queueMicrotask(callback); - } catch (error) { - /* v8 ignore next */ - callback(error instanceof Error ? error : new Error(String(error))); - } - }, - }); -} - -const destinations = new Map(); - -export const RigLoggerDestinationRegistrySingleton = defineSingleton({ - params: {}, - deps: {}, - get(params: { options: ResolvedRigLoggerOptions }): Writable { - const key = [ - params.options.logDir, - params.options.maxFileSizeBytes, - params.options.retentionDays, - ].join("\0"); - const existing = destinations.get(key); - if (existing) return existing; - const destination = createRollingLogDestination(params.options); - destinations.set(key, destination); - return destination; - }, -}); - -type RigLoggerFactoryDeps = { - pino: typeof pino; - destination: typeof RigLoggerDestinationRegistrySingleton.get; -}; - -const RigLoggerFactoryProductionDeps: RigLoggerFactoryDeps = { - pino, - destination(params) { - return RigLoggerDestinationRegistrySingleton.get(params); - }, -}; - -export class RigLoggerFactoryService extends defineService({ - params: {} as RigLoggerOptions, - deps: RigLoggerFactoryProductionDeps, -}) { - private readonly options = RigLoggerOptionResolverSingleton.resolve({ options: this.params }); - private logger: Logger | undefined; - - private base(_params: {}): Logger { - if (this.logger) return this.logger; - if (!this.options.enabled) { - this.logger = this.deps.pino({ enabled: false }); - return this.logger; - } - this.logger = this.deps.pino( - { - name: "rig", - level: this.options.level, - base: { app: "rig", pid: process.pid }, - timestamp: this.deps.pino.stdTimeFunctions.isoTime, - serializers: { err: this.deps.pino.stdSerializers.err }, - }, - this.deps.destination({ options: this.options }), - ); - return this.logger; - } - - public app(params: { component: string }): Logger { - return this.base({}).child({ - prefix: `rig:${params.component}`, - component: params.component, - }); - } - - public tool(params: { tool: string; command: string }): Logger { - return this.base({}).child({ - prefix: `tool:${params.tool}.${params.command}`, - component: "tool", - tool: params.tool, - command: params.command, - }); - } -} - -export type RigLoggerFactoryClass = { - app(component?: string): Logger; - tool(tool: string, command: string): Logger; -}; - -type RigLoggerFactoryConstructor = { - new (options?: RigLoggerOptions): RigLoggerFactoryClass; - readonly prototype: RigLoggerFactoryClass; -}; - -type RigLoggerFactoryAdapter = RigLoggerFactoryClass & { - readonly resource: RigLoggerFactoryService; -}; - -const RigLoggerFactoryClassAdapter = function constructRigLoggerFactory( - this: RigLoggerFactoryAdapter, - options: RigLoggerOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new RigLoggerFactoryService({ - params: options, - deps: RigLoggerFactoryProductionDeps, - }), - }); -}; -Object.defineProperty(RigLoggerFactoryClassAdapter, "name", { value: "RigLoggerFactoryClass" }); -Object.defineProperties(RigLoggerFactoryClassAdapter.prototype, { - app: { - configurable: true, - value: function app(this: RigLoggerFactoryAdapter, component = "app") { - return this.resource.app({ component }); - }, - writable: true, - }, - tool: { - configurable: true, - value: function createToolLogger( - this: RigLoggerFactoryAdapter, - toolName: string, - command: string, - ) { - return this.resource.tool({ tool: toolName, command }); - }, - writable: true, - }, -}); - -export const RigLoggerFactoryClass = - RigLoggerFactoryClassAdapter as unknown as RigLoggerFactoryConstructor; diff --git a/src/runtime/output/envelope.test.ts b/src/runtime/output/envelope.test.ts deleted file mode 100644 index 3dfa81d..0000000 --- a/src/runtime/output/envelope.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { describe, expect, test } from "vite-plus/test"; -import { - EnvelopeFactoryClass, - EnvelopeFactorySingleton, - envelopeFactory, - type ErrorEnvelope, - type SuccessEnvelope, -} from "./envelope"; - -describe("EnvelopeFactorySingleton", () => { - test("success returns a data envelope with an empty errors array", () => { - const result = EnvelopeFactorySingleton.success({ data: { ok: true } }); - expect(result).toEqual({ data: { ok: true }, errors: [] }); - }); - - test("success preserves null and undefined data", () => { - expect(EnvelopeFactorySingleton.success({ data: null })).toEqual({ - data: null, - errors: [], - }); - expect(EnvelopeFactorySingleton.success({ data: undefined })).toEqual({ - data: undefined, - errors: [], - }); - }); - - test("error returns a null-data envelope with a single issue", () => { - const result = EnvelopeFactorySingleton.error({ code: "INPUT_ERROR", message: "bad input" }); - expect(result).toMatchObject({ - data: null, - errors: [{ code: "INPUT_ERROR", message: "bad input" }], - }); - }); - - test("error includes details when provided", () => { - const result = EnvelopeFactorySingleton.error({ - code: "INTERNAL_ERROR", - message: "unexpected", - details: { field: "x" }, - }); - expect(result.errors[0].details).toEqual({ field: "x" }); - }); -}); - -describe("EnvelopeFactoryClass adapter", () => { - test("new EnvelopeFactoryClass() produces a usable instance", () => { - const factory = new EnvelopeFactoryClass(); - expect(factory.success({ data: 42 })).toEqual({ data: 42, errors: [] }); - expect(factory.error({ code: "X", message: "y" })).toMatchObject({ - data: null, - errors: [{ code: "X", message: "y" }], - }); - }); - - test("instanceof EnvelopeFactoryClass is true for instances", () => { - const factory = new EnvelopeFactoryClass(); - expect(factory instanceof EnvelopeFactoryClass).toBe(true); - }); - - test("envelopeFactory is a shared pre-built instance", () => { - expect(envelopeFactory.success({ data: "shared" })).toEqual({ - data: "shared", - errors: [], - }); - expect(envelopeFactory instanceof EnvelopeFactoryClass).toBe(true); - }); -}); diff --git a/src/runtime/output/envelope.ts b/src/runtime/output/envelope.ts deleted file mode 100644 index 418d1ca..0000000 --- a/src/runtime/output/envelope.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { defineSingleton } from "../../define"; -import type { ErrorCode } from "../../errors/codes"; - -export type RigIssue = { - code: ErrorCode | string; - message: string; - details?: unknown; -}; - -export type SuccessEnvelope = { - data: unknown; - errors: []; -}; - -export type ErrorEnvelope = { - data: null; - errors: [RigIssue]; -}; - -function buildSuccessEnvelope(params: { data: unknown }): SuccessEnvelope { - return { data: params.data, errors: [] }; -} - -function buildErrorEnvelope(params: { - code: ErrorCode | string; - message: string; - details?: unknown; -}): ErrorEnvelope { - return { - data: null, - errors: [{ code: params.code, message: params.message, details: params.details }], - }; -} - -export const EnvelopeFactorySingleton = defineSingleton({ - params: {}, - deps: {}, - success: buildSuccessEnvelope, - error: buildErrorEnvelope, -}); - -// --- Class-free adapter (backward compatibility) --- - -type EnvelopeFactoryCompatibility = { - success(params: { data: unknown }): SuccessEnvelope; - error(params: { code: ErrorCode | string; message: string; details?: unknown }): ErrorEnvelope; -}; - -type EnvelopeFactoryConstructor = { - new (): EnvelopeFactoryCompatibility; - readonly prototype: EnvelopeFactoryCompatibility; -}; - -const EnvelopeFactoryClassAdapter = function constructEnvelopeFactory() {}; -Object.defineProperty(EnvelopeFactoryClassAdapter, "name", { value: "EnvelopeFactoryClass" }); -Object.defineProperty(EnvelopeFactoryClassAdapter.prototype, "success", { - configurable: true, - value: function success(params: { data: unknown }): SuccessEnvelope { - return EnvelopeFactorySingleton.success(params); - }, - writable: true, -}); -Object.defineProperty(EnvelopeFactoryClassAdapter.prototype, "error", { - configurable: true, - value: function error(params: { - code: ErrorCode | string; - message: string; - details?: unknown; - }): ErrorEnvelope { - return EnvelopeFactorySingleton.error(params); - }, - writable: true, -}); - -export const EnvelopeFactoryClass = - EnvelopeFactoryClassAdapter as unknown as EnvelopeFactoryConstructor; - -export const envelopeFactory = new EnvelopeFactoryClass(); diff --git a/src/runtime/output/truncation.test.ts b/src/runtime/output/truncation.test.ts deleted file mode 100644 index d373263..0000000 --- a/src/runtime/output/truncation.test.ts +++ /dev/null @@ -1,216 +0,0 @@ -import { describe, expect, test } from "vite-plus/test"; -import { - DEFAULT_TRUNCATION_MAX_BYTES, - DEFAULT_TRUNCATION_MAX_LINES, - RigOutputTruncator, - RigOutputTruncatorClass, - RigOutputTruncatorService, - type OutputTruncationOptions, - type TextTruncationResult, -} from "./truncation"; - -describe("constants", () => { - test("DEFAULT_TRUNCATION_MAX_BYTES is 50 KiB", () => { - expect(DEFAULT_TRUNCATION_MAX_BYTES).toBe(50 * 1024); - }); - - test("DEFAULT_TRUNCATION_MAX_LINES is 2000", () => { - expect(DEFAULT_TRUNCATION_MAX_LINES).toBe(2000); - }); -}); - -// --------------------------------------------------------------------------- -// Deterministic service builder tests -// --------------------------------------------------------------------------- - -function defaultTmpDir(): string { - return "/tmp"; -} - -async function defaultMkdtemp(): Promise { - return "/tmp/rig-test-output"; -} - -async function defaultNodeWriteFile(): Promise {} - -function defaultJoinPaths(...parts: string[]): string { - return parts.join("/"); -} - -function buildTestService( - configOverrides: Required, - overrides: { - bunWrite?: ((path: string, data: string) => Promise) | undefined; - nodeWriteFile?: (path: string, data: string) => Promise; - mkdtemp?: (prefix: string) => Promise; - } = {}, -): RigOutputTruncatorService { - return new RigOutputTruncatorService({ - params: configOverrides, - deps: { - tmpdir: defaultTmpDir, - mkdtemp: overrides.mkdtemp ?? defaultMkdtemp, - nodeWriteFile: overrides.nodeWriteFile ?? defaultNodeWriteFile, - join: defaultJoinPaths, - encoder: new TextEncoder(), - decoder: new TextDecoder(), - getBunWrite: function getBunWrite() { - return overrides.bunWrite; - }, - }, - }); -} - -describe("RigOutputTruncatorService", () => { - test("returns data unchanged when within both limits", async () => { - const Service = buildTestService({ maxBytes: 1000, maxLines: 100 }); - const result = await Service.truncateData({ data: { ok: true } }); - expect(result).toEqual({ ok: true }); - }); - - test("returns undefined unchanged when data is undefined", async () => { - const Service = buildTestService({ maxBytes: 100, maxLines: 10 }); - const result = await Service.truncateData({ data: undefined }); - expect(result).toBeUndefined(); - }); - - test("returns truncation envelope when data exceeds line limit", async () => { - const Service = buildTestService({ maxBytes: 10_000, maxLines: 2 }); - const result = (await Service.truncateData({ data: { text: "a\nb\nc\nd" } })) as { - truncated: boolean; - omittedLines: number; - fullOutputPath: string; - message: string; - }; - expect(result.truncated).toBe(true); - expect(result.omittedLines).toBeGreaterThan(0); - expect(result.message).toContain("Output truncated"); - }); - - test("returns truncation envelope when data exceeds byte limit", async () => { - const Service = buildTestService({ maxBytes: 10, maxLines: 1000 }); - const result = (await Service.truncateData({ - data: { text: "this is a fairly long string that will exceed ten bytes" }, - })) as { truncated: boolean; fullOutputPath: string }; - expect(result.truncated).toBe(true); - }); - - test("nodeWriteFile dep is called when bunWrite is absent", async () => { - const writtenPaths: string[] = []; - const Service = buildTestService( - { maxBytes: 5, maxLines: 1 }, - { - nodeWriteFile: async function nodeWriteFile(path: string) { - writtenPaths.push(path); - }, - bunWrite: undefined, - }, - ); - await Service.truncateData({ data: "truncatable string data" }); - expect(writtenPaths).toHaveLength(1); - }); - - test("bunWrite dep is called instead of nodeWriteFile when provided", async () => { - const bunCalls: string[] = []; - const nodeCalls: string[] = []; - const Service = buildTestService( - { maxBytes: 5, maxLines: 1 }, - { - bunWrite: async function bunWrite(path: string) { - bunCalls.push(path); - }, - nodeWriteFile: async function nodeWriteFile(path: string) { - nodeCalls.push(path); - }, - }, - ); - await Service.truncateData({ data: "truncatable string data" }); - expect(bunCalls).toHaveLength(1); - expect(nodeCalls).toHaveLength(0); - }); - - test("truncation envelope contains all expected fields", async () => { - const Service = buildTestService({ maxBytes: 10, maxLines: 1000 }); - const result = (await Service.truncateData({ - data: { text: "very long text here" }, - })) as Record; - expect(result).toMatchObject({ - truncated: true, - strategy: "head", - previewFormat: "partial-json", - fullOutputFormat: "json", - maxBytes: 10, - maxLines: 1000, - }); - expect(typeof result.preview).toBe("string"); - expect(typeof result.fullOutputPath).toBe("string"); - expect(typeof result.totalBytes).toBe("number"); - expect(typeof result.shownBytes).toBe("number"); - expect(typeof result.omittedBytes).toBe("number"); - expect(typeof result.message).toBe("string"); - }); -}); - -describe("RigOutputTruncatorService", () => { - test("production service is built with default limits", async () => { - const result = await RigOutputTruncator.truncateData({ data: { ok: true } }); - expect(result).toEqual({ ok: true }); - }); -}); - -// --------------------------------------------------------------------------- -// Class-free adapter tests -// --------------------------------------------------------------------------- - -describe("RigOutputTruncatorClass adapter", () => { - test("new RigOutputTruncatorClass() returns a usable instance", async () => { - const truncator = new RigOutputTruncatorClass({ maxBytes: 1000, maxLines: 100 }); - const result = await truncator.truncateData({ ok: true }); - expect(result).toEqual({ ok: true }); - }); - - test("instanceof RigOutputTruncatorClass is true for instances", () => { - const truncator = new RigOutputTruncatorClass(); - expect(truncator instanceof RigOutputTruncatorClass).toBe(true); - }); - - test("adapter with default options delegates to production service", async () => { - const truncator = new RigOutputTruncatorClass(); - const result = await truncator.truncateData({ small: "data" }); - expect(result).toEqual({ small: "data" }); - }); - - test("adapter exposes truncator compat helper with truncate method", () => { - const truncator = new RigOutputTruncatorClass({ maxBytes: 10, maxLines: 2 }); - const truncatorHelper = ( - truncator as unknown as { - truncator: { - truncate(value: string, options: Required): TextTruncationResult; - }; - } - ).truncator; - expect(truncatorHelper.truncate("", { maxBytes: 10, maxLines: 10 })).toMatchObject({ - totalBytes: 0, - totalLines: 0, - truncated: false, - }); - expect(truncatorHelper.truncate("abc", { maxBytes: 2, maxLines: 10 })).toMatchObject({ - truncated: true, - }); - }); - - test("adapter exposes sizeFormatter compat helper with format method", () => { - const truncator = new RigOutputTruncatorClass(); - const formatter = (truncator as unknown as { sizeFormatter: { format(bytes: number): string } }) - .sizeFormatter; - expect(formatter.format(500)).toBe("500B"); - expect(formatter.format(1536)).toBe("1.5KB"); - expect(formatter.format(3 * 1024 * 1024)).toBe("3.0MB"); - }); - - test("no-arg constructor uses default limits", async () => { - const truncator = new RigOutputTruncatorClass(); - const tiny = await truncator.truncateData({ tiny: true }); - expect(tiny).toEqual({ tiny: true }); - }); -}); diff --git a/src/runtime/output/truncation.ts b/src/runtime/output/truncation.ts deleted file mode 100644 index bf43dbe..0000000 --- a/src/runtime/output/truncation.ts +++ /dev/null @@ -1,205 +0,0 @@ -import { mkdtemp, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { defineService } from "../../define"; - -export const DEFAULT_TRUNCATION_MAX_BYTES = 50 * 1024; -export const DEFAULT_TRUNCATION_MAX_LINES = 2000; - -export type TextTruncationResult = { - content: string; - truncated: boolean; - totalBytes: number; - totalLines: number; - outputBytes: number; - outputLines: number; -}; - -export type OutputTruncationOptions = { - maxBytes?: number; - maxLines?: number; -}; - -// --- Internal helpers --- - -function createTextTruncator(params: { encoder: TextEncoder; decoder: TextDecoder }) { - function truncate( - value: string, - options: Required, - ): TextTruncationResult { - const totalBytes = params.encoder.encode(value).byteLength; - const totalLines = value.length === 0 ? 0 : value.split("\n").length; - let content = value; - - if (totalLines > options.maxLines) { - content = content.split("\n").slice(0, options.maxLines).join("\n"); - } - - if (params.encoder.encode(content).byteLength > options.maxBytes) { - content = params.decoder.decode(params.encoder.encode(content).slice(0, options.maxBytes)); - } - - const outputBytes = params.encoder.encode(content).byteLength; - const outputLines = content.length === 0 ? 0 : content.split("\n").length; - - return { - content, - truncated: outputBytes < totalBytes || outputLines < totalLines, - totalBytes, - totalLines, - outputBytes, - outputLines, - }; - } - - return { truncate }; -} - -const rigTextTruncatorHelper = createTextTruncator({ - encoder: new TextEncoder(), - decoder: new TextDecoder(), -}); - -const rigSizeFormatterHelper = { - format(bytes: number): string { - if (bytes < 1024) return `${bytes}B`; - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`; - return `${(bytes / (1024 * 1024)).toFixed(1)}MB`; - }, -}; - -// --- Bun/Node boundary helper --- - -/* v8 ignore next 3 */ -function getBunWrite(): ((path: string, data: string) => Promise) | undefined { - if (typeof Bun === "undefined") return undefined; - return async function bunWrite(path: string, data: string) { - await Bun.write(path, data); - }; -} - -// --- Service deps --- - -const RigOutputTruncatorServiceDeps = { - tmpdir: function getTmpDir() { - return tmpdir(); - }, - mkdtemp: async function mkdtempAdapter(prefix: string) { - return mkdtemp(prefix); - }, - nodeWriteFile: async function nodeWriteFile(path: string, data: string) { - await writeFile(path, data, "utf8"); - }, - join: function joinPaths(...paths: string[]) { - return join(...paths); - }, - encoder: new TextEncoder(), - decoder: new TextDecoder(), - getBunWrite, -}; - -// --- Serializer --- - -function serializeOutputData(value: unknown): string { - return JSON.stringify(value, null, 2) ?? "null"; -} - -export class RigOutputTruncatorService extends defineService({ - params: { - maxBytes: DEFAULT_TRUNCATION_MAX_BYTES, - maxLines: DEFAULT_TRUNCATION_MAX_LINES, - }, - deps: RigOutputTruncatorServiceDeps, -}) { - private readonly truncator = createTextTruncator({ - encoder: this.deps.encoder, - decoder: this.deps.decoder, - }); - - private async writeOutputFile(params: { serialized: string }): Promise { - const dir = await this.deps.mkdtemp(this.deps.join(this.deps.tmpdir(), "rig-output-")); - const path = this.deps.join(dir, "data.json"); - const bunWrite = this.deps.getBunWrite(); - if (bunWrite !== undefined) await bunWrite(path, `${params.serialized}\n`); - else await this.deps.nodeWriteFile(path, `${params.serialized}\n`); - return path; - } - - public async truncateData(params: { data: unknown }): Promise { - const serialized = serializeOutputData(params.data); - const result = this.truncator.truncate(serialized, { - maxBytes: this.params.maxBytes, - maxLines: this.params.maxLines, - }); - - if (!result.truncated) return params.data; - - const fullOutputPath = await this.writeOutputFile({ serialized }); - const omittedBytes = result.totalBytes - result.outputBytes; - const omittedLines = result.totalLines - result.outputLines; - - return { - truncated: true, - strategy: "head", - preview: result.content, - previewFormat: "partial-json", - fullOutputPath, - fullOutputFormat: "json", - maxBytes: this.params.maxBytes, - maxLines: this.params.maxLines, - totalBytes: result.totalBytes, - totalLines: result.totalLines, - shownBytes: result.outputBytes, - shownLines: result.outputLines, - omittedBytes, - omittedLines, - message: `Output truncated: showing ${result.outputLines} of ${result.totalLines} lines (${rigSizeFormatterHelper.format(result.outputBytes)} of ${rigSizeFormatterHelper.format(result.totalBytes)}). Full output saved to: ${fullOutputPath}`, - }; - } -} - -export const RigOutputTruncator = new RigOutputTruncatorService(); - -// --- Class-free adapter (backward compatibility) --- - -type RigOutputTruncatorCompatibility = { - truncateData(data: unknown): Promise; -}; - -type RigOutputTruncatorConstructor = { - new (options?: OutputTruncationOptions): RigOutputTruncatorCompatibility; - readonly prototype: RigOutputTruncatorCompatibility; -}; - -function RigOutputTruncatorClassAdapter(options: OutputTruncationOptions = {}) { - const maxBytes = options.maxBytes ?? DEFAULT_TRUNCATION_MAX_BYTES; - const maxLines = options.maxLines ?? DEFAULT_TRUNCATION_MAX_LINES; - - const service = - maxBytes === DEFAULT_TRUNCATION_MAX_BYTES && maxLines === DEFAULT_TRUNCATION_MAX_LINES - ? RigOutputTruncator - : new RigOutputTruncatorService({ - params: { maxBytes, maxLines }, - deps: RigOutputTruncatorServiceDeps, - }); - - const adapter = Object.create(RigOutputTruncatorClassAdapter.prototype) as Record< - string, - unknown - >; - Object.defineProperties(adapter, { - truncateData: { - configurable: true, - value: function truncateData(data: unknown) { - return service.truncateData({ data }); - }, - writable: true, - }, - truncator: { configurable: true, value: rigTextTruncatorHelper, writable: true }, - sizeFormatter: { configurable: true, value: rigSizeFormatterHelper, writable: true }, - }); - return adapter; -} - -export const RigOutputTruncatorClass = - RigOutputTruncatorClassAdapter as unknown as RigOutputTruncatorConstructor; diff --git a/src/runtime/package-root.ts b/src/runtime/package-root.ts deleted file mode 100644 index 666d789..0000000 --- a/src/runtime/package-root.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./package/package-root"; diff --git a/src/runtime/package/package-root.test.ts b/src/runtime/package/package-root.test.ts deleted file mode 100644 index 07764d5..0000000 --- a/src/runtime/package/package-root.test.ts +++ /dev/null @@ -1,153 +0,0 @@ -import { posix } from "node:path"; -import { fileURLToPath } from "node:url"; -import { describe, expect, it, vi } from "vite-plus/test"; -import { RigPackageRootClass, RigPackageRootService, rigPackageRoot } from "./package-root"; - -function createHarness() { - const state: { - environmentRoot: string | undefined; - argvEntrypoint: string | undefined; - execPath: string; - } = { - environmentRoot: undefined, - argvEntrypoint: undefined, - execPath: "/runtime/bin/node", - }; - const existingPaths = new Set(); - const realpaths = new Map(); - const getRigPackageRootEnvironment = vi.fn<() => string | undefined>( - function getRigPackageRootEnvironment() { - return state.environmentRoot; - }, - ); - const getArgvEntrypoint = vi.fn<() => string | undefined>(function getArgvEntrypoint() { - return state.argvEntrypoint; - }); - const getExecPath = vi.fn<() => string>(function getExecPath() { - return state.execPath; - }); - - const Service = new RigPackageRootService({ - params: {}, - deps: { - getRigPackageRootEnvironment, - getArgvEntrypoint, - getExecPath, - existsSync(pathValue) { - return existingPaths.has(pathValue); - }, - realpathSync(pathValue) { - const realpath = realpaths.get(pathValue); - if (realpath) return realpath; - throw new Error(`Missing path: ${pathValue}`); - }, - basename: posix.basename, - dirname: posix.dirname, - join: posix.join, - resolve: posix.resolve, - fileURLToPath, - }, - }); - - return { - Service, - state, - existingPaths, - realpaths, - getRigPackageRootEnvironment, - getArgvEntrypoint, - getExecPath, - }; -} - -describe("RigPackageRootService", () => { - it("preserves environment, argv, executable, and Bun lookup precedence", () => { - const harness = createHarness(); - - harness.state.environmentRoot = "/configured/../configured-root"; - expect(harness.Service.find({ metaUrl: "file:///module.ts" })).toBe("/configured-root"); - expect(harness.getArgvEntrypoint).not.toHaveBeenCalled(); - - harness.state.environmentRoot = undefined; - harness.state.argvEntrypoint = "/workspace/src/cli.ts"; - harness.realpaths.set("/workspace/src/cli.ts", "/workspace/src/cli.ts"); - expect(harness.Service.find({ metaUrl: "file:///module.ts" })).toBe("/workspace"); - - harness.state.argvEntrypoint = "/missing/cli.ts"; - harness.state.execPath = "/installed/dist/rig.mjs"; - harness.realpaths.set("/installed/dist/rig.mjs", "/installed/dist/rig.mjs"); - expect(harness.Service.find({ metaUrl: "file:///module.ts" })).toBe("/installed"); - - harness.state.execPath = "/bun/bin/bun"; - expect(harness.Service.find({ metaUrl: "file:///$bunfs/root" })).toBe("/bun/bin"); - expect(harness.getRigPackageRootEnvironment).toHaveBeenCalledTimes(4); - }); - - it("reads dynamic process and environment dependencies for every find call", () => { - const harness = createHarness(); - - harness.state.environmentRoot = "/first"; - expect(harness.Service.find({ metaUrl: "file:///module.ts" })).toBe("/first"); - - harness.state.environmentRoot = "/second"; - expect(harness.Service.find({ metaUrl: "file:///module.ts" })).toBe("/second"); - expect(harness.getRigPackageRootEnvironment).toHaveBeenCalledTimes(2); - }); - - it("resolves entrypoints through realpaths and safe absolute fallbacks", () => { - const harness = createHarness(); - - expect(harness.Service.fromEntrypoint({ entrypoint: undefined })).toBeUndefined(); - harness.realpaths.set("/link", "/repo/src/cli.ts"); - expect(harness.Service.fromEntrypoint({ entrypoint: "/link" })).toBe("/repo"); - harness.realpaths.set("/dist-link", "/repo/dist/rig.mjs"); - expect(harness.Service.fromEntrypoint({ entrypoint: "/dist-link" })).toBe("/repo"); - harness.realpaths.set("/other", "/repo/bin/rig"); - expect(harness.Service.fromEntrypoint({ entrypoint: "/other" })).toBeUndefined(); - - harness.existingPaths.add("/repo/src/fallback.ts"); - expect(harness.Service.fromEntrypoint({ entrypoint: "/repo/src/fallback.ts" })).toBe("/repo"); - expect(harness.Service.fromEntrypoint({ entrypoint: "/missing" })).toBeUndefined(); - }); - - it("finds package files from modules and preserves the final fallback", () => { - const harness = createHarness(); - harness.state.argvEntrypoint = "/missing/cli.ts"; - harness.state.execPath = "/missing/node"; - harness.existingPaths.add("/workspace/package.json"); - - expect(harness.Service.fromModule({ metaUrl: "file:///workspace/src/deep/module.ts" })).toBe( - "/workspace", - ); - expect(harness.Service.find({ metaUrl: "file:///workspace/src/deep/module.ts" })).toBe( - "/workspace", - ); - expect( - harness.Service.packageFile({ - metaUrl: "file:///workspace/src/deep/module.ts", - parts: ["dist", "rig.mjs"], - }), - ).toBe("/workspace/dist/rig.mjs"); - - harness.existingPaths.clear(); - expect(harness.Service.fromModule({ metaUrl: "file:///module.ts" })).toBeUndefined(); - expect( - harness.Service.fromModule({ metaUrl: "file:///a/b/c/d/e/f/g/h/i/j/module.ts" }), - ).toBeUndefined(); - expect(harness.Service.find({ metaUrl: "file:///a/b/c/d/e/f/g/h/i/j/module.ts" })).toBe( - "/a/b/c/d/e/f/g/h", - ); - }); - - it("keeps class-free legacy adapters and variadic packageFile behavior", () => { - const PackageRoot = new RigPackageRootClass(); - const root = PackageRoot.find(import.meta.url); - - expect(PackageRoot).toBeInstanceOf(RigPackageRootClass); - expect(rigPackageRoot).toBeInstanceOf(RigPackageRootClass); - expect(PackageRoot.packageFile(import.meta.url, "dist", "rig.mjs")).toBe( - posix.join(root, "dist", "rig.mjs"), - ); - expect(PackageRoot.fromEntrypoint(undefined)).toBeUndefined(); - }); -}); diff --git a/src/runtime/package/package-root.ts b/src/runtime/package/package-root.ts deleted file mode 100644 index bd6f70b..0000000 --- a/src/runtime/package/package-root.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { existsSync, realpathSync } from "node:fs"; -import { basename, dirname, join, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; -import { defineService } from "../../define"; - -type RigPackageRootServiceDeps = { - getRigPackageRootEnvironment: () => string | undefined; - getArgvEntrypoint: () => string | undefined; - getExecPath: () => string; - existsSync: (pathValue: string) => boolean; - realpathSync: (pathValue: string) => string; - basename: (pathValue: string) => string; - dirname: (pathValue: string) => string; - join: (...parts: string[]) => string; - resolve: (pathValue: string) => string; - fileURLToPath: (metaUrl: string) => string; -}; - -const RigPackageRootServiceDeps: RigPackageRootServiceDeps = { - getRigPackageRootEnvironment: function getRigPackageRootEnvironment() { - return process.env.RIG_PACKAGE_ROOT; - }, - getArgvEntrypoint: function getArgvEntrypoint() { - return process.argv[1]; - }, - getExecPath: function getExecPath() { - return process.execPath; - }, - existsSync, - realpathSync, - basename, - dirname, - join, - resolve, - fileURLToPath, -}; - -function isBunBinary(params: { metaUrl: string }): boolean { - return ( - params.metaUrl.includes("$bunfs") || - params.metaUrl.includes("~BUN") || - params.metaUrl.includes("%7EBUN") - ); -} - -export class RigPackageRootService extends defineService({ - params: {}, - deps: RigPackageRootServiceDeps, -}) { - private safeRealpath(params: { pathValue: string }): string | undefined { - try { - return this.deps.realpathSync(params.pathValue); - } catch { - const absolute = this.deps.resolve(params.pathValue); - /* v8 ignore next */ - return this.deps.existsSync(absolute) ? absolute : undefined; - } - } - - public fromEntrypoint(params: { entrypoint: string | undefined }): string | undefined { - if (!params.entrypoint) return undefined; - const resolved = this.safeRealpath({ pathValue: params.entrypoint }); - if (!resolved) return undefined; - - const parent = this.deps.dirname(resolved); - const parentName = this.deps.basename(parent); - if (parentName === "src" || parentName === "dist") return this.deps.dirname(parent); - return undefined; - } - - public fromModule(params: { metaUrl: string }): string | undefined { - let current = this.deps.dirname(this.deps.fileURLToPath(params.metaUrl)); - for (let depth = 0; depth < 8; depth += 1) { - if (this.deps.existsSync(this.deps.join(current, "package.json"))) return current; - const parent = this.deps.dirname(current); - if (parent === current) return undefined; - current = parent; - } - return undefined; - } - - public find(params: { metaUrl: string }): string { - const environmentRoot = this.deps.getRigPackageRootEnvironment(); - if (environmentRoot) return this.deps.resolve(environmentRoot); - - const argvRoot = this.fromEntrypoint({ entrypoint: this.deps.getArgvEntrypoint() }); - if (argvRoot) return argvRoot; - - const execRoot = this.fromEntrypoint({ entrypoint: this.deps.getExecPath() }); - if (execRoot) return execRoot; - - if (isBunBinary(params)) return this.deps.dirname(this.deps.getExecPath()); - - const moduleRoot = this.fromModule(params); - if (moduleRoot) return moduleRoot; - - return this.deps.join(this.deps.dirname(this.deps.fileURLToPath(params.metaUrl)), "..", ".."); - } - - public packageFile(params: { metaUrl: string; parts: readonly string[] }): string { - return this.deps.join(this.find({ metaUrl: params.metaUrl }), ...params.parts); - } -} - -export const RigPackageRoot = new RigPackageRootService(); - -type RigPackageRootCompatibility = { - find(metaUrl: string): string; - packageFile(metaUrl: string, ...parts: string[]): string; - fromEntrypoint(entrypoint: string | undefined): string | undefined; - fromModule(metaUrl: string): string | undefined; -}; - -type RigPackageRootConstructor = { - new (): RigPackageRootCompatibility; - new (params: object, deps: object): RigPackageRootCompatibility; - readonly prototype: RigPackageRootCompatibility; -}; - -function createRigPackageRootCompatibility( - service: RigPackageRootService, -): RigPackageRootCompatibility { - return Object.assign(Object.create(RigPackageRootClassAdapter.prototype), { - find(metaUrl: string) { - return service.find({ metaUrl }); - }, - packageFile(metaUrl: string, ...parts: string[]) { - return service.packageFile({ metaUrl, parts }); - }, - fromEntrypoint(entrypoint: string | undefined) { - return service.fromEntrypoint({ entrypoint }); - }, - fromModule(metaUrl: string) { - return service.fromModule({ metaUrl }); - }, - }) as RigPackageRootCompatibility; -} - -function RigPackageRootClassAdapter() { - return createRigPackageRootCompatibility(RigPackageRoot); -} - -export const RigPackageRootClass = - RigPackageRootClassAdapter as unknown as RigPackageRootConstructor; -export const rigPackageRoot = new RigPackageRootClass(); diff --git a/src/runtime/process/shell.ts b/src/runtime/process/shell.ts deleted file mode 100644 index eb618c3..0000000 --- a/src/runtime/process/shell.ts +++ /dev/null @@ -1,343 +0,0 @@ -import { spawn, type ChildProcess } from "node:child_process"; -import { defineProvider, defineService, defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import type { RigShell, ShellOptions, ShellResult } from "../../tools/types"; - -const DefaultTimeoutMs = 30_000; -const DefaultMaxOutputBytes = 1_048_576; -const TerminationGraceMs = 100; -const TruncationMarker = "\n[rig: output truncated]"; - -function decodeUtf8Prefix(params: { buffer: Buffer }): string { - const strictDecoder = new TextDecoder("utf-8", { fatal: true }); - for (let removed = 0; removed <= Math.min(3, params.buffer.byteLength); removed++) { - try { - return strictDecoder.decode(params.buffer.subarray(0, params.buffer.byteLength - removed)); - } catch { - // A UTF-8 code point may be split at the byte boundary. Try the preceding boundary. - } - } - return new TextDecoder().decode(params.buffer); -} - -export const BoundedOutputCollectorSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { maxBytes: number }) { - const chunks: Buffer[] = []; - let capturedBytes = 0; - let truncated = false; - - function capture(captureParams: { buffer: Buffer }): void { - const remaining = Math.max(0, params.maxBytes - capturedBytes); - if (remaining > 0) { - const captured = captureParams.buffer.subarray(0, remaining); - chunks.push(captured); - capturedBytes += captured.byteLength; - } - if (captureParams.buffer.byteLength > remaining) truncated = true; - } - - function result(_params: {}): string { - const content = decodeUtf8Prefix({ - buffer: Buffer.concat(chunks, capturedBytes), - }); - return truncated ? `${content}${TruncationMarker}` : content; - } - - return { capture, result }; - }, -}); - -type ManagedProcessFactoryDeps = { - processGroup: boolean; - killProcessGroup: (pid: number, signal: NodeJS.Signals) => void; - setTimer: (callback: () => void, delay: number) => NodeJS.Timeout; - clearTimer: (timer: NodeJS.Timeout) => void; -}; - -const ManagedProcessFactoryProductionDeps: ManagedProcessFactoryDeps = { - processGroup: process.platform !== "win32", - killProcessGroup(pid, signal) { - process.kill(-pid, signal); - }, - setTimer(callback, delay) { - return setTimeout(callback, delay); - }, - clearTimer(timer) { - clearTimeout(timer); - }, -}; - -export class ManagedProcessFactoryService extends defineService({ - params: {}, - deps: ManagedProcessFactoryProductionDeps, -}) { - public create(params: { child: ChildProcess; command: string[]; timeoutMs: number }) { - let timedOut = false; - let timeout: NodeJS.Timeout | undefined; - let escalation: NodeJS.Timeout | undefined; - let spawnError: Error | undefined; - - const signal = (signalParams: { signal: NodeJS.Signals }): void => { - try { - /* v8 ignore else -- Windows does not expose POSIX process groups */ - if (this.deps.processGroup && params.child.pid) { - this.deps.killProcessGroup(params.child.pid, signalParams.signal); - } else { - params.child.kill(signalParams.signal); - } - } catch { - /* v8 ignore next -- the process may exit between close detection and signaling */ - return; - } - }; - - const terminate = (_params: {}): void => { - timedOut = true; - signal({ signal: "SIGTERM" }); - /* v8 ignore next -- process exit timing before forced escalation differs by platform */ - escalation = this.deps.setTimer(function forceTermination() { - signal({ signal: "SIGKILL" }); - }, TerminationGraceMs); - }; - - const wait = async (_params: {}): Promise => { - params.child.once("error", function captureSpawnError(error) { - spawnError = error; - }); - timeout = this.deps.setTimer(function terminateOnTimeout() { - terminate({}); - }, params.timeoutMs); - - try { - const exitCode = await new Promise(function waitForClose(resolveClose) { - params.child.once("close", function handleClose(code) { - resolveClose(code ?? 1); - }); - }); - if (spawnError) { - throw new RigErrorClass("SHELL_ERROR", `Command could not start: ${params.command[0]}`, { - command: params.command, - message: spawnError.message, - }); - } - if (timedOut) { - throw new RigErrorClass("SHELL_ERROR", `Command timed out after ${params.timeoutMs}ms.`, { - command: params.command, - }); - } - return exitCode; - } finally { - /* v8 ignore else -- every managed process installs a timeout */ - if (timeout) this.deps.clearTimer(timeout); - if (escalation) this.deps.clearTimer(escalation); - } - }; - - return { wait }; - } -} - -export const ManagedProcessFactory = new ManagedProcessFactoryService(); - -function shellQuote(params: { value: string }): string { - if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(params.value)) return params.value; - return `'${params.value.replaceAll("'", "'\\''")}'`; -} - -function renderTemplateValue(params: { value: unknown }): string { - if (Array.isArray(params.value)) { - return params.value - .map(function quoteItem(item) { - return shellQuote({ value: String(item) }); - }) - .join(" "); - } - return shellQuote({ value: String(params.value) }); -} - -function renderTemplateCommand(params: { - strings: TemplateStringsArray; - values: unknown[]; -}): string { - return params.strings.reduce(function appendTemplatePart(command, part, index) { - const value = - index < params.values.length ? renderTemplateValue({ value: params.values[index] }) : ""; - return `${command}${part}${value}`; - }, ""); -} - -function validateArgs(params: { args: string[] }): void { - if ( - !Array.isArray(params.args) || - params.args.length === 0 || - params.args.some(function invalidArgument(argument) { - return typeof argument !== "string" || argument.length === 0; - }) - ) { - throw new RigErrorClass( - "SHELL_ERROR", - "shell.exec expects a non-empty array of command arguments.", - { args: params.args }, - ); - } -} - -type BunRigShellDeps = { - spawn: typeof spawn; - env: NodeJS.ProcessEnv; - cwd: () => string; - detached: boolean; - createCollector: typeof BoundedOutputCollectorSingleton.create; - createManagedProcess: (params: { child: ChildProcess; command: string[]; timeoutMs: number }) => { - wait(params: {}): Promise; - }; -}; - -const BunRigShellProductionDeps: BunRigShellDeps = { - spawn, - env: process.env, - cwd: process.cwd.bind(process), - detached: process.platform !== "win32", - createCollector(params) { - return BoundedOutputCollectorSingleton.create(params); - }, - createManagedProcess(params) { - return ManagedProcessFactory.create(params); - }, -}; - -export class BunRigShellProvider extends defineProvider({ - params: {} as ShellOptions, - deps: BunRigShellProductionDeps, -}) { - private async runProcess(params: { - args: string[]; - options?: ShellOptions; - reportedCommand?: string[]; - }): Promise { - const options = params.options ?? {}; - const reportedCommand = params.reportedCommand ?? params.args; - const timeoutMs = options.timeoutMs ?? this.params.timeoutMs ?? DefaultTimeoutMs; - const maxOutputBytes = - options.maxOutputBytes ?? this.params.maxOutputBytes ?? DefaultMaxOutputBytes; - const child = this.deps.spawn(params.args[0]!, params.args.slice(1), { - cwd: options.cwd ?? this.params.cwd ?? this.deps.cwd(), - env: { ...this.deps.env, ...this.params.env, ...options.env }, - stdio: ["ignore", "pipe", "pipe"], - detached: this.deps.detached, - }); - const stdout = this.deps.createCollector({ maxBytes: maxOutputBytes }); - const stderr = this.deps.createCollector({ maxBytes: maxOutputBytes }); - child.stdout.on("data", function captureStdout(chunk: Buffer) { - stdout.capture({ buffer: chunk }); - }); - child.stderr.on("data", function captureStderr(chunk: Buffer) { - stderr.capture({ buffer: chunk }); - }); - const managed = this.deps.createManagedProcess({ - child, - command: reportedCommand, - timeoutMs, - }); - const exitCode = await managed.wait({}); - - return { - command: reportedCommand, - stdout: stdout.result({}), - stderr: stderr.result({}), - exitCode, - }; - } - - public async exec(params: { args: string[]; options?: ShellOptions }): Promise { - validateArgs({ args: params.args }); - return await this.runProcess(params); - } - - public async bash(params: { command: string; options?: ShellOptions }): Promise { - return await this.runProcess({ - args: ["bash", "-lc", params.command], - options: params.options, - reportedCommand: [params.command], - }); - } - - public async template(params: { - strings: TemplateStringsArray; - values: unknown[]; - }): Promise { - return await this.bash({ command: renderTemplateCommand(params) }); - } - - public async json(params: { args: string[]; options?: ShellOptions }): Promise { - const result = await this.exec(params); - if (result.exitCode !== 0) { - throw new RigErrorClass("SHELL_ERROR", "Command failed before JSON could be parsed.", result); - } - try { - return JSON.parse(result.stdout); - } catch (error) { - throw new RigErrorClass("SHELL_ERROR", "Command stdout was not valid JSON.", { - result, - error, - }); - } - } -} - -export type BunRigShellClass = RigShell; - -type BunRigShellConstructor = { - new (defaults?: ShellOptions): BunRigShellClass; - readonly prototype: BunRigShellClass; -}; - -type BunRigShellAdapter = BunRigShellClass & { readonly resource: BunRigShellProvider }; - -const BunRigShellClassAdapter = function constructBunRigShell( - this: BunRigShellAdapter, - defaults: ShellOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new BunRigShellProvider({ params: defaults, deps: BunRigShellProductionDeps }), - }); -}; -Object.defineProperty(BunRigShellClassAdapter, "name", { value: "BunRigShellClass" }); -Object.defineProperties(BunRigShellClassAdapter.prototype, { - $: { - configurable: true, - value: function template( - this: BunRigShellAdapter, - strings: TemplateStringsArray, - ...values: unknown[] - ) { - return this.resource.template({ strings, values }); - }, - writable: true, - }, - exec: { - configurable: true, - value: function exec(this: BunRigShellAdapter, args: string[], options: ShellOptions = {}) { - return this.resource.exec({ args, options }); - }, - writable: true, - }, - bash: { - configurable: true, - value: function bash(this: BunRigShellAdapter, command: string, options: ShellOptions = {}) { - return this.resource.bash({ command, options }); - }, - writable: true, - }, - json: { - configurable: true, - value: function json(this: BunRigShellAdapter, args: string[], options: ShellOptions = {}) { - return this.resource.json({ args, options }); - }, - writable: true, - }, -}); - -export const BunRigShellClass = BunRigShellClassAdapter as unknown as BunRigShellConstructor; diff --git a/src/runtime/shell.ts b/src/runtime/shell.ts deleted file mode 100644 index c4d7e9f..0000000 --- a/src/runtime/shell.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./process/shell"; diff --git a/src/runtime/support.test.ts b/src/runtime/support.test.ts deleted file mode 100644 index c532f87..0000000 --- a/src/runtime/support.test.ts +++ /dev/null @@ -1,1293 +0,0 @@ -import { afterEach, describe, expect, test, vi } from "vite-plus/test"; -import { existsSync } from "node:fs"; -import { - mkdir, - mkdtemp, - readFile, - realpath, - rm, - stat, - symlink, - utimes, - writeFile, -} from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join, resolve } from "node:path"; -import { pathToFileURL } from "node:url"; -import { z } from "zod"; -import { RigAgentInstructions } from "../agents/instructions"; -import { AgentInstructionSyncServiceClass } from "../agents/sync"; -import { RigConfigStoreClass } from "../config/config"; -import { RigPathsClass } from "../config/paths"; -import { rigConfigDefaults } from "../domains/settings/index.ts"; -import { DevLinkServiceClass } from "../dev/dev-link"; -import { ErrorCodes } from "../errors/codes"; -import { RigErrorClass, rigErrors } from "../errors/RigError"; -import { ToolDiscoveryServiceClass } from "../registry/discover"; -import { RegistryConfigServiceClass } from "../registry/registry"; -import { rigPackageRoot } from "./package-root"; -import { BunRigShellClass } from "./shell"; -import { RuntimeSupportClass } from "./support"; -import { RigOutputTruncatorClass } from "./truncation"; -import { ToolCreatorClass } from "../tools/create"; -import { ToolHelpRendererClass, ToolHelpServiceClass } from "../tools/help"; -import { ToolInspectorClass } from "../tools/inspect"; -import { ToolDefinitionValidatorClass, ToolLoaderClass } from "../tools/loader"; -import { ToolApiMigrationServiceClass } from "../tools/migration/tool-api-migration"; -import { ToolRunnerClass } from "../tools/run"; -import { ToolRuntimeInstructionSyncServiceClass } from "../tools/runtime-instruction"; -import { schemaRenderer } from "../tools/schema"; -import { args, createRigToolKit, defineTool, paths, RigTool, rig } from "../tools/sdk"; -import { commandIds } from "../tools/types"; -import { ToolTypecheckServiceClass } from "../tools/typecheck"; - -class TempWorkspaceStore { - private readonly paths: string[] = []; - private readonly originalEnv = { ...process.env }; - private readonly originalArgv = [...process.argv]; - private readonly originalCwd = process.cwd(); - private readonly originalExecPath = process.execPath; - - async create(prefix = "rig-coverage-"): Promise { - const path = await mkdtemp(join(tmpdir(), prefix)); - this.paths.push(path); - return path; - } - - async cleanup(): Promise { - process.chdir(this.originalCwd); - process.env = { ...this.originalEnv }; - process.argv.splice(0, process.argv.length, ...this.originalArgv); - Object.defineProperty(process, "execPath", { - value: this.originalExecPath, - configurable: true, - }); - vi.restoreAllMocks(); - await Promise.all( - this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), - ); - } -} - -const workspaces = new TempWorkspaceStore(); - -afterEach(async () => { - await workspaces.cleanup(); -}); - -async function writeTool(home: string, name: string, source: string, extension = "ts") { - const toolDir = join(home, "rig", "tools", name); - await mkdir(toolDir, { recursive: true }); - const toolPath = join(toolDir, `index.rig.${extension}`); - await writeFile(toolPath, source, "utf8"); - return { toolDir, toolPath }; -} - -function simpleToolSource(name: string, command = "echo") { - return `export default (rig) => rig.defineTool({ - name: ${JSON.stringify(name)}, - description: "${name} test tool.", - commands: { - ${command}: rig.defineCommand({ - description: "Echo text.", - input: rig.z.object({ text: rig.z.string().default("default") }), - output: rig.z.object({ text: rig.z.string() }), - run: async ({ input }) => ({ text: input.text }), - }), - }, -}); -`; -} - -describe("coverage support", () => { - test("syncs generated runtime comments into tool files", async () => { - const home = await workspaces.create(); - const created = await new ToolCreatorClass({ homeDir: home }).create("sample"); - const service = new ToolRuntimeInstructionSyncServiceClass({ homeDir: home }); - - expect(service.renderPrefix()).toContain("rig.$"); - expect(service.renderPrefix()).toContain("context.cache.query"); - expect(service.renderPrefix()).toContain("// rig:tool-api-version 2"); - expect(service.upsertPrefix("#!/usr/bin/env bun\nconsole.log(1);\n")).toMatch( - /^#!\/usr\/bin\/env bun\n\/\/ rig:runtime-reference:start/, - ); - expect(service.upsertPrefix("#!/usr/bin/env bun\nconsole.log(1);\n")).toContain( - "// rig:tool-api-version 1", - ); - expect(service.upsertPrefix("// rig:tool-api-version 0\nconsole.log(1);\n")).toContain( - "// rig:tool-api-version 1", - ); - expect(service.upsertPrefix("")).toContain("Rig tool runtime"); - - const first = await service.sync(); - expect(first.tools).toEqual([{ name: "sample", path: created.toolPath, changed: true }]); - const synced = await readFile(created.toolPath, "utf8"); - expect(synced).toMatch(/^\/\/ rig:runtime-reference:start/); - expect(synced).toContain("// rig:tool-api-version 2"); - expect(synced).toContain("Rig tool runtime"); - expect(synced).toContain("Bun Shell"); - - const second = await service.sync(); - expect(second.tools).toEqual([{ name: "sample", path: created.toolPath, changed: false }]); - - await writeFile(created.toolPath, synced.replace("Rig tool runtime", "Stale runtime"), "utf8"); - const third = await service.sync(); - expect(third.tools).toEqual([{ name: "sample", path: created.toolPath, changed: true }]); - expect(await readFile(created.toolPath, "utf8")).not.toContain("Stale runtime"); - - const emptyHome = await workspaces.create("rig-coverage-empty-tools-"); - await expect( - new ToolRuntimeInstructionSyncServiceClass({ homeDir: emptyHome }).sync(), - ).resolves.toEqual({ tools: [] }); - }); - - test("syncs agent instructions, managed blocks, and target discovery", async () => { - const home = await workspaces.create(); - const project = join(home, "project"); - const nested = join(project, "src"); - const packageProject = join(home, "package-project"); - const packageNested = join(packageProject, "app"); - const globalAgentSource = join(home, "wks", "AGENTS.md"); - const globalAgentLink = join(home, ".pi", "agent", "AGENTS.md"); - await mkdir(join(project, ".git"), { recursive: true }); - await mkdir(join(nested, ".claude"), { recursive: true }); - await mkdir(join(home, ".claude"), { recursive: true }); - await mkdir(join(home, ".pi", "agent"), { recursive: true }); - await mkdir(join(home, ".config", "opencode"), { recursive: true }); - await mkdir(join(home, "wks", ".git"), { recursive: true }); - await mkdir(packageNested, { recursive: true }); - await writeFile(join(project, "AGENTS.md"), "# Root agent notes\n", "utf8"); - await writeFile( - join(project, "CLAUDE.md"), - "# Ignored Claude notes\n\n", - "utf8", - ); - await writeFile( - join(home, ".claude", "CLAUDE.md"), - "# Ignored home Claude\n\n", - "utf8", - ); - await writeFile(join(nested, "CLAUDE.md"), "# Nested Claude notes\n", "utf8"); - await writeFile(join(nested, "package.json"), "{}\n", "utf8"); - await writeFile(join(packageProject, "package.json"), "{}\n", "utf8"); - await writeFile(join(packageProject, "AGENTS.md"), "# Package notes\n", "utf8"); - await writeFile(globalAgentSource, "# Global agent notes\n", "utf8"); - await writeFile( - join(home, ".config", "opencode", "opencode.json"), - JSON.stringify({ instructions: [globalAgentSource, "./missing.md", 42] }), - "utf8", - ); - await symlink(globalAgentSource, globalAgentLink); - await new RegistryConfigServiceClass({ homeDir: home }).add(join(project, "rig-tools")); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - - const service = new AgentInstructionSyncServiceClass({ homeDir: home, cwd: nested }); - expect(service.renderBlock("No tools found.")).toContain(RigAgentInstructions); - await expect( - (service as unknown as { realPath(path: string): Promise }).realPath( - join(home, "missing"), - ), - ).resolves.toBe(join(home, "missing")); - await expect( - (service as unknown as { isIgnored(path: string): Promise }).isIgnored( - join(home, "missing"), - ), - ).resolves.toBe(false); - await expect( - (service as unknown as { isIgnored(path: string): Promise }).isIgnored( - join(project, "CLAUDE.md"), - ), - ).resolves.toBe(true); - expect( - ( - service as unknown as { instructionScope(path: string): "all" | "visible" } - ).instructionScope("/tmp/global-agent.md"), - ).toBe("visible"); - const targets = await service.discoverTargets(); - expect(targets.map((target) => target.path)).toEqual([ - globalAgentLink, - join(project, "AGENTS.md"), - join(nested, ".claude", "CLAUDE.md"), - join(nested, "CLAUDE.md"), - ]); - expect(targets.find((target) => target.path === globalAgentLink)?.scope).toBe("all"); - - const first = await service.sync(); - expect(first).toMatchObject({ skipped: false }); - expect(first.targets.every((target) => target.changed)).toBe(true); - expect(await readFile(join(project, "AGENTS.md"), "utf8")).toContain( - "rig run sample.example text=example #", - ); - expect(await readFile(join(project, "CLAUDE.md"), "utf8")).not.toContain( - "", - ); - expect(await readFile(join(home, ".claude", "CLAUDE.md"), "utf8")).not.toContain( - "", - ); - expect(await readFile(join(nested, ".claude", "CLAUDE.md"), "utf8")).toContain( - "", - ); - expect(await readFile(globalAgentSource, "utf8")).toContain( - "rig run sample.example text=example #", - ); - - const second = await service.sync(); - expect(second.targets.every((target) => target.changed)).toBe(false); - - await writeFile( - join(project, "AGENTS.md"), - (await readFile(join(project, "AGENTS.md"), "utf8")).replace( - "sample # Describe what this tool does.", - "Stale tools.", - ), - "utf8", - ); - const third = await service.sync(); - expect( - third.targets.find((target) => target.path === join(project, "AGENTS.md"))?.changed, - ).toBe(true); - - expect( - ( - await new AgentInstructionSyncServiceClass({ - homeDir: home, - cwd: packageNested, - }).discoverTargets() - ).map((target) => target.path), - ).toContain(join(packageProject, "AGENTS.md")); - expect( - ( - await new AgentInstructionSyncServiceClass({ - homeDir: home, - cwd: join(home, "wks"), - }).discoverTargets() - ).filter((target) => target.path.endsWith("AGENTS.md")), - ).toHaveLength(1); - - await mkdir(join(home, ".opencode"), { recursive: true }); - await writeFile(join(home, ".opencode", "opencode.json"), "{", "utf8"); - await expect(service.discoverTargets()).resolves.toEqual(expect.any(Array)); - await writeFile(join(home, ".opencode", "opencode.json"), "[]", "utf8"); - await expect(service.discoverTargets()).resolves.toEqual(expect.any(Array)); - await writeFile( - join(home, ".opencode", "opencode.json"), - JSON.stringify({ instructions: ["~/wks/AGENTS.md", join(project, "AGENTS.md")] }), - "utf8", - ); - await expect(service.discoverTargets()).resolves.toEqual(expect.any(Array)); - - process.env.RIG_AGENT_SYNC = "0"; - await expect(service.sync()).resolves.toEqual({ skipped: true, targets: [] }); - delete process.env.RIG_AGENT_SYNC; - - const emptyHome = await workspaces.create("rig-coverage-empty-"); - const empty = join(emptyHome, "empty"); - await mkdir(empty); - await expect( - new AgentInstructionSyncServiceClass({ homeDir: emptyHome, cwd: empty }).sync(), - ).resolves.toMatchObject({ skipped: false, targets: [] }); - - // Exercise canSkipSync: set up a home with config, tool, and stamp file - const skipHome = await workspaces.create("rig-skip-sync-"); - const skipProject = join(skipHome, "project"); - await mkdir(join(skipProject, ".git"), { recursive: true }); - await writeFile(join(skipProject, "AGENTS.md"), "# Notes\n", "utf8"); - await new RegistryConfigServiceClass({ homeDir: skipHome }).add(join(skipProject, "rig-tools")); - await new ToolCreatorClass({ homeDir: skipHome }).create("skiptool"); - - const skipService = new AgentInstructionSyncServiceClass({ - homeDir: skipHome, - cwd: skipProject, - }); - const syncResult = await skipService.sync(); - expect(syncResult.targets.some((t) => t.changed)).toBe(true); - - // Second sync should skip via canSkipSync (stamp exists, no files changed) - const skipResult = await skipService.sync(); - expect(skipResult.targets.every((t) => !t.changed)).toBe(true); - - // Modify target file to invalidate cache - await writeFile(join(skipProject, "AGENTS.md"), "# Modified\n", "utf8"); - const reResult = await skipService.sync(); - expect(reResult.targets.some((t) => t.changed)).toBe(true); - }); - - test("fingerprints TypeScript and TSX tools across every configured registry", async () => { - const home = await workspaces.create(); - const project = join(home, "project"); - const customRegistry = join(project, "rig-tools"); - const agentPath = join(project, "AGENTS.md"); - await mkdir(join(project, ".git"), { recursive: true }); - await writeFile(agentPath, "# Agent notes\n", "utf8"); - await new ToolCreatorClass({ homeDir: home }).create("base"); - await new RegistryConfigServiceClass({ homeDir: home }).add(customRegistry); - const customToolDir = join(customRegistry, "custom"); - const customToolPath = join(customToolDir, "index.rig.tsx"); - await mkdir(customToolDir, { recursive: true }); - await writeFile(customToolPath, simpleToolSource("custom", "echo"), "utf8"); - - const service = new AgentInstructionSyncServiceClass({ homeDir: home, cwd: project }); - await service.sync(); - expect(await readFile(agentPath, "utf8")).toContain("custom.echo"); - const originalStat = await stat(customToolPath); - const changedSource = (await readFile(customToolPath, "utf8")).replace("echo", "ping"); - await writeFile(customToolPath, changedSource, "utf8"); - await utimes(customToolPath, originalStat.atime, originalStat.mtime); - - const changed = await service.sync(); - expect(changed.targets.some((target) => target.changed)).toBe(true); - expect(await readFile(agentPath, "utf8")).toContain("custom.ping"); - expect(await readFile(agentPath, "utf8")).not.toContain("custom.echo"); - }); - - test("exercises config reads, writes, defaults, and path helpers", async () => { - const home = await workspaces.create(); - const pathsForHome = new RigPathsClass({ homeDir: home }); - const configStore = new RigConfigStoreClass({ homeDir: home }); - - await expect(configStore.read()).rejects.toThrow("Could not read config"); - await mkdir(pathsForHome.rigDir, { recursive: true }); - await writeFile(pathsForHome.configPath, "{ nope", "utf8"); - await expect(configStore.read()).rejects.toThrow("Config is not valid JSON"); - await writeFile(pathsForHome.configPath, '{"version":2}', "utf8"); - await expect(configStore.read()).rejects.toThrow("Rig config is invalid"); - await expect(configStore.write({ version: 2 } as never)).rejects.toThrow( - "Rig config is invalid", - ); - - const config = rigConfigDefaults.create(); - config.customRegistries = ["~/custom-tools", join(home, "absolute-tools")]; - await configStore.write(config); - expect(await configStore.read()).toEqual(config); - expect(configStore.resolvedBaseRegistry({ ...config, baseRegistryDir: "" } as never)).toBe( - join(home, "rig", "tools"), - ); - expect(configStore.resolvedCustomRegistries(config)).toEqual([ - join(home, "custom-tools"), - join(home, "absolute-tools"), - ]); - expect(configStore.registryEntries(config).map((entry) => entry.kind)).toEqual([ - "base", - "custom", - "custom", - ]); - - const defaultPaths = new RigPathsClass(); - expect(defaultPaths.homeDir).toBeTruthy(); - expect(pathsForHome.expandTilde("~")).toBe(home); - expect(pathsForHome.expandTilde("~/tools")).toBe(join(home, "tools")); - expect(pathsForHome.expandTilde("relative")).toBe("relative"); - expect(pathsForHome.resolve("relative")).toBe(resolve(process.cwd(), "relative")); - expect(pathsForHome.resolve(join(home, "file"))).toBe(join(home, "file")); - expect(pathsForHome.defaultBaseRegistryDir).toBe("~/rig/tools"); - expect(pathsForHome.parentDir(join(home, "child", "file.txt"))).toBe(join(home, "child")); - }); - - test("exercises registry configuration and discovery edges", async () => { - const home = await workspaces.create(); - const service = new RegistryConfigServiceClass({ homeDir: home }); - const pathsForHome = new RigPathsClass({ homeDir: home }); - - const listed = await service.list(); - expect(listed.baseRegistryDir).toBe(pathsForHome.resolve("~/rig/tools")); - await expect(service.add("~/rig/tools")).rejects.toThrow( - "The base registry is already configured.", - ); - - const custom = join(home, "custom"); - await service.add(custom); - const addedTwice = await service.add(custom); - expect(addedTwice.customRegistries).toEqual([custom]); - await expect(service.remove(join(home, "missing"))).rejects.toThrow( - "Registry is not configured", - ); - - const discovery = new ToolDiscoveryServiceClass({ homeDir: home }); - expect(discovery.projectRootFor("/")).toBe("/"); - await expect(discovery.find("missing")).rejects.toThrow("Tool not found: missing"); - - await mkdir(join(home, "rig", "tools", "empty"), { recursive: true }); - await mkdir(join(home, "rig", "tools", "multi"), { recursive: true }); - await writeFile(join(home, "rig", "tools", "multi", "index.rig.ts"), "export default {};\n"); - await writeFile(join(home, "rig", "tools", "multi", "index.rig.tsx"), "export default {};\n"); - await writeFile(join(home, "rig", "tools", "not-a-dir"), "x\n"); - - await expect(discovery.discover()).rejects.toThrow("multiple Rig entry files"); - - await rm(join(home, "rig", "tools", "multi", "index.rig.tsx")); - const tools = await discovery.discover(); - expect(tools.map((tool) => tool.name)).toEqual(["multi"]); - await expect( - ( - discovery as unknown as { discoverRegistry(entry: unknown): Promise } - ).discoverRegistry({ kind: "custom", path: join(home, "does-not-exist") }), - ).resolves.toEqual([]); - }); - - test("exercises dev link status, rendering, force, and invalid roots", async () => { - const home = await workspaces.create(); - const repoRoot = await workspaces.create(); - await mkdir(join(repoRoot, "src"), { recursive: true }); - await writeFile(join(repoRoot, "package.json"), "{}\n", "utf8"); - await writeFile(join(repoRoot, "src", "cli.ts"), "console.log('rig');\n", "utf8"); - - const binDir = join(home, "bin"); - process.env.PATH = `${binDir}:${process.env.PATH ?? ""}`; - const serviceFromCwd = new DevLinkServiceClass({ homeDir: home }); - expect((await serviceFromCwd.status({ binDir })).repoRoot).toBe(process.cwd()); - - const service = new DevLinkServiceClass({ homeDir: home, repoRoot }); - const missing = await service.status({ binDir }); - expect(missing.exists).toBe(false); - expect(missing.binDirOnPath).toBe(true); - expect(service.renderLinkResult(missing)).toContain("On PATH: yes"); - - const linked = await service.link({ binDir }); - expect(linked.pointsToCurrentRepo).toBe(true); - expect(service.renderLinkResult({ ...linked, binDirOnPath: false })).toContain("Add "); - expect(service.renderUnlinkResult(linked)).toContain("Rig dev link removed."); - - await writeFile(join(binDir, "rig"), "#!/usr/bin/env bash\necho other\n", "utf8"); - await service.link({ binDir, force: true }); - await writeFile(join(binDir, "rig"), "#!/usr/bin/env bash\necho other\n", "utf8"); - await expect(service.unlink({ binDir })).rejects.toThrow("Refusing to remove non-Rig dev shim"); - const unlinked = await service.unlink({ binDir, force: true }); - expect(unlinked.exists).toBe(false); - await service.unlink({ binDir }); - - await mkdir(join(binDir, "rig"), { recursive: true }); - const directoryShim = await service.status({ binDir }); - expect(directoryShim.exists).toBe(true); - expect(directoryShim.isRigDevShim).toBe(false); - - delete process.env.PATH; - expect((await service.status({ binDir })).binDirOnPath).toBe(false); - - await expect( - new DevLinkServiceClass({ homeDir: home, repoRoot: home }).status(), - ).rejects.toThrow("Run dev link from the Rig repository root."); - }); - - test("exercises error conversion and exported codes", () => { - const rigError = new RigErrorClass("INPUT_ERROR", "bad", { field: "x" }); - expect(rigErrors.from(rigError)).toBe(rigError); - expect(rigErrors.from(new Error("plain"))).toMatchObject({ - code: "INTERNAL_ERROR", - message: "plain", - }); - expect(rigErrors.from("string failure")).toMatchObject({ - code: "INTERNAL_ERROR", - message: "string failure", - }); - expect(ErrorCodes.SHELL_ERROR).toBe("SHELL_ERROR"); - expect(ErrorCodes.TOOL_RUN_ERROR).toBe("TOOL_RUN_ERROR"); - }); - - test("exercises package-root detection modes", async () => { - const envRoot = await workspaces.create(); - process.env.RIG_PACKAGE_ROOT = envRoot; - expect(rigPackageRoot.find(import.meta.url)).toBe(envRoot); - expect(rigPackageRoot.packageFile(import.meta.url, "dist", "rig.mjs")).toBe( - join(envRoot, "dist", "rig.mjs"), - ); - delete process.env.RIG_PACKAGE_ROOT; - - const repoRoot = await workspaces.create(); - await mkdir(join(repoRoot, "src"), { recursive: true }); - const cliPath = join(repoRoot, "src", "cli.ts"); - await writeFile(cliPath, "", "utf8"); - process.argv[1] = cliPath; - expect(rigPackageRoot.find(import.meta.url)).toBe(await realpath(repoRoot)); - - process.argv[1] = join(repoRoot, "missing.ts"); - await mkdir(join(repoRoot, "dist"), { recursive: true }); - const execPath = join(repoRoot, "dist", "rig.mjs"); - await writeFile(execPath, "", "utf8"); - Object.defineProperty(process, "execPath", { value: execPath, configurable: true }); - expect(rigPackageRoot.find(import.meta.url)).toBe(await realpath(repoRoot)); - - Object.defineProperty(process, "execPath", { - value: join(repoRoot, "bin", "bun"), - configurable: true, - }); - process.argv[1] = join(repoRoot, "bin", "script"); - expect(rigPackageRoot.find("file:///%7EBUN/root")).toBe(join(repoRoot, "bin")); - expect( - ( - rigPackageRoot as unknown as { - fromEntrypoint(entrypoint: undefined): string | undefined; - } - ).fromEntrypoint(undefined), - ).toBeUndefined(); - - const symlinkRoot = await workspaces.create(); - await mkdir(join(symlinkRoot, "src"), { recursive: true }); - const realCli = join(symlinkRoot, "src", "cli.ts"); - const linkedCli = join(symlinkRoot, "src", "linked.ts"); - await writeFile(realCli, "", "utf8"); - await symlink(realCli, linkedCli); - process.argv[1] = linkedCli; - Object.defineProperty(process, "execPath", { - value: join(repoRoot, "bin", "bun"), - configurable: true, - }); - expect(rigPackageRoot.find(import.meta.url)).toBe(await realpath(symlinkRoot)); - - process.argv[1] = join(repoRoot, "missing", "script"); - Object.defineProperty(process, "execPath", { - value: join(repoRoot, "missing", "bun"), - configurable: true, - }); - const deepModule = join(repoRoot, "a", "b", "c", "d", "e", "f", "g", "h", "i", "mod.ts"); - expect(rigPackageRoot.find(pathToFileURL(deepModule).href)).toBe( - join(dirname(deepModule), "..", ".."), - ); - expect( - ( - rigPackageRoot as unknown as { - fromModule(metaUrl: string): string | undefined; - } - ).fromModule("file:///mod.ts"), - ).toBeUndefined(); - }); - - test("exercises shell execution, JSON parsing, validation, truncation, and timeouts", async () => { - const shell = new BunRigShellClass({ timeoutMs: 5_000, maxOutputBytes: 20 }); - const success = await shell.exec(["bun", "-e", "console.log('hello')"], { - cwd: process.cwd(), - env: { RIG_SHELL_TEST: "1" }, - maxOutputBytes: 100, - }); - expect(success).toMatchObject({ exitCode: 0, stdout: "hello\n" }); - - await expect(shell.$`echo ${"tagged ok"}`).resolves.toMatchObject({ - exitCode: 0, - stdout: "tagged ok\n", - }); - await expect(shell.$`printf '%s\n' ${["safe", "two words"]}`).resolves.toMatchObject({ - exitCode: 0, - stdout: "safe\ntwo words\n", - }); - await expect(shell.$`printf '%s\n' ${"safe; echo unsafe"}`).resolves.toMatchObject({ - exitCode: 0, - stdout: "safe; echo unsafe\n", - }); - await expect(shell.bash("echo bash-ok")).resolves.toMatchObject({ - exitCode: 0, - stdout: "bash-ok\n", - }); - - const bounded = await shell.exec( - ["bun", "-e", "process.stdout.write('x'.repeat(5_000_000)); console.error('y'.repeat(100))"], - { env: { FORCE_COLOR: undefined }, maxOutputBytes: 64 }, - ); - expect(bounded.stdout).toBe(`${"x".repeat(64)}\n[rig: output truncated]`); - expect(bounded.stderr).toBe(`${"y".repeat(64)}\n[rig: output truncated]`); - - const unicode = await shell.exec(["bun", "-e", "process.stdout.write('😀'.repeat(100))"], { - maxOutputBytes: 7, - }); - expect(unicode.stdout).toBe("😀\n[rig: output truncated]"); - expect(unicode.stdout).not.toContain("�"); - - const invalidUtf8 = await shell.bash("printf '\\377\\376\\375\\374'", { - maxOutputBytes: 10, - }); - expect(invalidUtf8.stdout).toContain("�"); - - const noOutput = await shell.exec(["bun", "-e", "process.stdout.write('hidden')"], { - maxOutputBytes: 0, - }); - expect(noOutput.stdout).toBe("\n[rig: output truncated]"); - - const markerRoot = await workspaces.create("rig-shell-timeout-"); - const delayedMarker = join(markerRoot, "delayed-marker"); - await expect( - shell.bash(`(sleep 0.2; touch ${delayedMarker}) & wait`, { timeoutMs: 10 }), - ).rejects.toThrow("Command timed out"); - await new Promise((resolvePromise) => setTimeout(resolvePromise, 300)); - expect(existsSync(delayedMarker)).toBe(false); - - await expect( - shell.exec(["bun", "-e", "process.on('SIGTERM', () => {}); setTimeout(() => {}, 1000)"], { - timeoutMs: 10, - }), - ).rejects.toThrow("Command timed out"); - await expect( - shell.bash("trap '' TERM; while true; do sleep 1; done", { timeoutMs: 10 }), - ).rejects.toThrow("Command timed out"); - - const failed = await shell.exec(["bun", "-e", "process.exit(2)"]); - expect(failed).toMatchObject({ exitCode: 2, stdout: "", stderr: "" }); - await expect(shell.exec(["rig-command-that-does-not-exist"])).rejects.toThrow( - "Command could not start", - ); - - await expect( - shell.json(["bun", "-e", "console.log(JSON.stringify({ok:true}))"]), - ).resolves.toEqual({ ok: true }); - await expect(shell.json(["bun", "-e", "process.exit(2)"])).rejects.toThrow( - "Command failed before JSON could be parsed.", - ); - await expect(shell.json(["bun", "-e", "console.log('nope')"])).rejects.toThrow( - "Command stdout was not valid JSON.", - ); - await expect(shell.exec([])).rejects.toThrow("shell.exec expects"); - await expect(shell.exec([""])).rejects.toThrow("shell.exec expects"); - await expect(shell.exec(["bun", 1 as never])).rejects.toThrow("shell.exec expects"); - - await expect(new BunRigShellClass().exec(["echo", "fallback"])).resolves.toMatchObject({ - command: ["echo", "fallback"], - stdout: "fallback\n", - exitCode: 0, - }); - await expect( - new BunRigShellClass({ env: { RIG_SHELL_DEFAULT: "available" } }).exec([ - "bash", - "-lc", - 'printf %s "$RIG_SHELL_DEFAULT"', - ]), - ).resolves.toMatchObject({ stdout: "available" }); - - const independentlyBounded = await new BunRigShellClass({ maxOutputBytes: 4 }).exec([ - "bun", - "-e", - "process.stdout.write('abcdefgh')", - ]); - expect(independentlyBounded.stdout).toBe("abcd\n[rig: output truncated]"); - }); - - test("exercises runtime support generation and truncation helpers", async () => { - const home = await workspaces.create(); - const registry = join(home, "registry"); - const support = new RuntimeSupportClass({ homeDir: home }); - await support.ensure([registry]); - expect(await readFile(join(registry, "tsconfig.json"), "utf8")).toContain("Generated by Rig"); - const runtimeSdk = await readFile(new RigPathsClass({ homeDir: home }).runtimeSdkPath, "utf8"); - expect(runtimeSdk).toContain("class RigManagedProcessClass"); - expect(runtimeSdk).toContain('detached: process.platform !== "win32"'); - expect(runtimeSdk).toContain("[rig: output truncated]"); - expect(runtimeSdk).toContain("^[A-Za-z0-9_-]+[.][A-Za-z0-9_-]+$"); - const runtimeTypes = await readFile( - new RigPathsClass({ homeDir: home }).runtimeTypesPath, - "utf8", - ); - expect(runtimeTypes).toContain("$(strings: TemplateStringsArray"); - expect(runtimeTypes).toContain("env: Env"); - expect(runtimeTypes).toContain("processEnv: NodeJS.ProcessEnv"); - expect(runtimeTypes).toContain("env?: Env"); - expect(runtimeTypes).toContain("setupDb?: (db: RigToolDatabase)"); - expect(runtimeTypes).toContain("kv: RigToolKvStore"); - expect(runtimeTypes).toContain("cache: RigToolCache"); - expect(runtimeTypes).toContain("log: RigToolLogger"); - expect(runtimeTypes).toContain("shell: RigShell"); - expect(runtimeTypes).toContain("collections: Collections"); - expect(runtimeTypes).toContain("collections?: Collections"); - await writeFile(join(registry, "tsconfig.json"), "{}\n", "utf8"); - await support.ensure([registry]); - expect(await readFile(join(registry, "tsconfig.json"), "utf8")).toBe("{}\n"); - - expect( - await new RigOutputTruncatorClass({ maxBytes: 100, maxLines: 10 }).truncateData({ ok: true }), - ).toEqual({ ok: true }); - const truncator = new RigOutputTruncatorClass({ maxBytes: 10, maxLines: 2 }); - expect(await truncator.truncateData(undefined)).toBeUndefined(); - const truncated = (await truncator.truncateData({ text: "a\nb\nc\nd" })) as { - truncated: boolean; - omittedLines: number; - fullOutputPath: string; - message: string; - }; - expect(truncated.truncated).toBe(true); - expect(truncated.omittedLines).toBeGreaterThan(0); - expect(existsSync(truncated.fullOutputPath)).toBe(true); - expect(truncated.message).toContain("Output truncated"); - - const textTruncator = ( - truncator as unknown as { - truncator: { - truncate(value: string, options: { maxBytes: number; maxLines: number }): unknown; - }; - } - ).truncator; - expect(textTruncator.truncate("", { maxBytes: 10, maxLines: 10 })).toMatchObject({ - totalBytes: 0, - totalLines: 0, - truncated: false, - }); - expect(textTruncator.truncate("abcdef", { maxBytes: 3, maxLines: 10 })).toMatchObject({ - content: "abc", - truncated: true, - }); - const formatter = (truncator as unknown as { sizeFormatter: { format(bytes: number): string } }) - .sizeFormatter; - expect(formatter.format(12)).toBe("12B"); - expect(formatter.format(2048)).toBe("2.0KB"); - expect(formatter.format(2 * 1024 * 1024)).toBe("2.0MB"); - }); - - test("exercises creator, help, inspection, listing, schemas, SDK, and type helpers", async () => { - const home = await workspaces.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await expect(new ToolCreatorClass({ homeDir: home }).create("sample")).rejects.toThrow( - "Tool already exists", - ); - - const renderer = new ToolHelpRendererClass(); - const loaded = await new ToolLoaderClass({ homeDir: home }).load("sample"); - const fullHelp = renderer.render(loaded.definition); - expect(fullHelp).toContain("# sample"); - await expect( - new ToolHelpServiceClass({ homeDir: home }).render("sample", "missing"), - ).rejects.toThrow("Command not found: sample.missing"); - - const command = loaded.definition.commands.example!; - expect( - renderer.render({ - ...loaded.definition, - commands: { example: { ...command, examples: [] } }, - }), - ).toContain("No examples declared."); - expect( - renderer.render({ - ...loaded.definition, - commands: { - example: { - ...command, - input: rig.z.object({ maybe: rig.z.string().optional() }), - examples: undefined, - }, - }, - }), - ).toContain("No examples declared."); - expect( - renderer.render({ - ...loaded.definition, - commands: { - example: { - ...command, - input: null as never, - output: null as never, - examples: [{ title: "Scalar", text: "Scalar input", input: "two words" }], - }, - }, - }), - ).toContain("'two words'"); - expect( - renderer.render({ - ...loaded.definition, - commands: { - example: { - ...command, - examples: [{ title: "Output only", text: "No input", output: { text: "done" } }], - }, - }, - }), - ).toContain("$ rig run sample.example"); - expect( - ( - renderer as unknown as { - renderCommand(tool: string, command: string, definition: unknown): string; - } - ).renderCommand("sample", "example", command), - ).toContain("### sample.example"); - expect( - new (await import("../tools/list")).CommandRunExampleRendererClass().render( - "sample", - "example", - command, - ), - ).toContain("rig run sample.example"); - expect( - (renderer as unknown as { renderExampleArgs(input: unknown): string }).renderExampleArgs({ - first: "two words", - second: "ok", - }), - ).toBe("first='two words' second=ok"); - expect((renderer as unknown as { typeName(schema: unknown): string }).typeName(null)).toBe( - "unknown", - ); - expect( - (renderer as unknown as { typeName(schema: unknown): string }).typeName({ - type: ["string", "number"], - }), - ).toBe("string | number"); - expect((renderer as unknown as { typeName(schema: unknown): string }).typeName({})).toBe( - "unknown", - ); - - await writeTool(home, "metadata", simpleToolSource("metadata")); - const inspector = new ToolInspectorClass({ homeDir: home }); - expect(await inspector.inspect("sample")).toMatchObject({ name: "sample" }); - expect(await inspector.inspect("metadata", "echo")).toMatchObject({ examples: [] }); - await expect(inspector.inspect("sample", "missing")).rejects.toThrow( - "Command not found: sample.missing", - ); - expect( - new (await import("../tools/list")).ToolListServiceClass({ homeDir: home }).renderPlain({ - tools: [], - }), - ).toBe("No Rig tools found."); - - expect(schemaRenderer.toJsonSchema({})).toMatchObject({ type: "unknown" }); - expect(schemaRenderer.summary(z.object({ text: z.string() }))).toContain("text"); - expect(commandIds.from("tool", "command")).toBe("tool.command"); - - const toolkit = createRigToolKit(); - await expect(toolkit.$`echo toolkit-ok`).resolves.toMatchObject({ - stdout: "toolkit-ok\n", - exitCode: 0, - }); - await expect(toolkit.run({ command: "bad" })).rejects.toThrow( - "Command id must use .", - ); - await expect(toolkit.run({ tool: "bad", command: "" })).rejects.toThrow( - "Command id must use .", - ); - await expect( - createRigToolKit({ homeDir: home }).run({ command: "missing.echo" }), - ).rejects.toThrow("Rig tool command failed: missing.echo"); - const builtArgs = toolkit - .args() - .raw("run") - .flag("--yes") - .flag("--no", false) - .value("--count", 2) - .value("--skip", null) - .value("--missing", undefined) - .values("--tag", ["a", "b"]) - .values("--empty", undefined) - .toArray(); - expect(builtArgs).toEqual(["run", "--yes", "--count", "2", "--tag", "a", "--tag", "b"]); - expect(args().raw("x").toArray()).toEqual(["x"]); - - const parent = join(home, "nested", "file.txt"); - await paths.ensureParent(parent); - expect(existsSync(dirname(parent))).toBe(true); - await writeFile(parent, "abc", "utf8"); - expect(paths.size(parent)).toBe(3); - expect(paths.size(join(home, "missing"))).toBe(0); - expect(paths.resolve(home, "~")).toBeTruthy(); - expect(paths.resolve(home, "~/file")).toContain("file"); - expect(paths.resolve(home, "relative")).toBe(join(home, "relative")); - expect(paths.resolve(home, parent)).toBe(parent); - expect(paths.home()).toBeTruthy(); - - const definition = { name: "defined", description: "Defined.", commands: {} } as never; - expect(defineTool(definition)).toBe(definition); - const factory = () => definition; - expect(defineTool(factory)).toBe(factory); - expect(RigTool.define(factory)).toBe(factory); - expect(rig.defineTool(definition)).toBe(definition); - }); - - test("exercises loader validation and load failures", async () => { - const validator = new ToolDefinitionValidatorClass(); - const toolkit = createRigToolKit(); - const validCommand = { - description: "Valid command.", - input: toolkit.z.object({ text: toolkit.z.string() }), - output: toolkit.z.object({ text: toolkit.z.string() }), - examples: [{ title: "Example", text: "Example text." }], - run: async ({ input: commandInput }: { input: { text: string } }) => ({ - text: commandInput.text, - }), - }; - const validTool = { - name: "valid", - description: "Valid tool.", - env: toolkit.z.object({ API_TOKEN: toolkit.z.string() }), - commands: { echo: validCommand }, - }; - expect(validator.validateToolDefinition(validTool, "valid")).toBe(validTool); - expect( - validator.validateToolDefinition( - { description: "Derived name.", commands: { echo: validCommand } }, - "derived", - ).name, - ).toBe("derived"); - expect(() => validator.validateToolDefinition({}, undefined)).toThrow("Tool needs a name"); - expect(() => validator.validateToolName("")).toThrow("Invalid tool name"); - expect(() => validator.validateCommandName("")).toThrow("Invalid command name"); - - const invalidDefinitions: Array<[unknown, string]> = [ - [null, "Tool default export must be an object"], - [{}, "needs a description"], - [{ name: "bad-name", description: "x", commands: {} }, "does not match its folder"], - [{ name: "valid", description: "", commands: {} }, "needs a description"], - [{ name: "valid", description: "x" }, "needs a commands object"], - [{ ...validTool, setupDb: true }, "setupDb must be a function"], - [{ ...validTool, env: true }, "needs a Zod schema for env"], - [{ ...validTool, collections: true }, "needs a collections object"], - [{ ...validTool, collections: { "../notes": {} } }, "Invalid collection name"], - [{ name: "valid", description: "x", commands: {} }, "must define at least one command"], - [{ ...validTool, commands: { "": validCommand } }, "Invalid command name"], - [{ ...validTool, commands: { echo: null } }, "Invalid command valid.echo"], - [ - { ...validTool, commands: { echo: { ...validCommand, description: "" } } }, - "needs a description", - ], - [ - { ...validTool, commands: { echo: { ...validCommand, input: null } } }, - "needs a Zod schema for input", - ], - [ - { ...validTool, commands: { echo: { ...validCommand, output: "not-a-schema" } } }, - "needs a Zod schema for output", - ], - [{ ...validTool, commands: { echo: { ...validCommand, examples: {} } } }, "Invalid examples"], - [ - { ...validTool, commands: { echo: { ...validCommand, examples: [null] } } }, - "Invalid example", - ], - [ - { - ...validTool, - commands: { echo: { ...validCommand, examples: [{ text: "Missing title" }] } }, - }, - "Invalid example title", - ], - [ - { - ...validTool, - commands: { echo: { ...validCommand, examples: [{ title: "Missing text" }] } }, - }, - "Invalid example text", - ], - [ - { ...validTool, commands: { echo: { ...validCommand, run: null } } }, - "needs a run function", - ], - ]; - - for (const [definition, message] of invalidDefinitions) { - expect(() => validator.validateToolDefinition(definition, "valid")).toThrow(message); - } - - const home = await workspaces.create(); - const loader = new ToolLoaderClass({ homeDir: home }); - loader.validateCommandName("echo"); - await expect(loader.load("nonexistent")).rejects.toThrow("Tool not found"); - await expect(loader.loadCommand("valid", "Bad")).rejects.toThrow("Tool not found"); - - await writeTool(home, "broken", "throw new Error('load failed');\n"); - await expect(loader.load("broken")).rejects.toThrow("Could not load tool broken"); - await writeTool( - home, - "factory", - "export default () => { throw new Error('factory failed'); };\n", - ); - await expect(loader.load("factory")).rejects.toThrow("Could not evaluate tool factory factory"); - await writeTool(home, "valid", simpleToolSource("valid")); - const discoveredValid = await new ToolDiscoveryServiceClass({ homeDir: home }).find("valid"); - await expect(loader.loadDiscovered(discoveredValid)).resolves.toMatchObject({ name: "valid" }); - await expect(loader.loadCommand("valid", "missing")).rejects.toThrow( - "Command not found: valid.missing", - ); - }); - - test("exercises runner input parsing, shell helpers, and error envelopes", async () => { - const home = await workspaces.create(); - await writeTool(home, "sample", simpleToolSource("sample")); - await writeTool( - home, - "scalar", - `export default (rig) => rig.defineTool({ - name: "scalar", - description: "Scalar test tool.", - commands: { - echo: rig.defineCommand({ - description: "Echo scalar.", - input: rig.z.string(), - output: rig.z.string(), - run: async ({ input }) => input, - }), - }, -}); -`, - ); - await writeTool( - home, - "writer", - `export default (rig) => rig.defineTool({ - name: "writer", - description: "Writer test tool.", - commands: { - save: rig.defineCommand({ - description: "Save text.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - ); - await writeTool( - home, - "sheller", - `export default (rig) => rig.defineTool({ - name: "sheller", - description: "Shell test tool.", - commands: { - blocked: rig.defineCommand({ - description: "Blocked shell.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => { - await rig.shell.exec(["bun", "-e", "console.log('blocked')"]); - return { ok: true }; - }, - }), - exec: rig.defineCommand({ - description: "Allowed shell exec.", - input: rig.z.object({}), - output: rig.z.object({ text: rig.z.string() }), - run: async () => { - const result = await rig.shell.exec(["bun", "-e", "console.log('ok')"]); - return { text: result.stdout.trim() }; - }, - }), - json: rig.defineCommand({ - description: "Allowed shell JSON.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => await rig.shell.json(["bun", "-e", "console.log(JSON.stringify({ok:true}))"]), - }), - }, -}); -`, - ); - await writeTool( - home, - "bad-output", - `export default (rig) => rig.defineTool({ - name: "bad-output", - description: "Bad output tool.", - commands: { - fail: rig.defineCommand({ - description: "Return wrong output.", - input: rig.z.object({}), - output: rig.z.object({ text: rig.z.string() }), - run: async () => ({ text: 123 }), - }), - }, -}); -`, - ); - await writeTool( - home, - "thrower", - `export default (rig) => rig.defineTool({ - name: "thrower", - description: "Throwing tool.", - commands: { - string: rig.defineCommand({ - description: "Throw a string.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => { throw "boom"; }, - }), - syntax: rig.defineCommand({ - description: "Throw an unexpected syntax error.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => { throw new SyntaxError("tool bug"); }, - }), - }, -}); -`, - ); - - const runner = new ToolRunnerClass({ homeDir: home }); - await expect(runner.run("sample", "echo", { homeDir: home })).resolves.toMatchObject({ - exitCode: 0, - envelope: { data: { text: "default" } }, - }); - await expect( - runner.run("sample", "echo", { homeDir: home, args: ['{"text":"json"}'] }), - ).resolves.toMatchObject({ envelope: { data: { text: "json" } } }); - await expect( - runner.run("sample", "echo", { homeDir: home, args: ["text=key-value"] }), - ).resolves.toMatchObject({ envelope: { data: { text: "key-value" } } }); - await expect( - runner.run("sample", "echo", { homeDir: home, args: ["two words"], dryRun: true }), - ).resolves.toMatchObject({ - envelope: { data: { commandLine: "rig run sample.echo 'two words'" } }, - }); - - const inputPath = join(home, "input.json"); - await writeFile(inputPath, '{"text":"file"}', "utf8"); - await expect( - runner.run("sample", "echo", { homeDir: home, inputFile: inputPath }), - ).resolves.toMatchObject({ envelope: { data: { text: "file" } } }); - await expect( - runner.run("sample", "echo", { homeDir: home, input: '{"text":"input"}' }), - ).resolves.toMatchObject({ envelope: { data: { text: "input" } } }); - await expect( - runner.run("sample", "echo", { homeDir: home, input: "{bad" }), - ).resolves.toMatchObject({ exitCode: 1, envelope: { errors: [{ code: "INPUT_ERROR" }] } }); - await expect( - runner.run("sample", "echo", { homeDir: home, input: "{}", args: ["x"] }), - ).resolves.toMatchObject({ exitCode: 1, envelope: { errors: [{ code: "INPUT_ERROR" }] } }); - await expect( - runner.run("sample", "echo", { homeDir: home, args: ["=x"] }), - ).resolves.toMatchObject({ exitCode: 1, envelope: { errors: [{ code: "INPUT_ERROR" }] } }); - await expect( - runner.run("sample", "echo", { homeDir: home, args: ["a", "b"] }), - ).resolves.toMatchObject({ exitCode: 1, envelope: { errors: [{ code: "INPUT_ERROR" }] } }); - - await expect( - runner.run("scalar", "echo", { homeDir: home, args: ["abc"] }), - ).resolves.toMatchObject({ envelope: { data: "abc" } }); - await expect( - runner.run("scalar", "echo", { homeDir: home, args: ["123"] }), - ).resolves.toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "VALIDATION_ERROR" }] }, - }); - await expect( - runner.run("scalar", "echo", { homeDir: home, args: ["a", "b"] }), - ).resolves.toMatchObject({ exitCode: 1, envelope: { errors: [{ code: "INPUT_ERROR" }] } }); - - await expect( - runner.run("writer", "save", { homeDir: home, args: ["text=hi"] }), - ).resolves.toMatchObject({ exitCode: 0, envelope: { data: { ok: true } } }); - - await expect(runner.run("sheller", "blocked", { homeDir: home })).resolves.toMatchObject({ - exitCode: 0, - envelope: { data: { ok: true } }, - }); - await expect(runner.run("sheller", "exec", { homeDir: home })).resolves.toMatchObject({ - exitCode: 0, - envelope: { data: { text: "ok" } }, - }); - await expect(runner.run("sheller", "json", { homeDir: home })).resolves.toMatchObject({ - exitCode: 0, - envelope: { data: { ok: true } }, - }); - await expect(runner.run("bad-output", "fail", { homeDir: home })).resolves.toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "OUTPUT_VALIDATION_ERROR" }] }, - }); - await expect(runner.run("thrower", "string", { homeDir: home })).resolves.toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "INTERNAL_ERROR", message: "boom" }] }, - }); - await expect(runner.run("thrower", "syntax", { homeDir: home })).resolves.toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "INTERNAL_ERROR", message: "tool bug" }] }, - }); - }); - - test("exercises typecheck success, all-tools mode, missing tools, and parser diagnostics", async () => { - const home = await workspaces.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const service = new ToolTypecheckServiceClass({ homeDir: home }); - const all = await service.typecheck(); - expect(all.ok).toBe(true); - expect(all.checked).toHaveLength(1); - await expect(service.typecheck("missing")).rejects.toThrow("Tool not found: missing"); - - const privateService = service as unknown as { - runTypeScript(path: string): { stdout: string; exitCode: number }; - parseHost(): { onUnRecoverableConfigFileDiagnostic(diagnostic: unknown): void }; - }; - expect(() => privateService.runTypeScript(join(home, "missing-tsconfig.json"))).toThrow( - "Unable to parse generated Rig tool tsconfig.", - ); - expect(() => - privateService.parseHost().onUnRecoverableConfigFileDiagnostic({ - category: 1, - code: 1000, - file: undefined, - length: 0, - start: 0, - messageText: "bad config", - }), - ).toThrow("Unable to parse generated Rig tool tsconfig."); - }); - - test("type-checks v2 env and collection context from a plain command definition", async () => { - const home = await workspaces.create(); - const source = `// rig:tool-api-version 2 -export default (rig: RigToolKit) => rig.defineTool({ - description: "Type-check the complete v2 authoring contract.", - env: rig.z.object({ TOKEN: rig.z.string() }), - collections: { - notes: { schema: rig.z.object({ title: rig.z.string() }) }, - }, - commands: (command) => ({ - add: command({ - description: "Add a note.", - input: rig.z.object({ title: rig.z.string() }), - output: rig.z.object({ title: rig.z.string(), token: rig.z.string() }), - run: async ({ input, env, collections }) => { - const entry = await collections.notes.create({ data: { title: input.title } }); - return { title: entry.data.title, token: env.TOKEN }; - }, - }), - }), -}); -`; - await writeTool(home, "contract", source); - - const typecheck = await new ToolTypecheckServiceClass({ homeDir: home }).typecheck("contract"); - expect(typecheck).toMatchObject({ ok: true, exitCode: 0 }); - const loaded = await new ToolLoaderClass({ homeDir: home }).loadDefinition("contract"); - expect(loaded.definition.name).toBe("contract"); - }); - - test("supports injected tool API migration collaborators", async () => { - const home = await workspaces.create(); - await new ToolCreatorClass({ homeDir: home }).create("current"); - let source = "// rig:tool-api-version 2\n"; - const service = new ToolApiMigrationServiceClass( - { homeDir: home }, - { - discovery: new ToolDiscoveryServiceClass({ homeDir: home }), - readSource: async () => source, - }, - ); - - await expect(service.inspect({ visibleFromPath: home })).resolves.toMatchObject({ - ready: true, - migrations: [], - unsupported: [], - }); - - source = "// rig:tool-api-version 999999999999999999999999\n"; - await expect(service.inspect()).resolves.toMatchObject({ - ready: false, - migrations: [{ fromVersion: 1, toVersion: 2 }], - }); - - expect( - service.renderAgentInstructions({ - report: { - currentVersion: 2, - ready: false, - migrations: [], - unsupported: [{ name: "future", path: "/future", fromVersion: 3, toVersion: 2 }], - }, - }), - ).toContain("newer API than this Rig installation supports"); - - const bunDiscovery = new ToolDiscoveryServiceClass({ homeDir: home }); - vi.spyOn(bunDiscovery, "discover").mockResolvedValue([ - { - name: "bun-source", - registryKind: "base", - registryPath: home, - toolDir: home, - toolPath: join(home, "index.rig.ts"), - }, - ]); - vi.stubGlobal("Bun", { - file: () => ({ text: async () => "// rig:tool-api-version 2\n" }), - }); - await expect( - new ToolApiMigrationServiceClass({ homeDir: home }, { discovery: bunDiscovery }).inspect(), - ).resolves.toMatchObject({ ready: true }); - }); -}); diff --git a/src/runtime/support.ts b/src/runtime/support.ts deleted file mode 100644 index 3c6eb6d..0000000 --- a/src/runtime/support.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../generated/runtime/runtime-support"; diff --git a/src/runtime/truncation.ts b/src/runtime/truncation.ts deleted file mode 100644 index 0363718..0000000 --- a/src/runtime/truncation.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./output/truncation"; diff --git a/src/runtime/update-check.test.ts b/src/runtime/update-check.test.ts deleted file mode 100644 index ba34f39..0000000 --- a/src/runtime/update-check.test.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { RigPathsClass } from "../config/paths"; -import { NpmUpdateCheckServiceClass } from "./update-check"; - -class UpdateCheckWorkspaceStore { - private readonly homes: string[] = []; - private readonly originalEnv = { ...process.env }; - - async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-update-check-")); - this.homes.push(home); - return home; - } - - async cleanup(): Promise { - process.env = { ...this.originalEnv }; - await Promise.all( - this.homes.splice(0).map((home) => rm(home, { recursive: true, force: true })), - ); - } -} - -const workspaces = new UpdateCheckWorkspaceStore(); - -afterEach(async () => { - await workspaces.cleanup(); -}); - -describe("npm update checks", () => { - test("skips checks when disabled", async () => { - const home = await workspaces.create(); - process.env.RIG_UPDATE_CHECK = "0"; - const service = new NpmUpdateCheckServiceClass({ - homeDir: home, - fetch: async () => { - throw new Error("fetch should not be called"); - }, - }); - - await expect(service.check("0.0.3")).resolves.toBeUndefined(); - }); - - test("fetches, caches, and renders newer npm versions", async () => { - const home = await workspaces.create(); - const urls: string[] = []; - const service = new NpmUpdateCheckServiceClass({ - homeDir: home, - now: () => 1000, - packageName: "@scope/tool", - fetch: async (url) => { - urls.push(url); - return { ok: true, json: async () => ({ version: "1.2.4" }) }; - }, - }); - - const notice = await service.check("1.2.3"); - - expect(urls).toEqual(["https://registry.npmjs.org/%40scope%2Ftool/latest"]); - expect(notice).toMatchObject({ - currentVersion: "1.2.3", - latestVersion: "1.2.4", - message: "Rig update available: @scope/tool 1.2.3 -> 1.2.4. Run npm install -g @scope/tool.", - }); - expect( - await readFile(new RigPathsClass({ homeDir: home }).updateCheckCachePath, "utf8"), - ).toContain('"latestVersion": "1.2.4"'); - - const cached = new NpmUpdateCheckServiceClass({ - homeDir: home, - now: () => 1001, - packageName: "@scope/tool", - fetch: async () => { - throw new Error("fresh cache should avoid fetch"); - }, - }); - await expect(cached.check("1.2")).resolves.toMatchObject({ latestVersion: "1.2.4" }); - await expect(cached.check("1.2.3")).resolves.toMatchObject({ latestVersion: "1.2.4" }); - await expect(cached.check("1.2.4")).resolves.toBeUndefined(); - await expect(cached.check("1.2.5")).resolves.toBeUndefined(); - - await writeFile( - new RigPathsClass({ homeDir: home }).updateCheckCachePath, - '{"checkedAt": 1002,"latestVersion":"1.2"}\n', - "utf8", - ); - const shorter = new NpmUpdateCheckServiceClass({ - homeDir: home, - now: () => 1003, - packageName: "@scope/tool", - fetch: async () => { - throw new Error("fresh cache should avoid fetch"); - }, - }); - await expect(shorter.check("1.2.1")).resolves.toBeUndefined(); - }); - - test("ignores stale cache misses and invalid registry data", async () => { - const home = await workspaces.create(); - const paths = new RigPathsClass({ homeDir: home }); - await mkdir(paths.rigDir, { recursive: true }); - await writeFile(paths.updateCheckCachePath, '{"checkedAt": 1}\n', "utf8"); - - const missing = new NpmUpdateCheckServiceClass({ - homeDir: home, - now: () => 10_000_000, - cacheTtlMs: 1, - fetch: async () => ({ ok: false, json: async () => ({ version: "9.9.9" }) }), - }); - await expect(missing.check("1.0.0")).resolves.toBeUndefined(); - - const invalid = new NpmUpdateCheckServiceClass({ - homeDir: home, - now: () => 10_000_000, - cacheTtlMs: 1, - fetch: async () => ({ ok: true, json: async () => ({ name: "rig" }) }), - }); - await expect(invalid.check("1.0.0")).resolves.toBeUndefined(); - - await writeFile(paths.updateCheckCachePath, "[]\n", "utf8"); - const invalidCache = new NpmUpdateCheckServiceClass({ - homeDir: home, - fetch: async () => ({ ok: false, json: async () => ({}) }), - }); - await expect(invalidCache.check("1.0.0")).resolves.toBeUndefined(); - }); - - test("handles invalid cache files and fetch failures", async () => { - const home = await workspaces.create(); - const paths = new RigPathsClass({ homeDir: home }); - await mkdir(paths.rigDir, { recursive: true }); - await writeFile(paths.updateCheckCachePath, "{ nope", "utf8"); - - const throwing = new NpmUpdateCheckServiceClass({ - homeDir: home, - fetch: async () => { - throw new Error("network failed"); - }, - }); - await expect(throwing.check("1.0.0")).resolves.toBeUndefined(); - - const aborted = new NpmUpdateCheckServiceClass({ - homeDir: home, - timeoutMs: 1, - fetch: async (_url, init) => - new Promise((_, reject) => { - init?.signal?.addEventListener("abort", () => reject(new Error("aborted"))); - }), - }); - await expect(aborted.check("1.0.0")).resolves.toBeUndefined(); - }); -}); diff --git a/src/runtime/update-check.ts b/src/runtime/update-check.ts deleted file mode 100644 index 574f560..0000000 --- a/src/runtime/update-check.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./updates/npm-update-check"; diff --git a/src/runtime/updates/npm-update-check.ts b/src/runtime/updates/npm-update-check.ts deleted file mode 100644 index e2a5989..0000000 --- a/src/runtime/updates/npm-update-check.ts +++ /dev/null @@ -1,273 +0,0 @@ -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { dirname } from "node:path"; -import { defineService, defineSingleton } from "../../define"; -import { RigPathsClass, type PathOptions } from "../../config/paths"; - -export type UpdateCheckNotice = { - packageName: string; - currentVersion: string; - latestVersion: string; - message: string; -}; - -type UpdateCheckCache = { - checkedAt: number; - latestVersion?: string; -}; - -type FetchLike = ( - url: string, - init?: { signal?: AbortSignal }, -) => Promise<{ - ok: boolean; - json(): Promise; -}>; - -export type UpdateCheckOptions = PathOptions & { - cacheTtlMs?: number; - timeoutMs?: number; - now?: () => number; - fetch?: FetchLike; - packageName?: string; -}; - -function versionParts(params: { version: string }): number[] { - return params.version - .replace(/^v/, "") - .split(/[.-]/) - .map(function parseVersionPart(part) { - return Number.parseInt(part, 10); - }) - .filter(function keepFinitePart(part) { - return Number.isFinite(part); - }); -} - -export const VersionComparatorSingleton = defineSingleton({ - params: {}, - deps: {}, - isNewer(params: { candidate: string; current: string }): boolean { - function compare(compareParams: { left: string; right: string }): number { - const leftParts = versionParts({ version: compareParams.left }); - const rightParts = versionParts({ version: compareParams.right }); - const length = Math.max(leftParts.length, rightParts.length); - - for (let index = 0; index < length; index++) { - const leftPart = leftParts[index] ?? 0; - const rightPart = rightParts[index] ?? 0; - if (leftPart > rightPart) return 1; - if (leftPart < rightPart) return -1; - } - - return 0; - } - - return compare({ left: params.candidate, right: params.current }) > 0; - }, -}); - -type NpmUpdateCheckConfig = { - updateCheckCachePath: string; - cacheTtlMs: number; - timeoutMs: number; - packageName: string; -}; - -type NpmUpdateCheckDeps = { - env: NodeJS.ProcessEnv; - now: () => number; - fetch: FetchLike; - mkdir: typeof mkdir; - dirname: typeof dirname; - readJson: (path: string) => Promise; - writeText: (path: string, content: string) => Promise; - createAbortController: () => AbortController; - setTimer: (callback: () => void, delay: number) => ReturnType; - clearTimer: (timer: ReturnType) => void; -}; - -const NpmUpdateCheckProductionDeps: NpmUpdateCheckDeps = { - env: process.env, - now: Date.now, - fetch, - mkdir, - dirname, - async readJson(path) { - /* v8 ignore next 3 */ - return typeof Bun !== "undefined" - ? await Bun.file(path).json() - : JSON.parse(await readFile(path, "utf8")); - }, - async writeText(path, content) { - /* v8 ignore next 2 */ - if (typeof Bun !== "undefined") await Bun.write(path, content); - else await writeFile(path, content, "utf8"); - }, - createAbortController() { - return new AbortController(); - }, - setTimer(callback, delay) { - return setTimeout(callback, delay); - }, - clearTimer(timer) { - clearTimeout(timer); - }, -}; - -const NpmUpdateCheckProductionConfig: NpmUpdateCheckConfig = { - updateCheckCachePath: new RigPathsClass().updateCheckCachePath, - cacheTtlMs: 24 * 60 * 60 * 1000, - timeoutMs: 750, - packageName: "@rendotdev/rig", -}; - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -export class NpmUpdateCheckService extends defineService({ - params: NpmUpdateCheckProductionConfig, - deps: NpmUpdateCheckProductionDeps, -}) { - private notice(params: { - currentVersion: string; - latestVersion: string | undefined; - }): UpdateCheckNotice | undefined { - if ( - !params.latestVersion || - !VersionComparatorSingleton.isNewer({ - candidate: params.latestVersion, - current: params.currentVersion, - }) - ) { - return undefined; - } - return { - packageName: this.params.packageName, - currentVersion: params.currentVersion, - latestVersion: params.latestVersion, - message: `Rig update available: ${this.params.packageName} ${params.currentVersion} -> ${params.latestVersion}. Run npm install -g ${this.params.packageName}.`, - }; - } - - private isFresh(params: { cache: UpdateCheckCache }): boolean { - return this.deps.now() - params.cache.checkedAt < this.params.cacheTtlMs; - } - - private registryUrl(_params: {}): string { - return `https://registry.npmjs.org/${encodeURIComponent(this.params.packageName)}/latest`; - } - - private async fetchLatestVersion(_params: {}): Promise { - const controller = this.deps.createAbortController(); - const timer = this.deps.setTimer(function abortRequest() { - controller.abort(); - }, this.params.timeoutMs); - - try { - const response = await this.deps.fetch(this.registryUrl({}), { - signal: controller.signal, - }); - if (!response.ok) return undefined; - const data = await response.json(); - if (!isRecord(data) || typeof data.version !== "string") return undefined; - return data.version; - } catch { - return undefined; - } finally { - this.deps.clearTimer(timer); - } - } - - private async readCache(_params: {}): Promise { - try { - const data = await this.deps.readJson(this.params.updateCheckCachePath); - if (!isRecord(data) || typeof data.checkedAt !== "number") return undefined; - return { - checkedAt: data.checkedAt, - latestVersion: typeof data.latestVersion === "string" ? data.latestVersion : undefined, - }; - } catch { - return undefined; - } - } - - private async writeCache(params: { cache: UpdateCheckCache }): Promise { - await this.deps.mkdir(this.deps.dirname(this.params.updateCheckCachePath), { - recursive: true, - }); - await this.deps.writeText( - this.params.updateCheckCachePath, - `${JSON.stringify(params.cache, null, 2)}\n`, - ); - } - - public async check(params: { currentVersion: string }): Promise { - if (this.deps.env.RIG_UPDATE_CHECK === "0") return undefined; - - const cached = await this.readCache({}); - if (cached && this.isFresh({ cache: cached })) { - return this.notice({ - currentVersion: params.currentVersion, - latestVersion: cached.latestVersion, - }); - } - - const latestVersion = await this.fetchLatestVersion({}); - if (!latestVersion) return undefined; - - await this.writeCache({ cache: { checkedAt: this.deps.now(), latestVersion } }); - return this.notice({ currentVersion: params.currentVersion, latestVersion }); - } -} - -export const NpmUpdateCheck = new NpmUpdateCheckService(); - -export type NpmUpdateCheckServiceClass = { - check(currentVersion: string): Promise; -}; - -type NpmUpdateCheckServiceConstructor = { - new (options?: UpdateCheckOptions): NpmUpdateCheckServiceClass; - readonly prototype: NpmUpdateCheckServiceClass; -}; - -type NpmUpdateCheckServiceAdapter = NpmUpdateCheckServiceClass & { - readonly resource: NpmUpdateCheckService; -}; - -const NpmUpdateCheckServiceClassAdapter = function constructNpmUpdateCheckService( - this: NpmUpdateCheckServiceAdapter, - options: UpdateCheckOptions = {}, -): void { - const paths = new RigPathsClass(options); - Object.defineProperty(this, "resource", { - value: new NpmUpdateCheckService({ - params: { - updateCheckCachePath: paths.updateCheckCachePath, - cacheTtlMs: options.cacheTtlMs ?? NpmUpdateCheckProductionConfig.cacheTtlMs, - timeoutMs: options.timeoutMs ?? NpmUpdateCheckProductionConfig.timeoutMs, - packageName: options.packageName ?? NpmUpdateCheckProductionConfig.packageName, - }, - deps: { - ...NpmUpdateCheckProductionDeps, - env: process.env, - now: options.now ?? NpmUpdateCheckProductionDeps.now, - fetch: options.fetch ?? NpmUpdateCheckProductionDeps.fetch, - }, - }), - }); -}; -Object.defineProperty(NpmUpdateCheckServiceClassAdapter, "name", { - value: "NpmUpdateCheckServiceClass", -}); -Object.defineProperty(NpmUpdateCheckServiceClassAdapter.prototype, "check", { - configurable: true, - value: function check(this: NpmUpdateCheckServiceAdapter, currentVersion: string) { - return this.resource.check({ currentVersion }); - }, - writable: true, -}); - -export const NpmUpdateCheckServiceClass = - NpmUpdateCheckServiceClassAdapter as unknown as NpmUpdateCheckServiceConstructor; diff --git a/src/runtime/updates/rig-updater.ts b/src/runtime/updates/rig-updater.ts deleted file mode 100644 index c4ee48e..0000000 --- a/src/runtime/updates/rig-updater.ts +++ /dev/null @@ -1,490 +0,0 @@ -import { spawn } from "node:child_process"; -import { existsSync } from "node:fs"; -import { basename, dirname, join, resolve } from "node:path"; -import { defineService } from "../../define"; - -export type RigUpdateStep = { - command: string; - args: string[]; - env?: NodeJS.ProcessEnv; -}; - -export type RigUpdatePlan = - | { - status: "ready"; - currentVersion: string; - latestVersion: string; - updateStep: RigUpdateStep; - versionStep: RigUpdateStep; - syncStep: RigUpdateStep; - } - | { status: "current"; version: string; syncStep: RigUpdateStep } - | { status: "skipped"; reason: string }; - -export type RigUpdateResult = - | { - status: "updated"; - previousVersion: string; - version: string; - step: RigUpdateStep; - output: string; - } - | { status: "current"; version: string } - | { status: "skipped"; reason: string }; - -type RunnableRigUpdatePlan = Exclude; - -type RigUpdateCommandRunnerDeps = { - spawn: typeof spawn; - env: NodeJS.ProcessEnv; -}; - -const RigUpdateCommandRunnerProductionDeps: RigUpdateCommandRunnerDeps = { - spawn, - env: process.env, -}; - -export class RigUpdateCommandRunnerService extends defineService({ - params: {}, - deps: RigUpdateCommandRunnerProductionDeps, -}) { - private async execute(params: RigUpdateStep): Promise { - return await new Promise((resolvePromise, reject) => { - const child = this.deps.spawn(params.command, params.args, { - stdio: ["ignore", "pipe", "pipe"], - env: { ...this.deps.env, ...params.env }, - }); - const output: Buffer[] = []; - child.stdout.on("data", function captureStdout(chunk: Buffer | string) { - output.push(Buffer.from(chunk)); - }); - child.stderr.on("data", function captureStderr(chunk: Buffer | string) { - output.push(Buffer.from(chunk)); - }); - child.once("error", reject); - child.once("exit", function handleExit(code, signal) { - if (code === 0) { - resolvePromise(Buffer.concat(output).toString("utf8")); - return; - } - const detail = Buffer.concat(output).toString("utf8").trim(); - reject( - new Error( - `${params.command} exited with ${signal ?? `code ${code ?? "unknown"}`}.${detail ? `\n${detail}` : ""}`, - ), - ); - }); - }); - } - - public async run(params: RigUpdateStep): Promise { - return await this.execute(params); - } - - public async read(params: RigUpdateStep): Promise { - return await this.execute(params); - } -} - -export const RigUpdateCommandRunner = new RigUpdateCommandRunnerService(); - -export type RigUpdateCommandRunnerClass = { - run(params: RigUpdateStep): Promise; - read(params: RigUpdateStep): Promise; -}; - -type RigUpdateCommandRunnerConstructor = { - new (params: {}, deps: RigUpdateCommandRunnerDeps): RigUpdateCommandRunnerClass; - readonly prototype: RigUpdateCommandRunnerClass; -}; - -type RigUpdateCommandRunnerAdapter = RigUpdateCommandRunnerClass & { - readonly resource: RigUpdateCommandRunnerService; -}; - -const RigUpdateCommandRunnerClassAdapter = function constructRigUpdateCommandRunner( - this: RigUpdateCommandRunnerAdapter, - _params: {}, - deps: RigUpdateCommandRunnerDeps, -): void { - Object.defineProperty(this, "resource", { - value: new RigUpdateCommandRunnerService({ params: {}, deps }), - }); -}; -Object.defineProperty(RigUpdateCommandRunnerClassAdapter, "name", { - value: "RigUpdateCommandRunnerClass", -}); -Object.defineProperties(RigUpdateCommandRunnerClassAdapter.prototype, { - run: { - configurable: true, - value: function run(this: RigUpdateCommandRunnerAdapter, params: RigUpdateStep) { - return this.resource.run(params); - }, - writable: true, - }, - read: { - configurable: true, - value: function read(this: RigUpdateCommandRunnerAdapter, params: RigUpdateStep) { - return this.resource.read(params); - }, - writable: true, - }, -}); - -export const RigUpdateCommandRunnerClass = - RigUpdateCommandRunnerClassAdapter as unknown as RigUpdateCommandRunnerConstructor; - -type RigUpdaterDeps = { - executableExists: (path: string) => boolean; - readCommand: (step: RigUpdateStep) => Promise; - runCommand: (step: RigUpdateStep) => Promise; - basename: typeof basename; - dirname: typeof dirname; - join: typeof join; - resolve: typeof resolve; -}; - -const RigUpdaterProductionDeps: RigUpdaterDeps = { - executableExists: existsSync, - /* v8 ignore start -- these default delegates would query and mutate the real global package manager; injected runner behavior is covered below. */ - readCommand(params) { - return RigUpdateCommandRunner.read(params); - }, - runCommand(params) { - return RigUpdateCommandRunner.run(params); - }, - /* v8 ignore stop */ - basename, - dirname, - join, - resolve, -}; - -type RigInstallation = - | { kind: "npm" | "bun"; packageManager: string; prefix: string; rig: string } - | { reason: string }; - -function resolveInstallation(params: { - packageRoot: string; - basename: typeof basename; - dirname: typeof dirname; - join: typeof join; -}): RigInstallation { - const scopeDirectory = params.dirname(params.packageRoot); - const nodeModulesDirectory = params.dirname(scopeDirectory); - if ( - params.basename(params.packageRoot) !== "rig" || - params.basename(scopeDirectory) !== "@rendotdev" || - params.basename(nodeModulesDirectory) !== "node_modules" - ) { - return { reason: "Rig is not running from a supported global npm or Bun installation." }; - } - - const parentDirectory = params.dirname(nodeModulesDirectory); - if (params.basename(parentDirectory) === "lib") { - const prefix = params.dirname(parentDirectory); - return { - kind: "npm", - packageManager: params.join(prefix, "bin", "npm"), - prefix, - rig: params.join(prefix, "bin", "rig"), - }; - } - - const installDirectory = params.dirname(parentDirectory); - const bunDirectory = params.dirname(installDirectory); - if ( - params.basename(parentDirectory) === "global" && - params.basename(installDirectory) === "install" && - params.basename(bunDirectory) === ".bun" - ) { - return { - kind: "bun", - packageManager: params.join(bunDirectory, "bin", "bun"), - prefix: bunDirectory, - rig: params.join(bunDirectory, "bin", "rig"), - }; - } - - return { reason: "Rig is not running from a supported global npm or Bun installation." }; -} - -function parseLatestVersion(params: { output: string }): string { - let value: unknown; - try { - value = JSON.parse(params.output); - } catch { - value = params.output.trim(); - } - if (typeof value !== "string" || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value.trim())) { - throw new Error("The package manager returned an invalid latest Rig version."); - } - return value.trim(); -} - -export class RigUpdaterService extends defineService({ - params: { packageRoot: "", currentVersion: "" }, - deps: RigUpdaterProductionDeps, -}) { - private readonly packageRoot = this.deps.resolve(this.params.packageRoot); - - public getCurrentVersion(_params: {}): string { - return this.params.currentVersion; - } - - public async plan(_params: {}): Promise { - const installation = resolveInstallation({ - packageRoot: this.packageRoot, - basename: this.deps.basename, - dirname: this.deps.dirname, - join: this.deps.join, - }); - if ("reason" in installation) return { status: "skipped", reason: installation.reason }; - if (!this.deps.executableExists(installation.packageManager)) { - return { - status: "skipped", - reason: `The package manager for this Rig installation was not found at ${installation.packageManager}.`, - }; - } - if (!this.deps.executableExists(installation.rig)) { - return { - status: "skipped", - reason: `The Rig executable for this installation was not found at ${installation.rig}.`, - }; - } - - const latestVersion = parseLatestVersion({ - output: await this.deps.readCommand({ - command: installation.packageManager, - args: - installation.kind === "npm" - ? ["view", "@rendotdev/rig@latest", "version", "--json"] - : ["pm", "view", "@rendotdev/rig@latest", "version"], - }), - }); - const syncStep = { - command: installation.rig, - args: ["init"], - env: { RIG_UPDATE_CHECK: "0" }, - }; - if (latestVersion === this.params.currentVersion) { - return { status: "current", version: this.params.currentVersion, syncStep }; - } - - return { - status: "ready", - currentVersion: this.params.currentVersion, - latestVersion, - updateStep: { - command: installation.packageManager, - args: - installation.kind === "npm" - ? [ - "install", - "--global", - "--prefix", - installation.prefix, - `@rendotdev/rig@${latestVersion}`, - ] - : ["install", "--global", `@rendotdev/rig@${latestVersion}`], - }, - versionStep: { command: installation.rig, args: ["--version"] }, - syncStep, - }; - } - - public async update(params: { plan?: RigUpdatePlan }): Promise { - const updatePlan = params.plan ?? (await this.plan({})); - switch (updatePlan.status) { - case "current": - return { status: "current", version: updatePlan.version }; - case "skipped": - return updatePlan; - case "ready": - break; - } - const output = await this.deps.runCommand(updatePlan.updateStep); - const version = (await this.deps.readCommand(updatePlan.versionStep)).trim(); - if (version !== updatePlan.latestVersion) { - throw new Error( - `Rig reported version ${version || "unknown"} after updating to ${updatePlan.latestVersion}.`, - ); - } - return { - status: "updated", - previousVersion: updatePlan.currentVersion, - version, - step: updatePlan.updateStep, - output, - }; - } - - public async sync(params: { plan: RunnableRigUpdatePlan }): Promise<{ - step: RigUpdateStep; - output: string; - }> { - return { - step: params.plan.syncStep, - output: await this.deps.runCommand(params.plan.syncStep), - }; - } -} - -export type RigUpdaterClass = { - getCurrentVersion(): string; - plan(): Promise; - update(params: { plan?: RigUpdatePlan }): Promise; - sync(params: { plan: RunnableRigUpdatePlan }): Promise<{ step: RigUpdateStep; output: string }>; -}; - -type RigUpdaterConstructorDeps = Pick< - RigUpdaterDeps, - "executableExists" | "readCommand" | "runCommand" ->; - -type RigUpdaterConstructor = { - new ( - params: { packageRoot: string; currentVersion: string }, - deps: RigUpdaterConstructorDeps, - ): RigUpdaterClass; - readonly prototype: RigUpdaterClass; -}; - -type RigUpdaterAdapter = RigUpdaterClass & { - readonly resource: RigUpdaterService; -}; - -function buildRigUpdater(params: { - params: { packageRoot: string; currentVersion: string }; - deps: RigUpdaterConstructorDeps; -}): RigUpdaterService { - return new RigUpdaterService({ - params: params.params, - deps: { - ...params.deps, - basename, - dirname, - join, - resolve, - }, - }); -} - -const RigUpdaterClassAdapter = function constructRigUpdater( - this: RigUpdaterAdapter, - params: { packageRoot: string; currentVersion: string }, - deps: RigUpdaterConstructorDeps, -): void { - Object.defineProperty(this, "resource", { - value: buildRigUpdater({ params, deps }), - }); -}; -Object.defineProperty(RigUpdaterClassAdapter, "name", { value: "RigUpdaterClass" }); -Object.defineProperties(RigUpdaterClassAdapter.prototype, { - getCurrentVersion: { - configurable: true, - value: function getCurrentVersion(this: RigUpdaterAdapter) { - return this.resource.getCurrentVersion({}); - }, - writable: true, - }, - plan: { - configurable: true, - value: function plan(this: RigUpdaterAdapter) { - return this.resource.plan({}); - }, - writable: true, - }, - update: { - configurable: true, - value: function update(this: RigUpdaterAdapter, params: { plan?: RigUpdatePlan }) { - return this.resource.update(params); - }, - writable: true, - }, - sync: { - configurable: true, - value: function sync(this: RigUpdaterAdapter, params: { plan: RunnableRigUpdatePlan }) { - return this.resource.sync(params); - }, - writable: true, - }, -}); - -export const RigUpdaterClass = RigUpdaterClassAdapter as unknown as RigUpdaterConstructor; - -type RigUpdaterFactoryDeps = { - spawn: typeof spawn; - env: NodeJS.ProcessEnv; - executableExists: (path: string) => boolean; -}; - -const RigUpdaterFactoryProductionDeps: RigUpdaterFactoryDeps = { - spawn, - env: process.env, - executableExists: existsSync, -}; - -export class RigUpdaterFactoryService extends defineService({ - params: {}, - deps: RigUpdaterFactoryProductionDeps, -}) { - public create(params: { packageRoot: string; currentVersion: string }): RigUpdaterService { - const runner = new RigUpdateCommandRunnerService({ - params: {}, - deps: { spawn: this.deps.spawn, env: this.deps.env }, - }); - return buildRigUpdater({ - params, - deps: { - executableExists: this.deps.executableExists, - readCommand: runner.read.bind(runner), - runCommand: runner.run.bind(runner), - }, - }); - } -} - -export type RigUpdaterFactoryClass = { - create(params: { packageRoot: string; currentVersion: string }): RigUpdaterClass; -}; - -type RigUpdaterFactoryConstructor = { - new (params: {}, deps: RigUpdaterFactoryDeps): RigUpdaterFactoryClass; - readonly prototype: RigUpdaterFactoryClass; -}; - -type RigUpdaterFactoryAdapter = RigUpdaterFactoryClass & { - readonly resource: RigUpdaterFactoryService; -}; - -function adaptRigUpdaterResource(resource: RigUpdaterService): RigUpdaterClass { - const adapter = Object.create(RigUpdaterClassAdapter.prototype) as RigUpdaterAdapter; - Object.defineProperty(adapter, "resource", { value: resource }); - return adapter; -} - -const RigUpdaterFactoryClassAdapter = function constructRigUpdaterFactory( - this: RigUpdaterFactoryAdapter, - _params: {}, - deps: RigUpdaterFactoryDeps, -): void { - Object.defineProperty(this, "resource", { - value: new RigUpdaterFactoryService({ params: {}, deps }), - }); -}; -Object.defineProperty(RigUpdaterFactoryClassAdapter, "name", { value: "RigUpdaterFactoryClass" }); -Object.defineProperty(RigUpdaterFactoryClassAdapter.prototype, "create", { - configurable: true, - value: function create( - this: RigUpdaterFactoryAdapter, - params: { packageRoot: string; currentVersion: string }, - ): RigUpdaterClass { - return adaptRigUpdaterResource(this.resource.create(params)); - }, - writable: true, -}); - -export const RigUpdaterFactoryClass = - RigUpdaterFactoryClassAdapter as unknown as RigUpdaterFactoryConstructor; -export const RigUpdaterFactory = new RigUpdaterFactoryClass({}, RigUpdaterFactoryProductionDeps); diff --git a/src/tooling/architecture/architecture.test.ts b/src/tooling/architecture/architecture.test.ts index 44715ca..4574ece 100644 --- a/src/tooling/architecture/architecture.test.ts +++ b/src/tooling/architecture/architecture.test.ts @@ -1,7 +1,9 @@ -import { access, readFile, readdir } from "node:fs/promises"; +import { readFile, readdir } from "node:fs/promises"; import { join, resolve } from "node:path"; import { describe, expect, it } from "vite-plus/test"; +import ts from "typescript"; import { + allowedDomainDependencies, allowedLayerDependencies, classifySourcePath, type DomainName, @@ -11,7 +13,6 @@ import { findDomainDependencyCycle, findLayerDependencyCycle, functionLineLimitForPath, - migratedDomainNames, resolveImportTarget, validateArchitectureModel, validateDependency, @@ -23,15 +24,35 @@ const sourceRoot = resolve(import.meta.dirname, "../.."); describe("architecture model", () => { it("defines an acyclic dependency graph", () => { expect(validateArchitectureModel()).toEqual([]); + expect(allowedLayerDependencies).toEqual({ + types: ["types"], + config: ["types", "config"], + repo: ["types", "config", "repo"], + service: ["types", "config", "repo", "service"], + runtime: ["types", "config", "service", "runtime"], + ui: ["types", "config", "service", "runtime", "ui"], + }); + expect(allowedDomainDependencies).toEqual({ + collections: [], + registry: ["settings"], + scheduling: ["settings", "tools"], + settings: [], + tools: ["collections", "registry", "settings"], + updates: ["settings"], + }); }); - it("ratchets known size debt without weakening the default limits", () => { + it("keeps production and test limits strict without migration overrides", () => { expect( - fileLineLimitForPath("/repo/src/collections/application/tool-collection.ts", false), - ).toBe(962); - expect(functionLineLimitForPath("/repo/src/cron/application/rig-cron.ts")).toBe(360); - expect(fileLineLimitForPath("/repo/src/domains/tools/service/new.ts", false)).toBe(400); - expect(functionLineLimitForPath("/repo/src/domains/tools/service/new.ts")).toBe(80); + fileLineLimitForPath("/repo/src/domains/collections/types/tool-collection.ts", false), + ).toBe(400); + expect(functionLineLimitForPath("/repo/src/domains/scheduling/runtime/rig-cron.ts")).toBe(80); + expect(fileLineLimitForPath("/repo/src/domains/tools/runtime/tool-runner.test.ts", true)).toBe( + 600, + ); + expect(functionLineLimitForPath("/repo/src/domains/tools/runtime/tool-runner.test.ts")).toBe( + 80, + ); }); it("detects a dependency cycle", () => { @@ -44,16 +65,6 @@ describe("architecture model", () => { expect(new Set(domainNames).size).toBe(domainNames.length); }); - it("requires migrated domains to expose a public API", async () => { - await Promise.all( - migratedDomainNames.map(async (domain) => { - await expect( - access(join(sourceRoot, "domains", domain, "index.ts")), - ).resolves.toBeUndefined(); - }), - ); - }); - it("keeps the repository cross-domain graph acyclic", async () => { const graph = await collectDomainDependencyGraph(); @@ -78,23 +89,27 @@ describe("architecture model", () => { `${root}/domains/tools/${layer}/source.ts`, `${root}/providers/filesystem/filesystem.ts`, ); - expect(decision.allowed).toBe(["repo", "service", "runtime", "ui"].includes(layer)); + expect(decision.allowed).toBe(["repo", "service", "runtime"].includes(layer)); } }); }); describe("dependency boundaries", () => { - it("requires app and cross-domain imports to use public APIs", () => { + it("lets app wiring import concrete domain modules", () => { expect( validateDependency(`${root}/app/cli/cli.ts`, `${root}/domains/tools/service/run-tool.ts`) .allowed, - ).toBe(false); - expect( - validateDependency(`${root}/app/cli/cli.ts`, `${root}/domains/tools/index.ts`).allowed, ).toBe(true); expect( - validateDependency(`${root}/app/cli/main.tsx`, `${root}/domains/tools/ui/index.ts`).allowed, + validateDependency(`${root}/app/cli/main.tsx`, `${root}/domains/tools/ui/tool-ui.ts`).allowed, ).toBe(true); + expect( + validateDependency(`${root}/app/cli/cli.ts`, `${root}/domains/tools/repo/tool-store.ts`) + .allowed, + ).toBe(false); + }); + + it("allows only declared cross-domain contracts at forward layers", () => { expect( validateDependency( `${root}/domains/tools/service/run-tool.ts`, @@ -104,7 +119,27 @@ describe("dependency boundaries", () => { expect( validateDependency( `${root}/domains/tools/service/run-tool.ts`, - `${root}/domains/settings/index.ts`, + `${root}/domains/settings/service/settings.ts`, + ).allowed, + ).toBe(true); + for (const layer of ["types", "config", "repo"] as const) { + expect( + validateDependency( + `${root}/domains/tools/${layer}/source.ts`, + `${root}/domains/settings/service/settings.ts`, + ).allowed, + ).toBe(false); + } + expect( + validateDependency( + `${root}/domains/scheduling/service/schedule.ts`, + `${root}/domains/collections/service/collections.ts`, + ).allowed, + ).toBe(false); + expect( + validateDependency( + `${root}/domains/scheduling/runtime/scheduler.ts`, + `${root}/domains/tools/runtime/tool-runner.ts`, ).allowed, ).toBe(true); }); @@ -113,7 +148,7 @@ describe("dependency boundaries", () => { expect( validateDependency( `${root}/providers/filesystem/filesystem.ts`, - `${root}/domains/tools/index.ts`, + `${root}/domains/tools/service/tool-service.ts`, ).allowed, ).toBe(false); expect( @@ -122,6 +157,12 @@ describe("dependency boundaries", () => { expect( validateDependency(`${root}/domains/tools/ui/tool.tsx`, `${root}/app/cli/main.tsx`).allowed, ).toBe(false); + expect( + validateDependency( + `${root}/domains/tools/ui/tool.tsx`, + `${root}/providers/filesystem/filesystem.ts`, + ).allowed, + ).toBe(false); expect( validateDependency( `${root}/domains/tools/service/run-tool.ts`, @@ -143,31 +184,35 @@ describe("source classification", () => { }); }); - it("recognizes domain and layer public entrypoints", () => { + it("rejects index entrypoints because barrel files are forbidden", () => { expect(classifySourcePath(`${root}/domains/tools/index.ts`)).toEqual({ - kind: "domain-public", - domain: "tools", + kind: "invalid", + reason: "source-location", }); expect(classifySourcePath(`${root}/domains/tools/ui/index.ts`)).toEqual({ - kind: "domain-public", - domain: "tools", + kind: "invalid", + reason: "source-location", }); }); - it("recognizes current legacy roots while rejecting new ad hoc locations", () => { - for (const path of ["tools/run.ts", "runtime/process/shell.ts", "cli.ts"]) { - expect(classifySourcePath(`${root}/${path}`)).toEqual({ kind: "legacy" }); + it("rejects legacy roots and new ad hoc locations", () => { + for (const path of [ + "tools/run.ts", + "runtime/process/shell.ts", + "cli.ts", + "helpers/helper.ts", + ]) { + expect(classifySourcePath(`${root}/${path}`)).toEqual({ + kind: "invalid", + reason: "source-location", + }); } - expect(classifySourcePath(`${root}/helpers/helper.ts`)).toEqual({ - kind: "invalid", - reason: "source-location", - }); }); it("resolves local imports without treating packages as source dependencies", () => { - expect(resolveImportTarget(`${root}/app/cli/cli.ts`, "../../domains/tools/index.ts")).toBe( - `${root}/domains/tools/index.ts`, - ); + expect( + resolveImportTarget(`${root}/app/cli/cli.ts`, "../../domains/tools/service/tool.ts"), + ).toBe(`${root}/domains/tools/service/tool.ts`); expect(resolveImportTarget(`${root}/app/cli/cli.ts`, "react")).toBeUndefined(); }); }); @@ -184,21 +229,20 @@ async function collectDomainDependencyGraph() { } const sourceFile = join(sourceRoot, relativeFile); const source = classifySourcePath(sourceFile); - const isDomainSource = source.kind === "domain-layer" || source.kind === "domain-public"; + const isDomainSource = source.kind === "domain-layer"; if (!isDomainSource) { continue; } // eslint-disable-next-line no-await-in-loop const sourceText = await readFile(sourceFile, "utf8"); - for (const match of sourceText.matchAll(/(?:from\s*|import\s*\()\s*["']([^"']+)["']/gu)) { - const targetFile = resolveImportTarget(sourceFile, match[1] ?? ""); + for (const specifier of importSpecifiers(relativeFile, sourceText)) { + const targetFile = resolveImportTarget(sourceFile, specifier); if (!targetFile) { continue; } const target = classifySourcePath(targetFile); - const isCrossDomainPublicImport = - target.kind === "domain-public" && target.domain !== source.domain; - if (isCrossDomainPublicImport) { + const isCrossDomainImport = target.kind === "domain-layer" && target.domain !== source.domain; + if (isCrossDomainImport) { (graph[source.domain] ??= new Set()).add(target.domain); } } @@ -207,3 +251,34 @@ async function collectDomainDependencyGraph() { Object.entries(graph).map(([domain, dependencies]) => [domain, [...dependencies]]), ); } + +function importSpecifiers(path: string, source: string) { + const sourceFile = ts.createSourceFile( + path, + source, + ts.ScriptTarget.Latest, + true, + path.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS, + ); + const specifiers: string[] = []; + const visit = (node: ts.Node) => { + const isStaticModuleDependency = + (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && + node.moduleSpecifier !== undefined && + ts.isStringLiteral(node.moduleSpecifier); + if (isStaticModuleDependency) { + specifiers.push(node.moduleSpecifier.text); + } + const isDynamicImport = + ts.isCallExpression(node) && + node.expression.kind === ts.SyntaxKind.ImportKeyword && + node.arguments.length === 1 && + ts.isStringLiteral(node.arguments[0]); + if (isDynamicImport) { + specifiers.push((node.arguments[0] as ts.StringLiteral).text); + } + ts.forEachChild(node, visit); + }; + visit(sourceFile); + return specifiers; +} diff --git a/src/tooling/architecture/architecture.ts b/src/tooling/architecture/architecture.ts index b19f681..b061a6e 100644 --- a/src/tooling/architecture/architecture.ts +++ b/src/tooling/architecture/architecture.ts @@ -8,89 +8,31 @@ export const domainNames = [ "tools", "updates", ] as const; -export const migratedDomainNames = ["settings"] as const; export const domainLayers = ["types", "config", "repo", "service", "runtime", "ui"] as const; -export const productionFileLineLimit = 400; -export const testFileLineLimit = 600; -export const functionLineLimit = 80; - -const migrationFileLineLimits: Readonly> = { - "src/application/cli/cli-application.ts": 664, - "src/collections/application/tool-collection.ts": 962, - "src/cron/application/rig-cron.ts": 732, - "src/generated/agent/agent-instruction-sync.ts": 468, - "src/generated/runtime/runtime-support.ts": 539, - "src/persistence/cache/tool-cache.ts": 413, - "src/runtime/support.test.ts": 1_192, - "src/runtime/updates/rig-updater.ts": 449, - "src/tools/collection.test.ts": 911, - "src/tools/cron.test.ts": 662, - "src/tools/loading/tool-loader.ts": 509, - "src/tools/presentation/tool-list.ts": 465, - "src/tools/run.test.ts": 1_424, -}; - -const migrationFunctionLineLimits: Readonly> = { - "src/application/cli/cli-application.ts": 420, - "src/collections/persistence/memory-index.ts": 123, - "src/collections/application/tool-collection.ts": 320, - "src/config/application/config-store.ts": 121, - "src/domains/settings/repo/file-lock.ts": 119, - "src/domains/settings/repo/rig-paths.ts": 109, - "src/domains/settings/service/directory-migration.ts": 130, - "src/cron/application/rig-cron.ts": 360, - "src/dev/dev-link.ts": 133, - "src/generated/agent/agent-instruction-sync.ts": 240, - "src/generated/runtime/runtime-support.ts": 280, - "src/persistence/cache/tool-cache.ts": 220, - "src/registry/application/discover-tools.ts": 170, - "src/registry/registry.test.ts": 133, - "src/runtime/logging/logger.ts": 91, - "src/runtime/process/shell.ts": 81, - "src/runtime/support.test.ts": 320, - "src/runtime/updates/npm-update-check.ts": 81, - "src/runtime/updates/rig-updater.ts": 260, - "src/tools/collection.test.ts": 300, - "src/tools/cron.test.ts": 260, - "src/tools/domain/tool-search.ts": 109, - "src/tools/env.test.ts": 102, - "src/tools/execution/tool-runner.ts": 90, - "src/tools/loading/tool-loader.ts": 240, - "src/tools/management/tool-env.ts": 102, - "src/tools/management/tool-typecheck.ts": 124, - "src/tools/presentation/tool-list.ts": 240, - "src/tools/run.test.ts": 320, -}; - -export const legacySourceRoots = [ - "agents", - "application", - "collections", - "config", - "cron", - "dev", - "errors", - "generated", - "persistence", - "registry", - "runtime", - "tools", -] as const; +const productionFileLineLimit = 400; +const testFileLineLimit = 600; +const functionLineLimit = 80; export function fileLineLimitForPath(filePath: string, isTest: boolean) { - return ( - migrationFileLineLimits[sourcePathKey(filePath)] ?? - (isTest ? testFileLineLimit : productionFileLineLimit) - ); + return isTest ? testFileLineLimit : productionFileLineLimit; } -export function functionLineLimitForPath(filePath: string) { - return migrationFunctionLineLimits[sourcePathKey(filePath)] ?? functionLineLimit; +export function functionLineLimitForPath(_filePath: string) { + return functionLineLimit; } export type DomainName = (typeof domainNames)[number]; export type DomainLayer = (typeof domainLayers)[number]; +export const allowedDomainDependencies: Readonly> = { + collections: [], + registry: ["settings"], + scheduling: ["settings", "tools"], + settings: [], + tools: ["collections", "registry", "settings"], + updates: ["settings"], +}; + export const allowedLayerDependencies: Readonly> = { types: ["types"], config: ["types", "config"], @@ -102,15 +44,14 @@ export const allowedLayerDependencies: Readonly - | Readonly<{ kind: "domain-public"; domain: DomainName }> | Readonly<{ kind: "domain-layer"; domain: DomainName; layer: DomainLayer }> | Readonly<{ kind: "invalid"; reason: "domain" | "domain-layer" | "source-location" }>; export type DependencyDecision = Readonly<{ allowed: boolean; - reason?: "app-internal-domain" | "cross-domain-internal" | "layer" | "top-level"; + reason?: "cross-domain" | "layer" | "top-level"; source: SourceClassification; target: SourceClassification; }>; @@ -120,16 +61,10 @@ export function classifySourcePath(filePath: string): SourceClassification { if (!sourcePath) { return { kind: "outside-source" }; } - const isDefinition = sourcePath === "define.ts" || sourcePath === "define.test.ts"; - if (isDefinition) { - return { kind: "definition" }; - } - const [topLevel, second, third, fourth] = sourcePath.split("/"); - const isLegacyRootFile = !sourcePath.includes("/"); - const usesLegacyLocation = isLegacyRootFile || isLegacySourceRoot(topLevel); - if (usesLegacyLocation) { - return { kind: "legacy" }; + if (/(?:^|\/)index\.tsx?$/u.test(sourcePath)) { + return { kind: "invalid", reason: "source-location" }; } + const [topLevel, second, third] = sourcePath.split("/"); const isProductionTopLevel = topLevel === "app" || topLevel === "providers" || topLevel === "utils"; if (isProductionTopLevel) { @@ -144,15 +79,6 @@ export function classifySourcePath(filePath: string): SourceClassification { if (!isDomainName(second)) { return { kind: "invalid", reason: "domain" }; } - const isDomainPublicApi = third === "index.ts" || third === "index.tsx"; - if (isDomainPublicApi) { - return { kind: "domain-public", domain: second }; - } - const isLayerPublicApi = - isDomainLayer(third) && (fourth === "index.ts" || fourth === "index.tsx"); - if (isLayerPublicApi) { - return { kind: "domain-public", domain: second }; - } if (!isDomainLayer(third)) { return { kind: "invalid", reason: "domain-layer" }; } @@ -178,32 +104,18 @@ export function validateDependency(sourceFile: string, targetFile: string): Depe if (hasInvalidLocation) { return { allowed: true, source, target }; } - if (source.kind === "legacy") { - return { allowed: true, source, target }; - } - if (target.kind === "legacy") { - return { allowed: false, reason: "top-level", source, target }; - } - if (target.kind === "definition") { - return { - allowed: source.kind !== "utils" && source.kind !== "tooling", - reason: "top-level", - source, - target, - }; - } - if (source.kind === "definition") { - return { allowed: target.kind === "providers", reason: "top-level", source, target }; - } if (source.kind === "app") { + const isAppDomainTarget = + target.kind === "domain-layer" && + ["types", "service", "runtime", "ui"].includes(target.layer); const allowed = target.kind === "app" || target.kind === "providers" || target.kind === "utils" || - target.kind === "domain-public"; + isAppDomainTarget; return { allowed, - reason: target.kind === "domain-layer" ? "app-internal-domain" : "top-level", + reason: "top-level", source, target, }; @@ -219,13 +131,6 @@ export function validateDependency(sourceFile: string, targetFile: string): Depe const allowed = target.kind === "tooling" || target.kind === "utils"; return { allowed, reason: "top-level", source, target }; } - if (source.kind === "domain-public") { - const isSameDomainLayer = target.kind === "domain-layer" && target.domain === source.domain; - const isSameDomainPublicApi = - target.kind === "domain-public" && target.domain === source.domain; - const allowed = isSameDomainLayer || isSameDomainPublicApi; - return { allowed, reason: "top-level", source, target }; - } if (source.kind !== "domain-layer") { return { allowed: false, reason: "top-level", source, target }; } @@ -254,20 +159,30 @@ export function validateArchitectureModel() { if (cycle) { errors.push(`Layer dependency cycle: ${cycle.join(" -> ")}.`); } + for (const domain of domainNames) { + for (const dependency of allowedDomainDependencies[domain]) { + if (!domainNames.includes(dependency)) { + errors.push(`${domain} references the unknown domain ${dependency}.`); + } + if (dependency === domain) { + errors.push(`${domain} must not declare itself as a domain dependency.`); + } + } + } + const domainCycle = findDomainDependencyCycle(allowedDomainDependencies); + if (domainCycle) { + errors.push(`Domain dependency cycle: ${domainCycle.join(" -> ")}.`); + } return errors; } function validateDomainLayerDependency( source: Extract, - target: Exclude, + target: Exclude, ): DependencyDecision { - if (target.kind === "domain-public") { - const allowed = target.domain !== source.domain; - return { allowed, reason: "cross-domain-internal", source, target }; - } const isCrossCuttingTarget = target.kind === "providers" || target.kind === "utils"; if (isCrossCuttingTarget) { - const allowsProviders = ["repo", "service", "runtime", "ui"].includes(source.layer); + const allowsProviders = ["repo", "service", "runtime"].includes(source.layer); return { allowed: target.kind === "utils" || allowsProviders, reason: "layer", @@ -279,12 +194,57 @@ function validateDomainLayerDependency( return { allowed: false, reason: "top-level", source, target }; } if (target.domain !== source.domain) { - return { allowed: false, reason: "cross-domain-internal", source, target }; + const allowsDomain = allowedDomainDependencies[source.domain].includes(target.domain); + const allowsTargetLayer = + target.layer === "types" || + (target.layer === "service" && ["service", "runtime", "ui"].includes(source.layer)) || + (target.layer === "runtime" && ["runtime", "ui"].includes(source.layer)); + return { + allowed: allowsDomain && allowsTargetLayer, + reason: "cross-domain", + source, + target, + }; } const allowed = allowedLayerDependencies[source.layer].includes(target.layer); return { allowed, reason: "layer", source, target }; } +export function describeSourceClassification(classification: SourceClassification) { + if (classification.kind === "domain-layer") { + return `${classification.domain}/${classification.layer}`; + } + if (classification.kind === "invalid") { + return `invalid ${classification.reason}`; + } + return classification.kind; +} + +export function dependencyRemediation(decision: DependencyDecision) { + const isDomainLayerDependency = + decision.source.kind === "domain-layer" && decision.target.kind === "domain-layer"; + if (isDomainLayerDependency) { + if (decision.source.domain !== decision.target.domain) { + const declared = allowedDomainDependencies[decision.source.domain]; + const domainList = declared.length > 0 ? declared.join(", ") : "no other domains"; + return `Route the dependency through an allowed types, service, or runtime contract at the same or an earlier layer. ${decision.source.domain} may depend on ${domainList}.`; + } + const layers = allowedLayerDependencies[decision.source.layer].join(", "); + return `Move the dependency behind ${decision.source.layer} or an earlier allowed layer. ${decision.source.layer} may import ${layers}.`; + } + const isDomainProviderDependency = + decision.source.kind === "domain-layer" && decision.target.kind === "providers"; + if (isDomainProviderDependency) { + return "Inject Providers through repo, service, or runtime. UI, config, and types must use domain contracts."; + } + const isAppDomainDependency = + decision.source.kind === "app" && decision.target.kind === "domain-layer"; + if (isAppDomainDependency) { + return "App code consumes domain types, service contracts, runtimes, and UI. Close config and repo dependencies inside exported domain Layers."; + } + return "Move the dependency to the owning domain layer or compose it in app wiring."; +} + export function findLayerDependencyCycle( graph: Readonly>, ) { @@ -370,19 +330,10 @@ function getSourceRelativePath(filePath: string) { return normalized.startsWith("src/") ? normalized.slice("src/".length) : undefined; } -function sourcePathKey(filePath: string) { - const relativePath = getSourceRelativePath(filePath); - return relativePath ? `src/${relativePath}` : normalizePath(filePath); -} - function isDomainName(value: string | undefined): value is DomainName { return domainNames.some((domain) => domain === value); } -function isLegacySourceRoot(value: string | undefined) { - return legacySourceRoots.some((root) => root === value); -} - function isDomainLayer(value: string | undefined): value is DomainLayer { return domainLayers.some((layer) => layer === value); } diff --git a/src/tooling/architecture/repository-architecture.test.ts b/src/tooling/architecture/repository-architecture.test.ts new file mode 100644 index 0000000..223bce9 --- /dev/null +++ b/src/tooling/architecture/repository-architecture.test.ts @@ -0,0 +1,452 @@ +import { describe, expect, it } from "vite-plus/test"; +import { readFile, readdir } from "node:fs/promises"; +import { resolve } from "node:path"; +import ts from "typescript"; +import { + classifySourcePath, + dependencyRemediation, + describeSourceClassification, + resolveImportTarget, + validateDependency, +} from "./architecture.ts"; + +class ArchitectureSourceSet { + async codeFiles(): Promise { + const files = await Promise.all( + ["src", "scripts", "test"].map(async (root) => + (await readdir(root, { recursive: true })) + .filter( + (path) => (path.endsWith(".ts") || path.endsWith(".tsx")) && !path.endsWith(".d.ts"), + ) + .map((path) => `${root}/${path}`), + ), + ); + return files.flat().toSorted(); + } + + async implementationFiles(): Promise { + return (await this.codeFiles()).filter( + (path) => + !path.startsWith("test/") && !path.endsWith(".test.ts") && !path.endsWith(".test.tsx"), + ); + } + + async effectImplementationFiles(): Promise { + return (await this.implementationFiles()).filter( + (path) => !path.startsWith("src/tooling/") && !path.includes("/fixtures/"), + ); + } + + async violations( + pattern: RegExp, + validate: (match: RegExpMatchArray) => boolean, + ): Promise { + const files = await this.implementationFiles(); + const violations = await Promise.all( + files.map(async (path) => { + const source = await readFile(path, "utf8"); + return [...source.matchAll(pattern)] + .filter((match) => !validate(match)) + .map((match) => `${path}:${this.line(source, match.index)}`); + }), + ); + return violations.flat(); + } + + async codeViolations( + pattern: RegExp, + validate: (match: RegExpMatchArray) => boolean, + ): Promise { + const files = (await this.codeFiles()).filter( + (path) => path !== "src/tooling/architecture/repository-architecture.test.ts", + ); + const violations = await Promise.all( + files.map(async (path) => { + const source = await readFile(path, "utf8"); + return [...source.matchAll(pattern)] + .filter((match) => !validate(match)) + .map((match) => `${path}:${this.line(source, match.index)}`); + }), + ); + return violations.flat(); + } + + private line(source: string, index: number | undefined): number { + return source.slice(0, index ?? 0).split("\n").length; + } +} + +describe("architecture", () => { + const sources = new ArchitectureSourceSet(); + + it("classifies every production module and validates every internal import edge", async () => { + const violations = await collectRepositoryArchitectureViolations( + await sources.implementationFiles(), + ); + expect(violations).toEqual([]); + }); + + it("forbids barrel files and re-export facades", async () => { + const files = await sources.codeFiles(); + expect(files.filter((path) => /(?:^|\/)index\.tsx?$/u.test(path))).toEqual([]); + expect( + await sources.codeViolations( + /^\s*export\s+(?:\*|\{[\s\S]*?\})\s+from\s+["'][^"']+["']/gmu, + () => false, + ), + ).toEqual([]); + }); + + it("forbids singleton construction vocabulary", async () => { + expect(await sources.codeViolations(/\b[A-Za-z_$][\w$]*Singleton\b/g, () => false)).toEqual([]); + expect( + await sources.codeViolations( + /^\s*export\s+const\s+[A-Za-z_$][\w$]*\s*=\s*new\s+/gmu, + () => false, + ), + ).toEqual([]); + }); + + it("forbids generic Class suffixes", async () => { + expect(await sources.codeViolations(/\bclass\s+[A-Za-z_$][\w$]*Class\b/g, () => false)).toEqual( + [], + ); + }); + + it("uses architectural suffixes for implementation class declarations", async () => { + const violations = await sources.violations( + /\bclass\s+([A-Za-z_$][\w$]*)([\s\S]*?)(?=\{)/g, + (match) => + /(?:Codec|Command|Config|Entrypoint|Error|File|Handle|Id|Index|Parser|Paths|Provider|Repo|Repository|Reporter|Resolver|Resource|Route|Runner|Runtime|Script|Service|Suite|Version)$/.test( + match[1]!, + ) || + /extends\s+(?:Context\.Service|Schema\.(?:Class|ErrorClass|TaggedClass|TaggedErrorClass))/.test( + match[0], + ), + ); + expect(violations).toEqual([]); + }); + + it("keeps behavior on instances instead of static methods", async () => { + const violations = await sources.violations(/\bstatic\s+[A-Za-z_$][\w$]*\s*[<(]/g, () => false); + expect(violations).toEqual([]); + }); + + it("keeps production behavior inside canonical Effect services", async () => { + const violations = ( + await Promise.all((await sources.effectImplementationFiles()).map(floatingBehaviorViolations)) + ).flat(); + expect(violations).toEqual([]); + }); + + it("gives every Context.Service its inline make and owned static layer", async () => { + const violations = ( + await Promise.all((await sources.effectImplementationFiles()).map(effectServiceViolations)) + ).flat(); + expect(violations).toEqual([]); + }); + + it("executes Effects only at approved process and compatibility boundaries", async () => { + const approved = new Set([ + "src/app/cli/composition-root.ts", + "src/app/cli/entrypoint.ts", + "src/app/cli/runtime-bootstrap.ts", + "src/domains/collections/repo/sqlite/sqlite-collection-index.ts", + "src/domains/collections/runtime/collection-handle-factory.ts", + "src/domains/tools/runtime/runtime-support.ts", + "src/domains/tools/runtime/tool-sdk.ts", + "scripts/bench.ts", + "scripts/release.ts", + "scripts/smoke.ts", + ]); + const paths = (await sources.effectImplementationFiles()).filter((path) => !approved.has(path)); + const violations = (await Promise.all(paths.map(runtimeBoundaryViolations))).flat(); + expect(violations).toEqual([]); + }); + + it("uses Effect services and layers at reliability boundaries", async () => { + const paths = [ + "src/app/cli/composition-root.ts", + "src/domains/collections/runtime/collection-handle-factory.ts", + "src/domains/scheduling/runtime/rig-cron.ts", + "src/domains/settings/service/config-store.ts", + "src/domains/tools/runtime/agent-instruction-sync.ts", + "src/domains/tools/runtime/tool-collections.ts", + "src/domains/tools/runtime/tool-loader.ts", + "src/domains/tools/runtime/tool-runner.ts", + "src/domains/updates/runtime/rig-updater.ts", + "src/providers/filesystem/file-lock.ts", + "src/providers/process/rig-shell-service.ts", + ]; + const sourcesByPath = await Promise.all( + paths.map(async (path) => ({ path, source: await readFile(path, "utf8") })), + ); + for (const { source } of sourcesByPath) { + expect(source).toContain("extends Context.Service"); + expect(source).toMatch(/(?:Layer\.|Layer<)/u); + } + + expect( + await sources.violations( + /\b(?:ClassAdapter|ConstructorAdapter|defaultConstruction)\b|\bas unknown as\s+[A-Za-z_$][\w$]*Constructor\b/g, + () => false, + ), + ).toEqual([]); + + const updateCheck = await readFile("src/domains/updates/service/npm-update-check.ts", "utf8"); + const updateTypes = await readFile("src/domains/updates/types/updates.ts", "utf8"); + expect(updateTypes).toContain("Schema.TaggedErrorClass"); + expect(updateCheck).toContain("Effect.retry"); + expect(updateCheck).toContain("Schedule.exponential"); + }); + + it("has no legacy define construction vocabulary", async () => { + const importingFiles = ( + await Promise.all( + ( + await sources.implementationFiles() + ).map(async (path) => ({ + path, + source: await readFile(path, "utf8"), + })), + ) + ) + .filter(({ source }) => /from\s+["'][^"']*define["']/.test(source)) + .map(({ path }) => path); + + expect(importingFiles).toEqual([]); + expect(await readdir("src")).not.toContain("define.ts"); + }); + + it("keeps vendored source as a read-only reference", async () => { + const violations = await sources.violations( + /(?:from\s+|import\s*)["']([^"']*repos\/effect[^"']*)["']/g, + () => false, + ); + expect(violations).toEqual([]); + }); +}); + +async function collectRepositoryArchitectureViolations(paths: readonly string[]) { + const sourcePaths = paths.filter((entry) => entry.startsWith("src/")); + const violationGroups = await Promise.all( + sourcePaths.map(async (path) => { + const violations: string[] = []; + const absolutePath = resolve(path); + const classification = classifySourcePath(absolutePath); + if (classification.kind === "invalid") { + return [ + `${path}: ${describeSourceClassification(classification)}. Move this module into the canonical architecture layout.`, + ]; + } + const source = await readFile(path, "utf8"); + const sourceFile = ts.createSourceFile( + path, + source, + ts.ScriptTarget.Latest, + true, + path.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS, + ); + for (const dependency of sourceDependencies(sourceFile)) { + const targetPath = resolveImportTarget(absolutePath, dependency.specifier); + if (!targetPath) continue; + const decision = validateDependency(absolutePath, targetPath); + if (decision.allowed) continue; + const line = sourceFile.getLineAndCharacterOfPosition( + dependency.node.getStart(sourceFile), + ).line; + violations.push( + `${path}:${line + 1} ${describeSourceClassification(decision.source)} -> ${describeSourceClassification(decision.target)}. ${dependencyRemediation(decision)}`, + ); + } + return violations; + }), + ); + return violationGroups.flat(); +} + +function sourceDependencies(sourceFile: ts.SourceFile) { + const dependencies: Array<{ specifier: string; node: ts.Node }> = []; + const visit = (node: ts.Node) => { + const isStaticModuleDependency = + (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && + node.moduleSpecifier !== undefined && + ts.isStringLiteral(node.moduleSpecifier); + if (isStaticModuleDependency) { + dependencies.push({ specifier: node.moduleSpecifier.text, node: node.moduleSpecifier }); + } + const isDynamicImport = + ts.isCallExpression(node) && + node.expression.kind === ts.SyntaxKind.ImportKeyword && + node.arguments.length === 1 && + ts.isStringLiteral(node.arguments[0]); + if (isDynamicImport) { + dependencies.push({ + specifier: (node.arguments[0] as ts.StringLiteral).text, + node: node.arguments[0] as ts.StringLiteral, + }); + } + ts.forEachChild(node, visit); + }; + visit(sourceFile); + return dependencies; +} + +function isAllowedTopLevelFunction(path: string, name: string) { + const isReactDeclaration = + path.endsWith(".tsx") && (/^[A-Z]/u.test(name) || /^use[A-Z]/u.test(name)); + if (isReactDeclaration) { + return true; + } + if (path === "src/domains/tools/runtime/tool-sdk.ts") { + return name.startsWith("define") || name === "createRigToolKit"; + } + const isCliBoundary = + path === "src/app/cli/entrypoint.ts" || + path === "src/app/cli/composition-root.ts" || + path === "src/app/cli/runtime-bootstrap.ts"; + const isScriptEntrypoint = /^scripts\/(?:bench|release|smoke)\.ts$/u.test(path); + return ( + (isCliBoundary || isScriptEntrypoint) && + new Set(["main", "program", "runCli", "runCliMain"]).has(name) + ); +} + +async function floatingBehaviorViolations(path: string) { + const source = await readFile(path, "utf8"); + const sourceFile = ts.createSourceFile( + path, + source, + ts.ScriptTarget.Latest, + true, + path.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS, + ); + const violations: string[] = []; + for (const statement of sourceFile.statements) { + inspectTopLevelStatement(path, sourceFile, statement, violations); + } + return violations; +} + +function inspectTopLevelStatement( + path: string, + sourceFile: ts.SourceFile, + statement: ts.Statement, + violations: string[], +) { + if (ts.isFunctionDeclaration(statement)) { + const name = statement.name?.text ?? "anonymousFunction"; + if (!isAllowedTopLevelFunction(path, name)) { + violations.push(sourceViolation(path, sourceFile, statement, name)); + } + } + if (ts.isVariableStatement(statement)) { + inspectTopLevelVariables(path, sourceFile, statement, violations); + } + const isDisallowedClass = + ts.isClassDeclaration(statement) && !isAllowedEffectClass(statement, sourceFile); + if (isDisallowedClass) { + violations.push( + sourceViolation(path, sourceFile, statement, statement.name?.text ?? "AnonymousClass"), + ); + } +} + +function inspectTopLevelVariables( + path: string, + sourceFile: ts.SourceFile, + statement: ts.VariableStatement, + violations: string[], +) { + for (const declaration of statement.declarationList.declarations) { + const name = ts.isIdentifier(declaration.name) ? declaration.name.text : "destructured"; + const initializer = declaration.initializer; + const isCallable = + initializer && + (ts.isArrowFunction(initializer) || + ts.isFunctionExpression(initializer) || + isEffectFnExpression(initializer)); + const isDisallowedCallable = isCallable && !isAllowedTopLevelFunction(path, name); + if (isDisallowedCallable) { + violations.push(sourceViolation(path, sourceFile, statement, name)); + } + } +} + +async function effectServiceViolations(path: string) { + const source = await readFile(path, "utf8"); + const sourceFile = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true); + const violations: string[] = []; + const inspect = (node: ts.Node) => { + const isNamedClass = ts.isClassDeclaration(node) && node.name !== undefined; + if (isNamedClass) { + inspectEffectServiceClass(path, sourceFile, node, violations); + } + ts.forEachChild(node, inspect); + }; + inspect(sourceFile); + return violations; +} + +function inspectEffectServiceClass( + path: string, + sourceFile: ts.SourceFile, + node: ts.ClassDeclaration, + violations: string[], +) { + const declaration = node.getText(sourceFile); + const isNotContextService = !/extends\s+Context\.Service\s*\s*\(\s*\)\s*\([\s\S]*?make\s*:\s*Effect\.(?:gen|succeed|sync)\s*\(/u + : /\}\s*>\s*\(\s*\)\s*\([\s\S]*?make\s*:\s*Effect\.gen\s*\(/u; + const hasInlineMake = makePattern.test(declaration); + const escapedName = name.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); + const hasOwnedLayer = new RegExp( + `static\\s+readonly\\s+layer\\s*=\\s*Layer\\.(?:effect|scoped)\\(\\s*${escapedName}\\s*,\\s*${escapedName}\\.make\\s*,?\\s*\\)`, + "u", + ).test(declaration); + const hasInvalidServiceContract = + !hasCanonicalName || !hasInlineApi || !hasInlineMake || !hasOwnedLayer; + if (hasInvalidServiceContract) { + violations.push(sourceViolation(path, sourceFile, node, name)); + } +} + +async function runtimeBoundaryViolations(path: string) { + const source = await readFile(path, "utf8"); + return [ + ...source.matchAll( + /\b(?:Effect\.(?:runCallback|runCallbackWith|runFork|runPromise|runPromiseExit|runPromiseWith|runSync|runSyncExit|runSyncWith)|ManagedRuntime\.make|BunRuntime\.runMain)\s*\(/gu, + ), + ].map((match) => `${path}:${source.slice(0, match.index).split("\n").length}`); +} + +function sourceViolation(path: string, sourceFile: ts.SourceFile, node: ts.Node, name: string) { + return `${path}:${sourceFile.getLineAndCharacterOfPosition(node.pos).line + 1}:${name}`; +} + +function isEffectFnExpression(expression: ts.Expression) { + const isNotCurriedCall = + !ts.isCallExpression(expression) || !ts.isCallExpression(expression.expression); + if (isNotCurriedCall) { + return false; + } + return expression.expression.expression.getText() === "Effect.fn"; +} + +function isAllowedEffectClass(declaration: ts.ClassDeclaration, sourceFile: ts.SourceFile) { + const heritage = + declaration.heritageClauses?.map((clause) => clause.getText(sourceFile)).join(" ") ?? ""; + return /extends\s+(?:Context\.Service|Schema\.(?:Class|ErrorClass|TaggedClass|TaggedErrorClass))\b/u.test( + heritage, + ); +} diff --git a/src/tooling/effect/effect-diagnostics.ts b/src/tooling/effect/effect-diagnostics.ts new file mode 100644 index 0000000..b04fa11 --- /dev/null +++ b/src/tooling/effect/effect-diagnostics.ts @@ -0,0 +1,34 @@ +async function capture(command: string[]): Promise { + const child = Bun.spawn(command, { stdout: "pipe", stderr: "pipe" }); + const [stdout, stderr, exitCode] = await Promise.all([ + new Response(child.stdout).text(), + new Response(child.stderr).text(), + child.exited, + ]); + if (exitCode !== 0) throw new Error(stderr || `${command[0]} exited with ${exitCode}.`); + return stdout; +} + +async function execute(command: string[]): Promise { + const child = Bun.spawn(command, { stdin: "inherit", stdout: "inherit", stderr: "inherit" }); + const exitCode = await child.exited; + if (exitCode !== 0) throw new Error(`${command[0]} exited with ${exitCode}.`); +} + +async function runEffectDiagnostics(): Promise { + const executable = (await capture(["bunx", "effect-tsgo", "get-exe-path"])).trim(); + if (!executable) throw new Error("@effect/tsgo did not report its diagnostics executable."); + if (process.platform !== "win32") await execute(["chmod", "+x", executable]); + await execute([ + "bunx", + "effect-tsgo", + "diagnostics", + "--project", + "tsconfig.json", + "--format", + "pretty", + "--strict", + ]); +} + +export const effectDiagnostics = await runEffectDiagnostics(); diff --git a/src/tooling/oxlint/effect-callable-contract.test.ts b/src/tooling/oxlint/effect-callable-contract.test.ts new file mode 100644 index 0000000..f64aaf5 --- /dev/null +++ b/src/tooling/oxlint/effect-callable-contract.test.ts @@ -0,0 +1,206 @@ +import type { Context, ESTree, Rule } from "@oxlint/plugins"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { effectServiceContractRule } from "./oxlint-plugin.ts"; + +describe("Effect callable payload contract", () => { + it("rejects Effect success objects with callable members", () => { + const report = vi.fn<(message: unknown) => void>(); + const service = serviceNode( + typeReference("Effect.Effect", [ + { + type: "TSTypeLiteral", + members: [ + { + type: "TSPropertySignature", + readonly: true, + key: identifier("close"), + typeAnnotation: { + type: "TSTypeAnnotation", + typeAnnotation: { type: "TSFunctionType", params: [] }, + }, + }, + ], + }, + ]), + spanned(call(member("Effect", "succeed"), [literal(undefined)])), + ); + const visitor = createVisitor(effectServiceContractRule, createContext(service.source, report)); + + visitor.ClassDeclaration?.(service.node as unknown as ESTree.Class); + + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ + messageId: "callableEffectPayload", + data: { name: "resource" }, + }), + ); + }); + + it("rejects lazy Effects that return locally-owned callable containers", () => { + const report = vi.fn<(message: unknown) => void>(); + const callable = call(member("Effect", "sync"), [ + { + type: "ArrowFunctionExpression", + body: { + type: "ObjectExpression", + properties: [property("close", { type: "ArrowFunctionExpression" })], + }, + }, + ]); + const service = serviceNode( + typeReference("Effect.Effect", [typeReference("ExternalHandle")]), + spanned(callable), + ); + const visitor = createVisitor(effectServiceContractRule, createContext(service.source, report)); + + visitor.ClassDeclaration?.(service.node as unknown as ESTree.Class); + + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ + messageId: "callableEffectValue", + data: { name: "resource" }, + }), + ); + }); +}); + +function serviceNode(apiType: object, implementation: object) { + const source = "readonly resource: Effect.Effect;"; + const serviceFactory = call( + member("Context", "Service"), + [], + [ + typeReference("HandleService"), + { + type: "TSTypeLiteral", + members: [ + { + type: "TSPropertySignature", + readonly: true, + key: identifier("resource"), + range: [0, source.length], + typeAnnotation: { type: "TSTypeAnnotation", typeAnnotation: apiType }, + }, + ], + }, + ], + ); + const make = call(member("Effect", "gen"), [ + { + type: "FunctionExpression", + generator: true, + body: { + type: "BlockStatement", + body: [ + variable("resource", implementation), + { + type: "ReturnStatement", + argument: { + type: "TSAsExpression", + expression: { + type: "ObjectExpression", + properties: [property("resource", identifier("resource"))], + }, + typeAnnotation: typeReference("const"), + }, + }, + ], + }, + }, + ]); + return { + source, + node: { + type: "ClassDeclaration", + id: identifier("HandleService"), + superClass: call(serviceFactory, [ + literal("@rendotdev/rig/test/HandleService"), + { type: "ObjectExpression", properties: [property("make", make)] }, + ]), + body: { + type: "ClassBody", + body: [ + { + type: "PropertyDefinition", + static: true, + readonly: true, + key: identifier("layer"), + value: call(member("Layer", "effect"), [ + identifier("HandleService"), + member("HandleService", "make"), + ]), + }, + ], + }, + }, + }; +} + +function spanned(effect: object) { + return call(member(effect, "pipe"), [ + call(member("Effect", "withSpan"), [literal("HandleService.resource")]), + ]); +} + +function createContext(source: string, report: ReturnType) { + return { + filename: "/repo/src/domains/tools/service/handle-service.ts", + report, + sourceCode: { text: source, getAllComments: () => [] }, + } as unknown as Context; +} + +function createVisitor(rule: Rule, context: Context) { + if (!rule.create) throw new Error("The Oxlint rule must provide a create visitor."); + return rule.create(context); +} + +function identifier(name: string) { + return { type: "Identifier", name }; +} + +function literal(value: unknown) { + return { type: "Literal", value }; +} + +function member(object: string | object, name: string) { + return { + type: "MemberExpression", + computed: false, + object: typeof object === "string" ? identifier(object) : object, + property: identifier(name), + }; +} + +function call(callee: object, arguments_: object[] = [], typeArguments?: object[]) { + return { + type: "CallExpression", + callee, + arguments: arguments_, + ...(typeArguments + ? { typeArguments: { type: "TSTypeParameterInstantiation", params: typeArguments } } + : {}), + }; +} + +function property(name: string, value: object) { + return { type: "Property", key: identifier(name), value, computed: false }; +} + +function variable(name: string, init: object) { + return { + type: "VariableDeclaration", + kind: "const", + declarations: [{ type: "VariableDeclarator", id: identifier(name), init }], + }; +} + +function typeReference(name: string, typeArguments?: object[]) { + return { + type: "TSTypeReference", + typeName: identifier(name), + ...(typeArguments + ? { typeArguments: { type: "TSTypeParameterInstantiation", params: typeArguments } } + : {}), + }; +} diff --git a/src/tooling/oxlint/effect-config-contract.test.ts b/src/tooling/oxlint/effect-config-contract.test.ts new file mode 100644 index 0000000..1a616f7 --- /dev/null +++ b/src/tooling/oxlint/effect-config-contract.test.ts @@ -0,0 +1,125 @@ +import type { Context, ESTree, Rule } from "@oxlint/plugins"; +import { describe, expect, it, vi } from "vite-plus/test"; +import { effectServiceContractRule } from "./oxlint-plugin.ts"; + +describe("Effect ConfigService contract", () => { + it("allows readonly non-callable data only on ConfigService APIs", () => { + const report = vi.fn<(message: unknown) => void>(); + const source = "readonly homeDir: string;"; + const make = call(member("Effect", "succeed"), [ + { + type: "TSAsExpression", + expression: { + type: "ObjectExpression", + properties: [property("homeDir", literal("/tmp/rig"))], + }, + typeAnnotation: typeReference("const"), + }, + ]); + const node = serviceNode("PathsConfigService", source, make); + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/settings/config/paths-config.ts", source, report), + ); + + visitor.ClassDeclaration?.(node as unknown as ESTree.Class); + + expect(report).not.toHaveBeenCalled(); + }); +}); + +function serviceNode(name: string, source: string, make: object) { + const serviceFactory = call( + member("Context", "Service"), + [], + [ + typeReference(name), + { + type: "TSTypeLiteral", + members: [ + { + type: "TSPropertySignature", + readonly: true, + key: identifier("homeDir"), + range: [0, source.length], + typeAnnotation: { + type: "TSTypeAnnotation", + typeAnnotation: { type: "TSStringKeyword" }, + }, + }, + ], + }, + ], + ); + return { + type: "ClassDeclaration", + id: identifier(name), + superClass: call(serviceFactory, [ + literal(`@rendotdev/rig/test/${name}`), + { type: "ObjectExpression", properties: [property("make", make)] }, + ]), + body: { + type: "ClassBody", + body: [ + { + type: "PropertyDefinition", + static: true, + readonly: true, + key: identifier("layer"), + value: call(member("Layer", "effect"), [identifier(name), member(name, "make")]), + }, + ], + }, + }; +} + +function createContext(filename: string, text: string, report: ReturnType) { + return { + filename, + report, + sourceCode: { text, getAllComments: () => [] }, + } as unknown as Context; +} + +function createVisitor(rule: Rule, context: Context) { + if (!rule.create) { + throw new Error("The Oxlint rule must provide a create visitor."); + } + return rule.create(context); +} + +function identifier(name: string) { + return { type: "Identifier", name }; +} + +function literal(value: string) { + return { type: "Literal", value }; +} + +function member(object: string | object, memberName: string) { + return { + type: "MemberExpression", + computed: false, + object: typeof object === "string" ? identifier(object) : object, + property: identifier(memberName), + }; +} + +function call(callee: object, arguments_: object[] = [], typeArguments?: object[]) { + return { + type: "CallExpression", + callee, + arguments: arguments_, + ...(typeArguments + ? { typeArguments: { type: "TSTypeParameterInstantiation", params: typeArguments } } + : {}), + }; +} + +function property(name: string, value: object) { + return { type: "Property", key: identifier(name), value, computed: false }; +} + +function typeReference(name: string) { + return { type: "TSTypeReference", typeName: identifier(name) }; +} diff --git a/src/tooling/oxlint/oxlint-plugin.test.ts b/src/tooling/oxlint/oxlint-plugin.test.ts index 8efa148..623c51f 100644 --- a/src/tooling/oxlint/oxlint-plugin.test.ts +++ b/src/tooling/oxlint/oxlint-plugin.test.ts @@ -1,14 +1,308 @@ -import type { Context, ESTree, Rule } from "@oxlint/plugins"; +import type { ESTree } from "@oxlint/plugins"; import { describe, expect, it, vi } from "vite-plus/test"; +import { + createOxlintContext as createContext, + createOxlintVisitor as createVisitor, + meaningfulSource, + oxlintImportNode as importNode, +} from "../../../test/fixtures/oxlint-rule-test-helpers.ts"; import { domainPublicApiRule, + effectRuntimeBoundaryRule, + effectServiceContractRule, layerBoundariesRule, maxFileLinesRule, maxFunctionLinesRule, namedCompoundIfConditionRule, + noBarrelFilesRule, + noFloatingBehaviorRule, sourceLocationRule, } from "./oxlint-plugin.ts"; +describe("Effect service contract", () => { + it("accepts input Effect.fn operations and lazy spanned zero-argument operations", () => { + const report = vi.fn<(message: unknown) => void>(); + const { node, source } = canonicalServiceNode(); + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/service/example-service.ts", source, report), + ); + + visitor.ClassDeclaration?.(node as unknown as ESTree.Class); + + expect(report).not.toHaveBeenCalled(); + }); + + it("reports detached APIs, detached make functions, and missing owned layers", () => { + const report = vi.fn<(message: unknown) => void>(); + const node = { + type: "ClassDeclaration", + id: identifier("Reader"), + superClass: call( + call( + member("Context", "Service"), + [], + [typeReference("Reader"), typeReference("ReaderShape")], + ), + ), + body: { type: "ClassBody", body: [] }, + }; + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/service/reader.ts", "", report), + ); + + visitor.ClassDeclaration?.(node as unknown as ESTree.Class); + + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "serviceSuffix" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "inlineApi" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "inlineMake" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "staticLayer" })); + }); + + it("reports Effect.fn trace names that do not match the service operation", () => { + const report = vi.fn<(message: unknown) => void>(); + const { node, source, readLabel } = canonicalServiceNode(); + readLabel.value = "Wrong.read"; + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/service/example-service.ts", source, report), + ); + + visitor.ClassDeclaration?.(node as unknown as ESTree.Class); + + expect(report).toHaveBeenCalledWith({ + node: readLabel, + messageId: "effectFnName", + data: { expected: "ExampleService.read" }, + }); + }); + + it("rejects deep and namespace Effect imports", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/service/example-service.ts", "", report), + ); + + visitor.ImportDeclaration?.({ + type: "ImportDeclaration", + source: literal("effect/ManagedRuntime"), + specifiers: [], + } as unknown as ESTree.ImportDeclaration); + visitor.ImportDeclaration?.({ + type: "ImportDeclaration", + source: literal("effect"), + specifiers: [{ type: "ImportNamespaceSpecifier" }], + } as unknown as ESTree.ImportDeclaration); + + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "deepEffectImport" })); + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ messageId: "namedEffectImport" }), + ); + }); + + it("requires descriptive Layer suffixes for top-level layer bindings", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/runtime/tool-runtime.ts", "", report), + ); + visitor.VariableDeclarator?.({ + type: "VariableDeclarator", + id: identifier("toolRuntime"), + init: call(member("Layer", "merge")), + } as ESTree.VariableDeclarator); + + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ messageId: "layerSuffix", data: { name: "toolRuntime" } }), + ); + }); + + it("requires tagged operational failures to use the Error suffix", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectServiceContractRule, + createContext("/repo/src/domains/tools/types/failures.ts", "", report), + ); + visitor.ClassDeclaration?.({ + type: "ClassDeclaration", + id: identifier("ToolFailure"), + superClass: call(call(member("Schema", "TaggedErrorClass"))), + body: { type: "ClassBody", body: [] }, + } as unknown as ESTree.Class); + + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ + messageId: "taggedErrorSuffix", + data: { name: "ToolFailure" }, + }), + ); + }); +}); + +describe("Effect behavior boundaries", () => { + it("rejects floating functions, callable constants, constructions, and ordinary classes", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + noFloatingBehaviorRule, + createContext("/repo/src/domains/tools/service/floating.ts", "", report), + ); + + visitor.FunctionDeclaration?.({ + type: "FunctionDeclaration", + id: identifier("loadTool"), + } as ESTree.Function); + visitor.VariableDeclarator?.({ + type: "VariableDeclarator", + id: identifier("readTool"), + init: { type: "ArrowFunctionExpression" }, + } as ESTree.VariableDeclarator); + visitor.ClassDeclaration?.({ + type: "ClassDeclaration", + id: identifier("ToolManager"), + superClass: null, + } as ESTree.Class); + visitor.ClassExpression?.({ + type: "ClassExpression", + id: identifier("InlineManager"), + superClass: null, + } as ESTree.Class); + visitor.VariableDeclarator?.({ + type: "VariableDeclarator", + id: identifier("client"), + init: { type: "NewExpression", callee: identifier("Client") }, + } as ESTree.VariableDeclarator); + + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "floatingFunction" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "floatingCallable" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "behavioralClass" })); + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ messageId: "floatingConstruction" }), + ); + }); + + it("allows React components, Schema data classes, and test callbacks", () => { + const reactReport = vi.fn<(message: unknown) => void>(); + const reactVisitor = createVisitor( + noFloatingBehaviorRule, + createContext("/repo/src/domains/tools/ui/tool-view.tsx", "", reactReport), + ); + reactVisitor.FunctionDeclaration?.({ + type: "FunctionDeclaration", + id: identifier("ToolView"), + } as ESTree.Function); + reactVisitor.ClassDeclaration?.({ + type: "ClassDeclaration", + id: identifier("ToolError"), + superClass: call(member("Schema", "TaggedErrorClass")), + } as ESTree.Class); + + const testReport = vi.fn<(message: unknown) => void>(); + const testVisitor = createVisitor( + noFloatingBehaviorRule, + createContext("/repo/src/domains/tools/service/tool.test.ts", "", testReport), + ); + testVisitor.FunctionDeclaration?.({ + type: "FunctionDeclaration", + id: identifier("fixture"), + } as ESTree.Function); + + expect(reactReport).not.toHaveBeenCalled(); + expect(testReport).not.toHaveBeenCalled(); + }); + + it("keeps Effect runners outside application services", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectRuntimeBoundaryRule, + createContext("/repo/src/domains/tools/service/run-tool.ts", "", report), + ); + const node = call(member("Effect", "runPromise")); + + visitor.CallExpression?.(node as ESTree.CallExpression); + + expect(report).toHaveBeenCalledWith({ + node, + messageId: "runtimeBoundary", + data: { call: "Effect.runPromise" }, + }); + }); + + it("allows the explicit Promise collection-handle adapter boundary", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectRuntimeBoundaryRule, + createContext( + "/repo/src/domains/collections/runtime/collection-handle-factory.ts", + "", + report, + ), + ); + + visitor.CallExpression?.(call(member("Effect", "runPromise")) as ESTree.CallExpression); + + expect(report).not.toHaveBeenCalled(); + }); + + it("allows the explicit Promise SQLite collection-index adapter boundary", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectRuntimeBoundaryRule, + createContext( + "/repo/src/domains/collections/repo/sqlite/sqlite-collection-index.ts", + "", + report, + ), + ); + + visitor.CallExpression?.(call(member("Effect", "runPromise")) as ESTree.CallExpression); + + expect(report).not.toHaveBeenCalled(); + }); + + it("requires ManagedRuntime disposal at approved compatibility boundaries", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + effectRuntimeBoundaryRule, + createContext("/repo/src/domains/tools/runtime/tool-sdk.ts", "", report), + ); + const declaration = { + type: "VariableDeclarator", + id: identifier("runtime"), + init: call(member("ManagedRuntime", "make")), + }; + + visitor.VariableDeclarator?.(declaration as ESTree.VariableDeclarator); + visitor["Program:exit"]?.({ type: "Program" } as ESTree.Program); + + expect(report).toHaveBeenCalledWith({ + node: declaration, + messageId: "undisposedRuntime", + data: { name: "runtime" }, + }); + }); +}); + +describe("barrel prohibition", () => { + it("reports index modules and every external re-export facade", () => { + const report = vi.fn<(message: unknown) => void>(); + const visitor = createVisitor( + noBarrelFilesRule, + createContext("/repo/src/domains/tools/index.ts", "", report), + ); + + visitor.Program?.({ type: "Program" } as ESTree.Program); + visitor.ExportAllDeclaration?.({ + type: "ExportAllDeclaration", + source: literal("./tool.ts"), + } as unknown as ESTree.ExportAllDeclaration); + + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "indexFile" })); + expect(report).toHaveBeenCalledWith(expect.objectContaining({ messageId: "reExport" })); + }); +}); + describe("architecture dependency rules", () => { it("reports a forbidden layer dependency with remediation", () => { const report = vi.fn<(message: unknown) => void>(); @@ -19,12 +313,18 @@ describe("architecture dependency rules", () => { visitor.ImportDeclaration?.(importNode("../runtime/server.ts") as ESTree.ImportDeclaration); - expect(report).toHaveBeenCalledWith({ - node: expect.objectContaining({ value: "../runtime/server.ts" }), - messageId: "invalidDependency", - }); + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ + node: expect.objectContaining({ value: "../runtime/server.ts" }), + messageId: "invalidDependency", + data: expect.objectContaining({ + source: "tools/service", + target: "tools/runtime", + }), + }), + ); expect(layerBoundariesRule.meta?.messages?.invalidDependency).toContain( - "move orchestration to runtime/app", + "{{source}} cannot import {{target}}", ); }); @@ -40,7 +340,7 @@ describe("architecture dependency rules", () => { expect(report).not.toHaveBeenCalled(); }); - it("reports app and cross-domain deep imports through the public API rule", () => { + it("allows direct module imports and rejects barrel imports", () => { const appReport = vi.fn<(message: unknown) => void>(); const appVisitor = createVisitor( domainPublicApiRule, @@ -50,21 +350,23 @@ describe("architecture dependency rules", () => { importNode("../../domains/tools/service/run-tool.ts") as ESTree.ImportDeclaration, ); - const domainReport = vi.fn<(message: unknown) => void>(); - const domainVisitor = createVisitor( + const barrelReport = vi.fn<(message: unknown) => void>(); + const barrelVisitor = createVisitor( domainPublicApiRule, - createContext("/repo/src/domains/tools/service/run-tool.ts", "", domainReport), + createContext("/repo/src/app/cli/cli.ts", "", barrelReport), ); - domainVisitor.ExportAllDeclaration?.( - exportAllNode("../../settings/repo/store.ts") as ESTree.ExportAllDeclaration, + barrelVisitor.ImportDeclaration?.( + importNode("../../domains/tools/index.ts") as ESTree.ImportDeclaration, ); - expect(appReport).toHaveBeenCalledOnce(); - expect(domainReport).toHaveBeenCalledOnce(); - expect(domainPublicApiRule.meta?.messages?.invalidDependency).toContain("layer index.ts"); + expect(appReport).not.toHaveBeenCalled(); + expect(barrelReport).toHaveBeenCalledOnce(); + expect(domainPublicApiRule.meta?.messages?.invalidDependency).toContain( + "Barrel index.ts files are forbidden", + ); }); - it("allows domain public API imports", () => { + it("allows direct domain module imports", () => { const report = vi.fn<(message: unknown) => void>(); const visitor = createVisitor( domainPublicApiRule, @@ -72,7 +374,7 @@ describe("architecture dependency rules", () => { ); visitor.ImportDeclaration?.( - importNode("../../domains/tools/index.ts") as ESTree.ImportDeclaration, + importNode("../../domains/tools/service/run-tool.ts") as ESTree.ImportDeclaration, ); expect(report).not.toHaveBeenCalled(); @@ -217,40 +519,166 @@ describe("named compound if condition rule", () => { }); }); -function createContext( - filename: string, - text: string, - report: ReturnType, - comments: Array<{ type: string; value: string; range: [number, number] }> = [], -) { - return { - filename, - report, - sourceCode: { - text, - getAllComments() { - return comments; +function canonicalServiceNode(options: { refreshEffect?: object } = {}) { + const source = + "readonly read: (input: string) => Effect.Effect; readonly refresh: Effect.Effect;"; + const readStart = source.indexOf("readonly read"); + const readEnd = source.indexOf(";", readStart) + 1; + const refreshStart = source.indexOf("readonly refresh"); + const readLabel = literal("ExampleService.read") as { type: string; value: string }; + const readEffect = effectFn(readLabel, true); + const refreshEffect = + options.refreshEffect ?? + call( + member( + call(member("Effect", "gen"), [{ type: "FunctionExpression", generator: true }]), + "pipe", + ), + [call(member("Effect", "withSpan"), [literal("ExampleService.refresh")])], + ); + const make = canonicalMake(readEffect, refreshEffect); + const api = canonicalApi(readStart, readEnd, refreshStart, source.length); + const serviceFactory = call( + member("Context", "Service"), + [], + [typeReference("ExampleService"), api], + ); + const node = { + type: "ClassDeclaration", + id: identifier("ExampleService"), + superClass: call(serviceFactory, [ + literal("@rendotdev/rig/example/ExampleService"), + { type: "ObjectExpression", properties: [property("make", make)] }, + ]), + body: { + type: "ClassBody", + body: [ + { + type: "PropertyDefinition", + static: true, + readonly: true, + key: identifier("layer"), + value: call(member("Layer", "effect"), [ + identifier("ExampleService"), + member("ExampleService", "make"), + ]), + }, + ], + }, + }; + return { node, source, readLabel, refreshMember: api.members[1]! }; +} + +function canonicalMake(readEffect: object, refreshEffect: object) { + const returnedApi = { + type: "ReturnStatement", + argument: { + type: "TSAsExpression", + expression: { + type: "ObjectExpression", + properties: [ + property("read", identifier("read")), + property("refresh", identifier("refresh")), + ], }, + typeAnnotation: typeReference("const"), }, - } as unknown as Context; + }; + return call(member("Effect", "gen"), [ + { + type: "FunctionExpression", + generator: true, + body: { + type: "BlockStatement", + body: [ + variable("normalize", { type: "ArrowFunctionExpression" }), + variable("read", readEffect), + variable("refresh", refreshEffect), + returnedApi, + ], + }, + }, + ]); +} + +function canonicalApi(readStart: number, readEnd: number, refreshStart: number, sourceEnd: number) { + return { + type: "TSTypeLiteral", + members: [ + { + type: "TSPropertySignature", + readonly: true, + key: identifier("read"), + range: [readStart, readEnd], + typeAnnotation: { + type: "TSTypeAnnotation", + typeAnnotation: { type: "TSFunctionType", params: [identifier("input")] }, + }, + }, + { + type: "TSPropertySignature", + readonly: true, + key: identifier("refresh"), + range: [refreshStart, sourceEnd], + typeAnnotation: { + type: "TSTypeAnnotation", + typeAnnotation: typeReference("Effect.Effect"), + }, + }, + ], + }; } -function createVisitor(rule: Rule, context: Context) { - const create = rule.create; - if (!create) { - throw new Error("The Oxlint rule must provide a create visitor."); - } - return create(context); +function identifier(name: string) { + return { type: "Identifier", name }; } -function importNode(value: string) { - return { type: "ImportDeclaration", source: { type: "Literal", value } }; +function literal(value: string) { + return { type: "Literal", value }; } -function exportAllNode(value: string) { - return { type: "ExportAllDeclaration", source: { type: "Literal", value } }; +function member(object: string | object, memberName: string) { + return { + type: "MemberExpression", + computed: false, + object: typeof object === "string" ? identifier(object) : object, + property: identifier(memberName), + }; +} + +function call(callee: object, arguments_: object[] = [], typeArguments?: object[]) { + return { + type: "CallExpression", + callee, + arguments: arguments_, + ...(typeArguments + ? { typeArguments: { type: "TSTypeParameterInstantiation", params: typeArguments } } + : {}), + }; +} + +function property(name: string, value: object) { + return { type: "Property", key: identifier(name), value, computed: false }; +} + +function variable(name: string, init: object) { + return { + type: "VariableDeclaration", + kind: "const", + declarations: [{ type: "VariableDeclarator", id: identifier(name), init }], + }; +} + +function typeReference(name: string, typeArguments?: object[]) { + return { + type: "TSTypeReference", + typeName: identifier(name), + ...(typeArguments + ? { typeArguments: { type: "TSTypeParameterInstantiation", params: typeArguments } } + : {}), + }; } -function meaningfulSource(lines: number) { - return Array.from({ length: lines }, (_, index) => `const value${index} = ${index};`).join("\n"); +function effectFn(label: object, generator: boolean) { + return call(call(member("Effect", "fn"), [label]), [{ type: "FunctionExpression", generator }]); } diff --git a/src/tooling/oxlint/oxlint-plugin.ts b/src/tooling/oxlint/oxlint-plugin.ts index a592007..42eed1d 100644 --- a/src/tooling/oxlint/oxlint-plugin.ts +++ b/src/tooling/oxlint/oxlint-plugin.ts @@ -1,17 +1,25 @@ import { definePlugin } from "@oxlint/plugins"; import { domainPublicApiRule } from "./rules/domain-public-api.ts"; +import { effectRuntimeBoundaryRule } from "./rules/effect-runtime-boundary.ts"; +import { effectServiceContractRule } from "./rules/effect-service-contract.ts"; import { layerBoundariesRule } from "./rules/layer-boundaries.ts"; import { maxFileLinesRule } from "./rules/max-file-lines.ts"; import { maxFunctionLinesRule } from "./rules/max-function-lines.ts"; import { namedCompoundIfConditionRule } from "./rules/named-compound-if-condition.ts"; +import { noBarrelFilesRule } from "./rules/no-barrel-files.ts"; +import { noFloatingBehaviorRule } from "./rules/no-floating-behavior.ts"; import { sourceLocationRule } from "./rules/source-location.ts"; export { domainPublicApiRule, + effectRuntimeBoundaryRule, + effectServiceContractRule, layerBoundariesRule, maxFileLinesRule, maxFunctionLinesRule, namedCompoundIfConditionRule, + noBarrelFilesRule, + noFloatingBehaviorRule, sourceLocationRule, }; @@ -19,10 +27,14 @@ export default definePlugin({ meta: { name: "rig" }, rules: { "domain-public-api": domainPublicApiRule, + "effect-runtime-boundary": effectRuntimeBoundaryRule, + "effect-service-contract": effectServiceContractRule, "layer-boundaries": layerBoundariesRule, "max-file-lines": maxFileLinesRule, "max-function-lines": maxFunctionLinesRule, "named-compound-if-condition": namedCompoundIfConditionRule, + "no-barrel-files": noBarrelFilesRule, + "no-floating-behavior": noFloatingBehaviorRule, "source-location": sourceLocationRule, }, }); diff --git a/src/tooling/oxlint/rules/dependency.ts b/src/tooling/oxlint/rules/dependency.ts index 22a7658..db06f31 100644 --- a/src/tooling/oxlint/rules/dependency.ts +++ b/src/tooling/oxlint/rules/dependency.ts @@ -1,6 +1,8 @@ import type { ESTree, Node, Rule } from "@oxlint/plugins"; import { defineRule } from "@oxlint/plugins"; import { + dependencyRemediation, + describeSourceClassification, resolveImportTarget, type DependencyDecision, validateDependency, @@ -27,7 +29,15 @@ export function defineDependencyRule(definition: DependencyRuleDefinition): Rule } const decision = validateDependency(context.filename, target); if (definition.shouldReport(decision)) { - context.report({ node, messageId: "invalidDependency" }); + context.report({ + node, + messageId: "invalidDependency", + data: { + source: describeSourceClassification(decision.source), + target: describeSourceClassification(decision.target), + remediation: dependencyRemediation(decision), + }, + }); } } return { @@ -57,8 +67,5 @@ export function defineDependencyRule(definition: DependencyRuleDefinition): Rule } export function isPublicApiViolation(decision: DependencyDecision) { - return ( - !decision.allowed && - (decision.reason === "app-internal-domain" || decision.reason === "cross-domain-internal") - ); + return decision.target.kind === "invalid" && decision.target.reason === "source-location"; } diff --git a/src/tooling/oxlint/rules/domain-public-api.ts b/src/tooling/oxlint/rules/domain-public-api.ts index d081177..7d9c28d 100644 --- a/src/tooling/oxlint/rules/domain-public-api.ts +++ b/src/tooling/oxlint/rules/domain-public-api.ts @@ -1,8 +1,8 @@ import { defineDependencyRule, isPublicApiViolation } from "./dependency.ts"; export const domainPublicApiRule = defineDependencyRule({ - description: "Require app and cross-domain imports to use a domain or layer public API.", + description: "Require imports to name their concrete domain module.", message: - "Import this capability through the target domain or layer index.ts. Keep internal files private to their domain.", + "Import the concrete service, type, or repository module directly. Barrel index.ts files are forbidden.", shouldReport: isPublicApiViolation, }); diff --git a/src/tooling/oxlint/rules/effect-ast.ts b/src/tooling/oxlint/rules/effect-ast.ts new file mode 100644 index 0000000..2508668 --- /dev/null +++ b/src/tooling/oxlint/rules/effect-ast.ts @@ -0,0 +1,134 @@ +import type { Node } from "@oxlint/plugins"; + +export type EffectAstNode = Node & { + readonly [key: string]: unknown; + readonly parent?: EffectAstNode; +}; + +export function asEffectNode(value: unknown): EffectAstNode | undefined { + const isEffectAstNode = value !== null && typeof value === "object" && "type" in value; + if (!isEffectAstNode) { + return undefined; + } + return value as unknown as EffectAstNode; +} + +export function unwrapExpression(value: unknown): EffectAstNode | undefined { + let node = asEffectNode(value); + while ( + node && + (node.type === "ParenthesizedExpression" || + node.type === "TSAsExpression" || + node.type === "TSSatisfiesExpression" || + node.type === "TSNonNullExpression" || + node.type === "TSTypeAssertion" || + node.type === "TSInstantiationExpression" || + node.type === "ChainExpression") + ) { + node = asEffectNode(node.expression); + } + return node; +} + +export function identifierName(value: unknown): string | undefined { + const node = asEffectNode(value); + return node?.type === "Identifier" && typeof node.name === "string" ? node.name : undefined; +} + +export function propertyName(value: unknown): string | undefined { + const node = asEffectNode(value); + const isNamedIdentifier = node?.type === "Identifier" && typeof node.name === "string"; + if (isNamedIdentifier) { + return node.name as string; + } + return node?.type === "Literal" && typeof node.value === "string" ? node.value : undefined; +} + +export function memberPath(value: unknown): string | undefined { + const node = unwrapExpression(value); + if (!node) { + return undefined; + } + const name = identifierName(node); + if (name) { + return name; + } + const isUnsupportedMemberExpression = node.type !== "MemberExpression" || node.computed === true; + if (isUnsupportedMemberExpression) { + return undefined; + } + const objectPath = memberPath(node.object); + const memberName = propertyName(node.property); + return objectPath && memberName ? `${objectPath}.${memberName}` : undefined; +} + +export function isCallTo(value: unknown, path: string): boolean { + const node = unwrapExpression(value); + return node?.type === "CallExpression" && memberPath(node.callee) === path; +} + +export function callArguments(value: unknown): readonly EffectAstNode[] { + const node = unwrapExpression(value); + const hasNoCallArguments = node?.type !== "CallExpression" || !Array.isArray(node.arguments); + if (hasNoCallArguments) { + return []; + } + const argumentsArray = node.arguments as unknown[]; + return argumentsArray.map(asEffectNode).filter((argument) => argument !== undefined); +} + +export function callTypeArguments(value: unknown): readonly EffectAstNode[] { + const node = unwrapExpression(value); + if (node?.type !== "CallExpression") { + return []; + } + const typeArguments = asEffectNode(node.typeArguments ?? node.typeParameters); + const hasNoTypeArguments = !typeArguments || !Array.isArray(typeArguments.params); + if (hasNoTypeArguments) { + return []; + } + const params = typeArguments.params as unknown[]; + return params.map(asEffectNode).filter((argument) => argument !== undefined); +} + +export function isTopLevelDeclaration(value: unknown): boolean { + const node = asEffectNode(value); + if (!node?.parent) { + return true; + } + if (node.parent.type === "Program") { + return true; + } + return ( + (node.parent.type === "ExportNamedDeclaration" || + node.parent.type === "ExportDefaultDeclaration") && + node.parent.parent?.type === "Program" + ); +} + +export function sourcePathKey(filePath: string): string { + const normalized = filePath.replaceAll("\\", "/"); + for (const marker of ["/src/", "/scripts/", "/test/"] as const) { + const index = normalized.lastIndexOf(marker); + if (index >= 0) { + return normalized.slice(index + 1); + } + } + return normalized.replace(/^\.\//u, ""); +} + +export function isTestOrFixturePath(filePath: string): boolean { + const path = sourcePathKey(filePath); + return ( + /\.(?:test|spec)\.[cm]?[jt]sx?$/u.test(path) || + path.startsWith("test/") || + path.includes("/fixtures/") || + path.endsWith(".d.ts") + ); +} + +export function isEffectPolicyPath(filePath: string): boolean { + const path = sourcePathKey(filePath); + const isProductionSource = path.startsWith("src/") || path.startsWith("scripts/"); + return isProductionSource && !path.startsWith("src/tooling/") && !isTestOrFixturePath(path); +} diff --git a/src/tooling/oxlint/rules/effect-runtime-boundary.ts b/src/tooling/oxlint/rules/effect-runtime-boundary.ts new file mode 100644 index 0000000..0e56308 --- /dev/null +++ b/src/tooling/oxlint/rules/effect-runtime-boundary.ts @@ -0,0 +1,123 @@ +import type { ESTree } from "@oxlint/plugins"; +import { defineRule } from "@oxlint/plugins"; +import { + identifierName, + isCallTo, + isEffectPolicyPath, + isTestOrFixturePath, + memberPath, + sourcePathKey, + unwrapExpression, +} from "./effect-ast.ts"; + +const runtimeCalls = new Set([ + "BunRuntime.runMain", + "Effect.runFork", + "Effect.runCallback", + "Effect.runCallbackWith", + "Effect.runPromise", + "Effect.runPromiseExit", + "Effect.runPromiseWith", + "Effect.runSync", + "Effect.runSyncExit", + "Effect.runSyncWith", + "ManagedRuntime.make", +]); + +export const effectRuntimeBoundaryRule = defineRule({ + meta: { + type: "problem", + docs: { + description: + "Keep Effect execution and ManagedRuntime ownership at explicit process/API boundaries.", + }, + messages: { + runtimeBoundary: + "Move '{{call}}' to an approved CLI, script, or SDK compatibility boundary. Application services compose and return Effects; they never execute them.", + undisposedRuntime: + "ManagedRuntime '{{name}}' must expose or invoke dispose at this boundary so scopes, connections, and finalizers are closed.", + }, + }, + create(context) { + const isOutsideRuntimePolicy = + !isEffectPolicyPath(context.filename) || isTestOrFixturePath(context.filename); + if (isOutsideRuntimePolicy) { + return {}; + } + const filePath = sourcePathKey(context.filename); + const runtimes = new Map(); + const disposed = new Set(); + + return { + VariableDeclarator(node: ESTree.VariableDeclarator) { + if (isCallTo(node.init, "ManagedRuntime.make")) { + const name = identifierName(node.id); + if (name) { + runtimes.set(name, node); + } + } + }, + CallExpression(node: ESTree.CallExpression) { + const path = memberPath(node.callee); + const isUnapprovedRuntimeCall = + path !== undefined && runtimeCalls.has(path) && !isApprovedRuntimeBoundary(filePath); + if (isUnapprovedRuntimeCall) { + context.report({ node, messageId: "runtimeBoundary", data: { call: path } }); + } + const isAnonymousManagedRuntime = + path === "ManagedRuntime.make" && + isApprovedRuntimeBoundary(filePath) && + node.parent?.type !== "VariableDeclarator"; + if (isAnonymousManagedRuntime) { + context.report({ + node, + messageId: "undisposedRuntime", + data: { name: "anonymousRuntime" }, + }); + } + const callee = unwrapExpression(node.callee); + const isUnsupportedMemberCall = + callee?.type !== "MemberExpression" || callee.computed === true; + if (isUnsupportedMemberCall) { + return; + } + const objectName = identifierName(callee.object); + const isRuntimeDisposal = + objectName !== undefined && identifierName(callee.property) === "dispose"; + if (isRuntimeDisposal) { + disposed.add(objectName); + } + }, + "Program:exit"() { + if (!isApprovedRuntimeBoundary(filePath)) { + return; + } + for (const [name, node] of runtimes) { + const isUndisposedRuntime = + !disposed.has(name) && !hasExposedDisposer(context.sourceCode.text, name); + if (isUndisposedRuntime) { + context.report({ node, messageId: "undisposedRuntime", data: { name } }); + } + } + }, + }; + }, +}); + +function isApprovedRuntimeBoundary(filePath: string) { + return ( + filePath === "src/app/cli/entrypoint.ts" || + filePath === "src/app/cli/composition-root.ts" || + filePath === "src/app/cli/runtime-bootstrap.ts" || + filePath === "src/domains/collections/runtime/collection-handle-factory.ts" || + filePath === "src/domains/collections/repo/sqlite/sqlite-collection-index.ts" || + filePath === "src/domains/tools/runtime/tool-sdk.ts" || + filePath === "src/domains/tools/runtime/runtime-support.ts" || + /^scripts\/(?:bench|release|smoke)\.ts$/u.test(filePath) + ); +} + +function hasExposedDisposer(source: string, runtimeName: string) { + const escapedName = runtimeName.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); + return new RegExp(`\\bdispose\\s*:\\s*${escapedName}\\.dispose\\b`, "u").test(source); +} diff --git a/src/tooling/oxlint/rules/effect-service-callable-contract.ts b/src/tooling/oxlint/rules/effect-service-callable-contract.ts new file mode 100644 index 0000000..926f745 --- /dev/null +++ b/src/tooling/oxlint/rules/effect-service-callable-contract.ts @@ -0,0 +1,137 @@ +import { + asEffectNode, + callArguments, + identifierName, + isCallTo, + unwrapExpression, + type EffectAstNode, +} from "./effect-ast.ts"; + +export function effectSuccessTypeHasCallableMembers(apiType: EffectAstNode | undefined) { + const effectType = + apiType?.type === "TSFunctionType" + ? annotatedType(apiType.returnType ?? apiType.typeAnnotation) + : apiType; + const isEffectType = + effectType?.type === "TSTypeReference" && typeNamePath(effectType.typeName) === "Effect.Effect"; + if (!isEffectType) { + return false; + } + const [successType] = typeArguments(effectType); + return typeHasCallableMembers(successType); +} + +export function lazyEffectExposesCallableContainer( + value: EffectAstNode, + localCallables: ReadonlySet, +) { + const pipeCall = unwrapExpression(value); + const pipeCallee = + pipeCall?.type === "CallExpression" ? unwrapExpression(pipeCall.callee) : undefined; + if (pipeCallee?.type !== "MemberExpression") return false; + const source = unwrapExpression(pipeCallee.object); + if (isCallTo(source, "Effect.succeed")) { + return objectHasCallableMembers(callArguments(source)[0], localCallables); + } + const isEffectCallback = isCallTo(source, "Effect.sync") || isCallTo(source, "Effect.gen"); + if (isEffectCallback) { + return callbackReturnsCallableObject(callArguments(source)[0], localCallables); + } + return false; +} + +function annotatedType(value: unknown) { + const node = asEffectNode(value); + return node?.type === "TSTypeAnnotation" ? asEffectNode(node.typeAnnotation) : node; +} + +function typeArguments(value: EffectAstNode) { + const container = asEffectNode(value.typeArguments ?? value.typeParameters); + return Array.isArray(container?.params) + ? container.params.map(asEffectNode).filter((entry) => entry !== undefined) + : []; +} + +function typeNamePath(value: unknown): string | undefined { + const node = asEffectNode(value); + const direct = identifierName(node); + if (direct) return direct; + if (node?.type !== "TSQualifiedName") return undefined; + const left = typeNamePath(node.left); + const right = identifierName(node.right); + return left && right ? `${left}.${right}` : undefined; +} + +function typeHasCallableMembers(value: EffectAstNode | undefined): boolean { + if (!value) return false; + if (value.type === "TSParenthesizedType") { + return typeHasCallableMembers(asEffectNode(value.typeAnnotation)); + } + const isReadonlyType = + value.type === "TSTypeReference" && typeNamePath(value.typeName) === "Readonly"; + if (isReadonlyType) { + return typeHasCallableMembers(typeArguments(value)[0]); + } + const hasNoCallableTypeMembers = value.type !== "TSTypeLiteral" || !Array.isArray(value.members); + if (hasNoCallableTypeMembers) return false; + const members = value.members as unknown[]; + return members + .map(asEffectNode) + .filter((member) => member !== undefined) + .some((member) => { + const isCallableSignature = + member.type === "TSMethodSignature" || member.type === "TSCallSignatureDeclaration"; + if (isCallableSignature) { + return true; + } + return ( + member.type === "TSPropertySignature" && + annotatedType(member.typeAnnotation)?.type === "TSFunctionType" + ); + }); +} + +function callbackReturnsCallableObject( + value: EffectAstNode | undefined, + localCallables: ReadonlySet, +) { + const isFunctionCallback = + value?.type === "ArrowFunctionExpression" || value?.type === "FunctionExpression"; + if (!isFunctionCallback) { + return false; + } + const body = asEffectNode(value.body); + if (body?.type === "ObjectExpression") return objectHasCallableMembers(body, localCallables); + const hasNoBlockBody = body?.type !== "BlockStatement" || !Array.isArray(body.body); + if (hasNoBlockBody) return false; + const bodyStatements = body.body as unknown[]; + const returned = bodyStatements + .map(asEffectNode) + .filter((statement) => statement !== undefined) + .findLast((statement) => statement.type === "ReturnStatement"); + return objectHasCallableMembers(returned?.argument, localCallables); +} + +function objectHasCallableMembers(value: unknown, localCallables: ReadonlySet): boolean { + const object = unwrapExpression(value); + const hasNoObjectProperties = + object?.type !== "ObjectExpression" || !Array.isArray(object.properties); + if (hasNoObjectProperties) return false; + const properties = object.properties as unknown[]; + return properties + .map(asEffectNode) + .filter((property) => property !== undefined) + .some((property) => { + if (property.type !== "Property") return false; + if (property.method === true) return true; + const memberValue = unwrapExpression(property.value); + const isCallableMember = + memberValue?.type === "ArrowFunctionExpression" || + memberValue?.type === "FunctionExpression"; + if (isCallableMember) { + return true; + } + const memberName = identifierName(memberValue); + return memberName !== undefined && localCallables.has(memberName); + }); +} diff --git a/src/tooling/oxlint/rules/effect-service-contract.ts b/src/tooling/oxlint/rules/effect-service-contract.ts new file mode 100644 index 0000000..554293a --- /dev/null +++ b/src/tooling/oxlint/rules/effect-service-contract.ts @@ -0,0 +1,145 @@ +import type { ESTree } from "@oxlint/plugins"; +import { defineRule } from "@oxlint/plugins"; +import { + identifierName, + isEffectPolicyPath, + isTopLevelDeclaration, + memberPath, + unwrapExpression, +} from "./effect-ast.ts"; +import { inspectContextService, validateEffectService } from "./effect-service-inspector.ts"; + +export const effectServiceContractRule = defineRule({ + meta: { + type: "problem", + docs: { + description: + "Enforce Rig's canonical Effect v4 Context.Service declaration and method shape.", + }, + messages: { + serviceSuffix: + "Name this behavioral service '{{name}}Service' so its architectural role is explicit.", + inlineApi: + "Inline the service API as Context.Service Effect.Effect<...> }>; detached Shape aliases are forbidden.", + readonlyApi: + "Declare '{{name}}' as readonly. Operations with inputs are function properties returning Effect.Effect; zero-argument operations are lazy Effect.Effect values.", + lazyEffectApi: + "Declare zero-argument operation '{{name}}' as readonly {{name}}: Effect.Effect<...>, then trace its value with .pipe(Effect.withSpan(\"{{service}}.{{name}}\")).", + inlineMake: + "Declare make inline in Context.Service options. Behavioral services use Effect.gen(function* () { ... }); ConfigService may use Effect.gen, Effect.succeed, or Effect.sync.", + pureHelper: + "Declare private pure helper '{{name}}' as a local const arrow inside the service make block.", + localConst: + "Declare private helper '{{name}}' with const. Service helpers and operations are immutable local bindings inside make.", + effectHelper: + "Wrap effectful helper '{{name}}' with Effect.fn(\"{{service}}.{{name}}\")(function* (...) { ... }).", + effectFnName: + "Name this effect '{{expected}}' so traces identify the owning service and method.", + effectFnGenerator: + "Implement '{{name}}' with the generator callback form Effect.fn(\"{{service}}.{{name}}\")(function* (...) { ... }).", + publicMethod: + "Public service operation '{{name}}' must resolve to a local named Effect.fn, or a lazy Effect value traced with Effect.withSpan for a zero-argument operation.", + callableEffectPayload: + "Service operation '{{name}}' exposes an object with callable members through Effect. Promote the owned behavior to named service methods instead.", + callableEffectValue: + "Lazy service operation '{{name}}' returns a locally-owned callable container. Expose each behavior as a named Effect service method instead.", + effectSpan: + "Trace lazy Effect '{{name}}' with .pipe(Effect.withSpan(\"{{service}}.{{name}}\")).", + returnedApi: + "Finish the service make block with return { publicMethod } as const; return only the declared public Effect.fn methods.", + apiMismatch: + "Keep the inline API and returned object identical. Declared: {{declared}}. Returned: {{returned}}.", + staticLayer: + "Add static readonly layer = Layer.effect({{service}}, {{service}}.make), or Layer.scoped when make owns scoped resources.", + classMembers: + "A Context.Service declaration may contain only static readonly layer; keep all behavior inside its make block.", + deepEffectImport: + "Import Context, Effect, Layer, ManagedRuntime, and other Effect modules as named imports from 'effect'; deep Effect imports are forbidden.", + namedEffectImport: + "Use named imports from 'effect', for example import { Context, Effect, Layer } from 'effect'.", + serviceTag: + "Use a stable service tag string containing the service name, for example '@rendotdev/rig/domain/{{service}}'.", + layerSuffix: + "Name this composed layer '{{name}}Layer'. Layer bindings use a descriptive camelCase name with the Layer suffix; each service's owned layer remains static `layer`.", + taggedErrorSuffix: + "Name tagged operational failure '{{name}}Error'. The Error suffix makes the typed failure channel legible to agents and catchTag handlers.", + }, + }, + create(context) { + if (!isEffectPolicyPath(context.filename)) { + return {}; + } + return { + ImportDeclaration(node: ESTree.ImportDeclaration) { + const source = node.source.value; + if (source.startsWith("effect/")) { + context.report({ node: node.source, messageId: "deepEffectImport" }); + return; + } + if (source !== "effect") { + return; + } + const hasNonNamedImport = node.specifiers.some( + (specifier) => specifier.type !== "ImportSpecifier", + ); + if (hasNonNamedImport) { + context.report({ node, messageId: "namedEffectImport" }); + } + }, + ClassDeclaration(node: ESTree.Class) { + const service = inspectContextService(node); + if (service) { + validateEffectService(context, node, service); + return; + } + const name = node.id?.name; + const hasInvalidTaggedErrorName = + name !== undefined && isTaggedErrorClass(node) && !name.endsWith("Error"); + if (hasInvalidTaggedErrorName) { + context.report({ node: node.id ?? node, messageId: "taggedErrorSuffix", data: { name } }); + } + }, + VariableDeclarator(node: ESTree.VariableDeclarator) { + const isNotTopLevelLayer = !isTopLevelDeclaration(node) || !isLayerBinding(node.init); + if (isNotTopLevelLayer) { + return; + } + const name = identifierName(node.id); + const hasInvalidLayerName = name !== undefined && !name.endsWith("Layer"); + if (hasInvalidLayerName) { + context.report({ node: node.id, messageId: "layerSuffix", data: { name } }); + } + }, + }; + }, +}); + +function isLayerBinding(value: unknown) { + const expression = unwrapExpression(value); + if (!expression) { + return false; + } + const directPath = memberPath(expression); + if (directPath?.endsWith(".layer")) { + return true; + } + if (expression.type !== "CallExpression") { + return false; + } + const calleePath = memberPath(expression.callee); + return calleePath?.startsWith("Layer.") === true; +} + +function isTaggedErrorClass(node: ESTree.Class) { + let expression = unwrapExpression(node.superClass); + for (let depth = 0; expression && depth < 4; depth++) { + if (expression.type !== "CallExpression") { + return false; + } + if (memberPath(expression.callee) === "Schema.TaggedErrorClass") { + return true; + } + expression = unwrapExpression(expression.callee); + } + return false; +} diff --git a/src/tooling/oxlint/rules/effect-service-inspector.ts b/src/tooling/oxlint/rules/effect-service-inspector.ts new file mode 100644 index 0000000..b7d00fb --- /dev/null +++ b/src/tooling/oxlint/rules/effect-service-inspector.ts @@ -0,0 +1,408 @@ +import type { Context, ESTree } from "@oxlint/plugins"; +import { + asEffectNode, + callArguments, + callTypeArguments, + identifierName, + isCallTo, + memberPath, + propertyName, + unwrapExpression, + type EffectAstNode, +} from "./effect-ast.ts"; +import { + effectSuccessTypeHasCallableMembers, + lazyEffectExposesCallableContainer, +} from "./effect-service-callable-contract.ts"; +import { returnedAsConstNames } from "./effect-service-return-inspector.ts"; +import { inspectEffectFn, inspectSpannedEffect } from "./effect-service-trace-inspector.ts"; + +type ServiceDescriptor = Readonly<{ + className: string; + outerCall: EffectAstNode; + serviceFactory: EffectAstNode; +}>; + +type ServiceOperation = Readonly<{ + name: string; + kind: "data" | "function" | "lazy"; + callablePayload: boolean; +}>; + +export function inspectContextService(node: ESTree.Class): ServiceDescriptor | undefined { + const className = node.id?.name; + const outerCall = unwrapExpression(node.superClass); + const hasNoServiceCall = !className || outerCall?.type !== "CallExpression"; + if (hasNoServiceCall) { + return undefined; + } + const serviceFactory = unwrapExpression(outerCall.callee); + const isNotContextService = + serviceFactory?.type !== "CallExpression" || + memberPath(serviceFactory.callee) !== "Context.Service"; + if (isNotContextService) { + return undefined; + } + return { className, outerCall, serviceFactory }; +} + +export function validateEffectService( + context: Context, + node: ESTree.Class, + service: ServiceDescriptor, +) { + if (!service.className.endsWith("Service")) { + context.report({ + node: node.id ?? node, + messageId: "serviceSuffix", + data: { name: service.className }, + }); + } + const declaredApi = validateInlineApi(context, service); + const make = findMake(context, service); + if (!make) { + context.report({ node: service.outerCall, messageId: "inlineMake" }); + } else if (make.kind === "generator") { + validateMake(context, service.className, make.callback, declaredApi); + } + validateStaticLayer(context, node, service.className); +} + +function validateInlineApi(context: Context, service: ServiceDescriptor) { + const typeArguments = callTypeArguments(service.serviceFactory); + const selfType = typeArguments[0]; + const apiType = typeArguments[1]; + const selfName = + selfType?.type === "TSTypeReference" + ? identifierName(asEffectNode(selfType.typeName)) + : undefined; + const hasInvalidInlineApi = selfName !== service.className || apiType?.type !== "TSTypeLiteral"; + if (hasInvalidInlineApi) { + context.report({ node: service.serviceFactory, messageId: "inlineApi" }); + return []; + } + + const members = Array.isArray(apiType.members) + ? apiType.members.map(asEffectNode).filter((member) => member !== undefined) + : []; + return members.map((member) => inspectApiMember(context, service.className, member)); +} + +function inspectApiMember(context: Context, serviceName: string, member: EffectAstNode) { + const name = propertyName(member.key) ?? "unknownMethod"; + const typeAnnotation = asEffectNode(member.typeAnnotation); + const functionType = asEffectNode(typeAnnotation?.typeAnnotation); + const source = sourceFor(context, member); + const isEffectType = /\bEffect\.Effect\s* property?.type === "Property" && propertyName(property.key) === "make"); + const makeCall = unwrapExpression(makeProperty?.value); + if (isCallTo(makeCall, "Effect.gen")) { + const [callback] = callArguments(makeCall); + return callback?.type === "FunctionExpression" && callback.generator === true + ? ({ kind: "generator", callback } as const) + : undefined; + } + const isConfigValue = + service.className.endsWith("ConfigService") && + (isCallTo(makeCall, "Effect.succeed") || isCallTo(makeCall, "Effect.sync")); + return isConfigValue ? ({ kind: "config" } as const) : undefined; +} + +function validateMake( + context: Context, + serviceName: string, + callback: EffectAstNode, + declaredApi: readonly ServiceOperation[], +) { + const body = asEffectNode(callback.body); + const statements = Array.isArray(body?.body) + ? body.body.map(asEffectNode).filter((statement) => statement !== undefined) + : []; + const implementations = collectImplementations(context, serviceName, statements); + validateReturnedApi(context, callback, body, statements, declaredApi, implementations); +} + +function collectImplementations( + context: Context, + serviceName: string, + statements: readonly EffectAstNode[], +) { + const effectFns = new Set(); + const lazyEffects = new Set(); + const callableContainers = new Set(); + const localCallables = new Set(); + for (const statement of statements) { + const hasNoVariableDeclarations = + statement.type !== "VariableDeclaration" || !Array.isArray(statement.declarations); + if (hasNoVariableDeclarations) { + continue; + } + const declarations = statement.declarations as unknown[]; + for (const declaration of declarations + .map(asEffectNode) + .filter((entry) => entry !== undefined)) { + const name = identifierName(declaration.id); + const init = unwrapExpression(declaration.init); + const isLocalCallable = + name !== undefined && + (init?.type === "ArrowFunctionExpression" || init?.type === "FunctionExpression"); + if (isLocalCallable) { + localCallables.add(name); + } + } + } + for (const statement of statements) { + const hasNoVariableDeclarations = + statement.type !== "VariableDeclaration" || !Array.isArray(statement.declarations); + if (hasNoVariableDeclarations) { + continue; + } + const declarations = statement.declarations as unknown[]; + for (const declaration of declarations + .map(asEffectNode) + .filter((entry) => entry !== undefined)) { + const name = identifierName(declaration.id); + const init = unwrapExpression(declaration.init); + const isCallableBinding = + init?.type === "ArrowFunctionExpression" || + init?.type === "FunctionExpression" || + isCallTo(init, "Effect.gen") || + inspectEffectFn(init) !== undefined || + inspectSpannedEffect(init) !== undefined; + const hasMutableCallableBinding = + name !== undefined && statement.kind !== "const" && isCallableBinding; + if (hasMutableCallableBinding) { + context.report({ node: declaration, messageId: "localConst", data: { name } }); + } + inspectImplementation( + context, + serviceName, + declaration, + effectFns, + lazyEffects, + callableContainers, + localCallables, + ); + } + } + return { effectFns, lazyEffects, callableContainers } as const; +} + +function inspectImplementation( + context: Context, + serviceName: string, + declaration: EffectAstNode, + effectFns: Set, + lazyEffects: Set, + callableContainers: Set, + localCallables: ReadonlySet, +) { + const name = identifierName(declaration.id); + const init = unwrapExpression(declaration.init); + const hasNoNamedImplementation = !name || !init; + if (hasNoNamedImplementation) { + return; + } + if (init.type === "FunctionExpression") { + context.report({ node: declaration, messageId: "pureHelper", data: { name } }); + return; + } + if (isCallTo(init, "Effect.gen")) { + context.report({ + node: declaration, + messageId: "effectHelper", + data: { name, service: serviceName }, + }); + return; + } + const lazyEffect = inspectSpannedEffect(init); + if (lazyEffect) { + lazyEffects.add(name); + if (lazyEffectExposesCallableContainer(init, localCallables)) { + callableContainers.add(name); + } + validateTraceName(context, lazyEffect, serviceName, name); + return; + } + const effectFn = inspectEffectFn(init); + if (!effectFn) { + return; + } + effectFns.add(name); + validateTraceName(context, effectFn, serviceName, name); + if (!effectFn.generator) { + context.report({ + node: declaration, + messageId: "effectFnGenerator", + data: { name, service: serviceName }, + }); + } +} + +function validateTraceName( + context: Context, + trace: Readonly<{ label?: string; labelNode: EffectAstNode }>, + serviceName: string, + operationName: string, +) { + const expected = `${serviceName}.${operationName}`; + if (trace.label !== expected) { + context.report({ node: trace.labelNode, messageId: "effectFnName", data: { expected } }); + } +} + +function validateReturnedApi( + context: Context, + callback: EffectAstNode, + body: EffectAstNode | undefined, + statements: readonly EffectAstNode[], + declaredApi: readonly ServiceOperation[], + implementations: Readonly<{ + effectFns: Set; + lazyEffects: Set; + callableContainers: Set; + }>, +) { + const returnStatement = statements.findLast((statement) => statement.type === "ReturnStatement"); + const returned = returnedAsConstNames(returnStatement); + if (!returned) { + context.report({ node: returnStatement ?? body ?? callback, messageId: "returnedApi" }); + return; + } + for (const operation of declaredApi) { + if (operation.kind === "data") { + continue; + } + const implementationSet = + operation.kind === "function" ? implementations.effectFns : implementations.lazyEffects; + const hasInvalidPublicMethod = + !implementationSet.has(operation.name) && returned.includes(operation.name); + if (hasInvalidPublicMethod) { + context.report({ + node: returnStatement ?? callback, + messageId: "publicMethod", + data: { name: operation.name }, + }); + } + const exposesCallableLazyValue = + operation.kind === "lazy" && + !operation.callablePayload && + implementations.callableContainers.has(operation.name) && + returned.includes(operation.name); + if (exposesCallableLazyValue) { + context.report({ + node: returnStatement ?? callback, + messageId: "callableEffectValue", + data: { name: operation.name }, + }); + } + } + reportApiMismatch(context, returnStatement ?? callback, declaredApi, returned); +} + +function reportApiMismatch( + context: Context, + node: EffectAstNode, + declaredApi: readonly ServiceOperation[], + returned: readonly string[], +) { + const declared = declaredApi.map(({ name }) => name).toSorted(); + const actual = [...returned].toSorted(); + if (declared.join("\0") !== actual.join("\0")) { + context.report({ + node, + messageId: "apiMismatch", + data: { + declared: declared.join(", ") || "none", + returned: actual.join(", ") || "none", + }, + }); + } +} + +function validateStaticLayer(context: Context, node: ESTree.Class, serviceName: string) { + const members = node.body.body.map(asEffectNode).filter((member) => member !== undefined); + const layer = members.find((member) => propertyName(member.key) === "layer"); + const value = unwrapExpression(layer?.value); + const [serviceArgument, makeArgument] = callArguments(value); + const hasExactLayer = + layer?.type === "PropertyDefinition" && + layer.static === true && + layer.readonly === true && + (isCallTo(value, "Layer.effect") || isCallTo(value, "Layer.scoped")) && + identifierName(serviceArgument) === serviceName && + memberPath(makeArgument) === `${serviceName}.make`; + if (!hasExactLayer) { + context.report({ + node: layer ?? node.body, + messageId: "staticLayer", + data: { service: serviceName }, + }); + } + if (members.some((member) => member !== layer)) { + context.report({ node: node.body, messageId: "classMembers" }); + } +} + +function sourceFor(context: Context, node: EffectAstNode) { + const range = node.range; + return Array.isArray(range) + ? context.sourceCode.text.slice(range[0] as number, range[1] as number) + : ""; +} diff --git a/src/tooling/oxlint/rules/effect-service-return-inspector.ts b/src/tooling/oxlint/rules/effect-service-return-inspector.ts new file mode 100644 index 0000000..e46fe7a --- /dev/null +++ b/src/tooling/oxlint/rules/effect-service-return-inspector.ts @@ -0,0 +1,34 @@ +import { + asEffectNode, + identifierName, + propertyName, + unwrapExpression, + type EffectAstNode, +} from "./effect-ast.ts"; + +export function returnedAsConstNames(returnStatement: EffectAstNode | undefined) { + const argument = asEffectNode(returnStatement?.argument); + const assertion = + argument?.type === "TSAsExpression" ? asEffectNode(argument.typeAnnotation) : undefined; + const isConstAssertion = + (assertion?.type === "TSTypeReference" && identifierName(assertion.typeName) === "const") || + assertion?.type === "TSConstKeyword"; + const object = + argument?.type === "TSAsExpression" ? unwrapExpression(argument.expression) : undefined; + const hasInvalidReturnedObject = + !isConstAssertion || object?.type !== "ObjectExpression" || !Array.isArray(object.properties); + if (hasInvalidReturnedObject) { + return undefined; + } + const properties = object.properties as unknown[]; + const names: string[] = []; + for (const property of properties.map(asEffectNode).filter((entry) => entry !== undefined)) { + const name = property.type === "Property" ? propertyName(property.key) : undefined; + const hasInvalidReturnedProperty = !name || identifierName(property.value) !== name; + if (hasInvalidReturnedProperty) { + return undefined; + } + names.push(name); + } + return names; +} diff --git a/src/tooling/oxlint/rules/effect-service-trace-inspector.ts b/src/tooling/oxlint/rules/effect-service-trace-inspector.ts new file mode 100644 index 0000000..f0b49ed --- /dev/null +++ b/src/tooling/oxlint/rules/effect-service-trace-inspector.ts @@ -0,0 +1,44 @@ +import { + callArguments, + isCallTo, + propertyName, + unwrapExpression, + type EffectAstNode, +} from "./effect-ast.ts"; + +export function inspectEffectFn(value: unknown) { + const outerCall = unwrapExpression(value); + if (outerCall?.type !== "CallExpression") return undefined; + const factoryCall = unwrapExpression(outerCall.callee); + if (!isCallTo(factoryCall, "Effect.fn")) return undefined; + const [labelNode] = callArguments(factoryCall); + const label = + labelNode?.type === "Literal" && typeof labelNode.value === "string" + ? labelNode.value + : undefined; + const [implementation] = callArguments(outerCall); + return { + label, + labelNode: labelNode ?? factoryCall ?? outerCall, + generator: implementation?.type === "FunctionExpression" && implementation.generator === true, + } as const; +} + +export function inspectSpannedEffect(value: unknown) { + const pipeCall = unwrapExpression(value); + const callee = + pipeCall?.type === "CallExpression" ? unwrapExpression(pipeCall.callee) : undefined; + const isPipe = + callee?.type === "MemberExpression" && + callee.computed !== true && + propertyName(callee.property) === "pipe"; + if (!isPipe) return undefined; + const span = callArguments(pipeCall).find((argument) => isCallTo(argument, "Effect.withSpan")); + if (!span) return undefined; + const [labelNode] = callArguments(span); + const label = + labelNode?.type === "Literal" && typeof labelNode.value === "string" + ? labelNode.value + : undefined; + return { label, labelNode: (labelNode ?? span) as EffectAstNode } as const; +} diff --git a/src/tooling/oxlint/rules/layer-boundaries.ts b/src/tooling/oxlint/rules/layer-boundaries.ts index 0e6f52e..f598d70 100644 --- a/src/tooling/oxlint/rules/layer-boundaries.ts +++ b/src/tooling/oxlint/rules/layer-boundaries.ts @@ -2,8 +2,7 @@ import { defineDependencyRule, isPublicApiViolation } from "./dependency.ts"; export const layerBoundariesRule = defineDependencyRule({ description: "Enforce the layered domain dependency graph and top-level source boundaries.", - message: - "This dependency crosses a forbidden architecture edge. Depend only on earlier allowed layers, or move orchestration to runtime/app. See ARCHITECTURE.md.", + message: "{{source}} cannot import {{target}}. {{remediation}} See ARCHITECTURE.md.", shouldReport(decision) { return !decision.allowed && !isPublicApiViolation(decision); }, diff --git a/src/tooling/oxlint/rules/no-barrel-files.ts b/src/tooling/oxlint/rules/no-barrel-files.ts new file mode 100644 index 0000000..1648816 --- /dev/null +++ b/src/tooling/oxlint/rules/no-barrel-files.ts @@ -0,0 +1,44 @@ +import type { ESTree } from "@oxlint/plugins"; +import { defineRule } from "@oxlint/plugins"; +import { sourcePathKey } from "./effect-ast.ts"; + +export const noBarrelFilesRule = defineRule({ + meta: { + type: "problem", + docs: { description: "Forbid index modules and re-export facades across Rig." }, + messages: { + indexFile: + "Barrel files are forbidden. Rename this module for its concrete responsibility and import it directly.", + reExport: + "Re-export facades are forbidden. Import the concrete module at each call site instead.", + barrelImport: + "Import the concrete module directly; paths resolving to index.ts or index.tsx are forbidden.", + }, + }, + create(context) { + return { + Program(node) { + if (/(?:^|\/)index\.[cm]?[jt]sx?$/u.test(sourcePathKey(context.filename))) { + context.report({ node, messageId: "indexFile" }); + } + }, + ImportDeclaration(node: ESTree.ImportDeclaration) { + if (isIndexSpecifier(node.source.value)) { + context.report({ node: node.source, messageId: "barrelImport" }); + } + }, + ExportAllDeclaration(node: ESTree.ExportAllDeclaration) { + context.report({ node, messageId: "reExport" }); + }, + ExportNamedDeclaration(node: ESTree.ExportNamedDeclaration) { + if (node.source) { + context.report({ node, messageId: "reExport" }); + } + }, + }; + }, +}); + +function isIndexSpecifier(specifier: string) { + return /(?:^|\/)index(?:\.[cm]?[jt]sx?)?$/u.test(specifier); +} diff --git a/src/tooling/oxlint/rules/no-floating-behavior.ts b/src/tooling/oxlint/rules/no-floating-behavior.ts new file mode 100644 index 0000000..bd04cce --- /dev/null +++ b/src/tooling/oxlint/rules/no-floating-behavior.ts @@ -0,0 +1,175 @@ +import type { ESTree } from "@oxlint/plugins"; +import { defineRule } from "@oxlint/plugins"; +import { + asEffectNode, + identifierName, + isCallTo, + isEffectPolicyPath, + isTopLevelDeclaration, + memberPath, + sourcePathKey, + unwrapExpression, +} from "./effect-ast.ts"; + +const compatibilityCallableNames = new Set([ + "createRigToolKit", + "defineTool", + "main", + "program", + "runCli", + "runCliMain", +]); + +export const noFloatingBehaviorRule = defineRule({ + meta: { + type: "problem", + docs: { + description: + "Keep production behavior inside Effect Context.Service implementations while allowing declarative language and framework constructs.", + }, + messages: { + floatingFunction: + "Move '{{name}}' into a Context.Service make block. Use a local const arrow for pure private logic, or Effect.fn(\"ServiceName.{{name}}\") for effectful logic.", + floatingCallable: + "Move the callable '{{name}}' into a Context.Service make block and expose it through the service's returned readonly API.", + behavioralClass: + "Replace '{{name}}' with Context.Service for behavior, or Schema.Class, Schema.TaggedClass, Schema.ErrorClass, or Schema.TaggedErrorClass for data and typed errors.", + floatingConstruction: + "Move module-level construction of '{{name}}' into a Context.Service make block so the resource is replaceable and its lifetime is owned by a Layer.", + functionExpression: + "Use a const arrow for pure private logic. Function expressions are reserved for generator callbacks passed directly to Effect.gen or Effect.fn.", + }, + }, + create(context) { + if (!isEffectPolicyPath(context.filename)) { + return {}; + } + const filePath = sourcePathKey(context.filename); + + return { + FunctionDeclaration(node: ESTree.Function) { + const name = node.id?.name ?? "anonymousFunction"; + const isAllowedFunction = + isAllowedReactDeclaration(filePath, name) || isCompatibilityCallable(filePath, name); + if (isAllowedFunction) { + return; + } + context.report({ node, messageId: "floatingFunction", data: { name } }); + }, + FunctionExpression(node: ESTree.Function) { + if (isEffectGeneratorCallback(node)) { + return; + } + context.report({ node, messageId: "functionExpression" }); + }, + VariableDeclarator(node: ESTree.VariableDeclarator) { + if (!isTopLevelDeclaration(node)) { + return; + } + const name = identifierName(node.id) ?? "anonymousCallable"; + const isAllowedCallable = + isAllowedReactDeclaration(filePath, name) || isCompatibilityCallable(filePath, name); + if (isAllowedCallable) { + return; + } + const init = unwrapExpression(node.init); + const isDirectCallable = + init?.type === "ArrowFunctionExpression" || + init?.type === "FunctionExpression" || + isEffectFnCall(init); + if (isDirectCallable) { + context.report({ node, messageId: "floatingCallable", data: { name } }); + } + if (init?.type === "NewExpression") { + context.report({ node, messageId: "floatingConstruction", data: { name } }); + } + }, + ClassDeclaration(node: ESTree.Class) { + const name = node.id?.name ?? "AnonymousClass"; + const isAllowedClass = isContextService(node) || isSchemaClass(node); + if (isAllowedClass) { + return; + } + context.report({ node, messageId: "behavioralClass", data: { name } }); + }, + ClassExpression(node: ESTree.Class) { + const name = node.id?.name ?? "AnonymousClass"; + const isAllowedClass = isContextService(node) || isSchemaClass(node); + if (isAllowedClass) { + return; + } + context.report({ node, messageId: "behavioralClass", data: { name } }); + }, + }; + }, +}); + +function isEffectGeneratorCallback(node: ESTree.Function) { + const parent = asEffectNode(node.parent); + if (parent?.type !== "CallExpression") { + return false; + } + if (isCallTo(parent, "Effect.gen")) { + return node.generator; + } + const innerCall = asEffectNode(parent.callee); + return innerCall?.type === "CallExpression" && isCallTo(innerCall, "Effect.fn") && node.generator; +} + +function isContextService(node: ESTree.Class) { + const outerCall = unwrapExpression(node.superClass); + if (outerCall?.type !== "CallExpression") { + return false; + } + const serviceFactory = unwrapExpression(outerCall.callee); + return ( + serviceFactory?.type === "CallExpression" && + memberPath(serviceFactory.callee) === "Context.Service" + ); +} + +function isSchemaClass(node: ESTree.Class) { + let expression = unwrapExpression(node.superClass); + for (let depth = 0; expression && depth < 4; depth++) { + if (expression.type !== "CallExpression") { + return false; + } + const path = memberPath(expression.callee); + const isSchemaClassFactory = + path === "Schema.Class" || + path === "Schema.TaggedClass" || + path === "Schema.ErrorClass" || + path === "Schema.TaggedErrorClass"; + if (isSchemaClassFactory) { + return true; + } + expression = unwrapExpression(expression.callee); + } + return false; +} + +function isEffectFnCall(value: unknown) { + const outerCall = unwrapExpression(value); + if (outerCall?.type !== "CallExpression") { + return false; + } + return isCallTo(outerCall.callee, "Effect.fn"); +} + +function isAllowedReactDeclaration(filePath: string, name: string) { + const isReactModule = filePath.endsWith(".tsx"); + return isReactModule && (/^[A-Z]/u.test(name) || /^use[A-Z]/u.test(name)); +} + +function isCompatibilityCallable(filePath: string, name: string) { + const isSdkModule = filePath === "src/domains/tools/runtime/tool-sdk.ts"; + const isCliBoundary = + filePath === "src/app/cli/entrypoint.ts" || + filePath === "src/app/cli/composition-root.ts" || + filePath === "src/app/cli/runtime-bootstrap.ts"; + const isScriptEntrypoint = /^scripts\/(?:bench|release|smoke)\.ts$/u.test(filePath); + return ( + (isSdkModule && (name.startsWith("define") || name === "createRigToolKit")) || + ((isCliBoundary || isScriptEntrypoint) && compatibilityCallableNames.has(name)) + ); +} diff --git a/src/tooling/oxlint/rules/source-location.ts b/src/tooling/oxlint/rules/source-location.ts index 4721f06..738181b 100644 --- a/src/tooling/oxlint/rules/source-location.ts +++ b/src/tooling/oxlint/rules/source-location.ts @@ -10,8 +10,7 @@ export const sourceLocationRule = defineRule({ "Use one of Rig's configured business domains. Update the shared architecture model only when introducing a durable business capability.", invalidLayer: "Place this file in types, config, repo, service, runtime, or ui according to its responsibility.", - invalidLocation: - "Place production code under app, domains, providers, utils, or tooling. Keep define.ts at the source root.", + invalidLocation: "Place production code under app, domains, providers, utils, or tooling.", }, }, create(context) { diff --git a/src/tools/cache.ts b/src/tools/cache.ts deleted file mode 100644 index 47d4935..0000000 --- a/src/tools/cache.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../persistence/cache/tool-cache"; diff --git a/src/tools/collection-field-path.ts b/src/tools/collection-field-path.ts deleted file mode 100644 index c27d58c..0000000 --- a/src/tools/collection-field-path.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../collections/domain/field-path"; diff --git a/src/tools/collection-memory-index.ts b/src/tools/collection-memory-index.ts deleted file mode 100644 index 1a0da06..0000000 --- a/src/tools/collection-memory-index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../collections/persistence/memory-index"; diff --git a/src/tools/collection.test.ts b/src/tools/collection.test.ts deleted file mode 100644 index 85411ed..0000000 --- a/src/tools/collection.test.ts +++ /dev/null @@ -1,1037 +0,0 @@ -import { existsSync } from "node:fs"; -import { spawnSync } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test"; -import { z } from "zod"; -import { - CollectionHandleImplClass, - FrontmatterCodecClass, - ToolCollectionServiceClass, - type CollectionHandleFactory, - type ManagedCollectionHandle, -} from "./collection"; -import { MemoryCollectionIndexClass } from "./collection-memory-index"; - -class BunCollectionIntegrationRunner { - async run(collectionPath: string, body: string): Promise> { - const scriptPath = join(collectionPath, `integration-${randomUUID()}.ts`); - const collectionModule = pathToFileURL(join(process.cwd(), "src/tools/collection.ts")).href; - const zodModule = pathToFileURL(join(process.cwd(), "node_modules/zod/index.js")).href; - await writeFile( - scriptPath, - `import { Database } from "bun:sqlite"; -import { stat, unlink, utimes, writeFile } from "node:fs/promises"; -import { join } from "node:path"; -import { CollectionHandleImplClass } from ${JSON.stringify(collectionModule)}; -import { z } from ${JSON.stringify(zodModule)}; - -const dir = ${JSON.stringify(collectionPath)}; -${body} -`, - "utf8", - ); - const result = spawnSync("bun", [scriptPath], { - cwd: process.cwd(), - encoding: "utf8", - env: { ...process.env, RIG_LOG: "0" }, - }); - if (result.status !== 0) throw new Error(result.stderr || result.stdout); - return JSON.parse(result.stdout.trim()) as Record; - } -} - -class TrackingMemoryCollectionIndex extends MemoryCollectionIndexClass { - upsertCalls = 0; - - override upsertDoc(...args: Parameters): void { - this.upsertCalls++; - super.upsertDoc(...args); - } -} - -describe("CollectionHandle", () => { - let dir: string; - let handle: CollectionHandleImplClass; - - const schema = z.object({ - ticket: z.string(), - summary: z.string(), - status: z.enum(["open", "in-progress", "done"]).default("open"), - priority: z.number().optional(), - }); - - beforeEach(async () => { - dir = await mkdtemp(join(tmpdir(), "rig-collection-test-")); - handle = new CollectionHandleImplClass( - "test-collection", - dir, - { schema, generateId: (data: any) => data.ticket }, - new MemoryCollectionIndexClass(), - ); - await handle.init(); - }); - - afterEach(async () => { - handle.close(); - await rm(dir, { recursive: true, force: true }); - }); - - it("creates an entry and reads it back", async () => { - const entry = await handle.create({ - data: { ticket: "CONSOLE-100", summary: "Fix bug", status: "open" }, - body: "## Notes\nSome content", - }); - - expect(entry.id).toBe("CONSOLE-100"); - expect(entry.data.ticket).toBe("CONSOLE-100"); - expect(entry.data.summary).toBe("Fix bug"); - expect(entry.body).toBe("## Notes\nSome content"); - - const fetched = await handle.getEntry("CONSOLE-100"); - expect(fetched).not.toBeNull(); - expect(fetched!.id).toBe("CONSOLE-100"); - expect(fetched!.data.summary).toBe("Fix bug"); - }); - - it("uses generateId from data", async () => { - const entry = await handle.create({ - data: { ticket: "CONSOLE-200", summary: "Another bug", status: "open" }, - }); - expect(entry.id).toBe("CONSOLE-200"); - }); - - it("allows explicit id override", async () => { - const entry = await handle.create({ - id: "custom-id", - data: { ticket: "CONSOLE-300", summary: "Custom", status: "open" }, - }); - expect(entry.id).toBe("custom-id"); - }); - - it("rejects entry ids that could escape the collection", async () => { - const invalidIds = ["", ".", "..", "../escaped", "/tmp/escaped", "nested/id", "nested\\id"]; - const outsidePath = join(dir, "..", "escaped.md"); - - await Promise.all( - invalidIds.map(async (id) => { - await expect( - handle.create({ - id, - data: { ticket: "SAFE-1", summary: "Unsafe id", status: "open" }, - }), - ).rejects.toThrow("Invalid collection entry id"); - await expect(handle.getEntry(id)).rejects.toThrow("Invalid collection entry id"); - await expect(handle.update(id, { body: "unsafe" })).rejects.toThrow( - "Invalid collection entry id", - ); - await expect( - handle.upsert({ - id, - data: { ticket: "SAFE-1", summary: "Unsafe id", status: "open" }, - }), - ).rejects.toThrow("Invalid collection entry id"); - await expect(handle.remove(id)).rejects.toThrow("Invalid collection entry id"); - }), - ); - - expect(existsSync(outsidePath)).toBe(false); - }); - - it("validates ids returned by generateId", async () => { - await expect( - handle.create({ - data: { ticket: "../generated-escape", summary: "Unsafe generated id", status: "open" }, - }), - ).rejects.toThrow("Invalid collection entry id"); - }); - - it("throws on duplicate create", async () => { - await handle.create({ - data: { ticket: "CONSOLE-100", summary: "First", status: "open" }, - }); - await expect( - handle.create({ - data: { ticket: "CONSOLE-100", summary: "Duplicate", status: "open" }, - }), - ).rejects.toThrow("already exists"); - }); - - it("validates data against schema", async () => { - await expect( - handle.create({ - id: "bad", - data: { ticket: 123, summary: "Bad" } as any, - }), - ).rejects.toThrow("invalid"); - }); - - it("updates entry data (merge) and body", async () => { - await handle.create({ - data: { ticket: "CONSOLE-100", summary: "Original", status: "open", priority: 1 }, - body: "Original body", - }); - - const updated = await handle.update("CONSOLE-100", { - data: { status: "done" }, - body: "Updated body", - }); - - expect(updated.data.status).toBe("done"); - expect(updated.data.summary).toBe("Original"); // preserved - expect(updated.data.priority).toBe(1); // preserved - expect(updated.body).toBe("Updated body"); - }); - - it("removes an entry", async () => { - await handle.create({ - data: { ticket: "CONSOLE-100", summary: "To delete", status: "open" }, - }); - - const removed = await handle.remove("CONSOLE-100"); - expect(removed).toBe(true); - - const fetched = await handle.getEntry("CONSOLE-100"); - expect(fetched).toBeNull(); - }); - - it("remove returns false for non-existent", async () => { - const removed = await handle.remove("non-existent"); - expect(removed).toBe(false); - }); - - it("upserts: creates if missing, updates if exists", async () => { - const r1 = await handle.upsert({ - id: "CONSOLE-100", - data: { ticket: "CONSOLE-100", summary: "Created", status: "open" }, - }); - expect(r1.created).toBe(true); - - const r2 = await handle.upsert({ - id: "CONSOLE-100", - data: { ticket: "CONSOLE-100", summary: "Updated", status: "done" }, - }); - expect(r2.created).toBe(false); - - const entry = await handle.getEntry("CONSOLE-100"); - expect(entry!.data.summary).toBe("Updated"); - expect(entry!.data.status).toBe("done"); - }); - - it("lists entries with where filter", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); - await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); - await handle.create({ data: { ticket: "A-3", summary: "Three", status: "open" } }); - - const { entries, total } = await handle.list({ where: { status: "open" } }); - expect(total).toBe(2); - expect(entries.map((e) => e.id).toSorted()).toEqual(["A-1", "A-3"]); - }); - - it("lists entries with sort", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open", priority: 3 } }); - await handle.create({ data: { ticket: "A-2", summary: "Two", status: "open", priority: 1 } }); - await handle.create({ data: { ticket: "A-3", summary: "Three", status: "open", priority: 2 } }); - - const { entries } = await handle.list({ sort: "priority" }); - expect(entries.map((e) => e.id)).toEqual(["A-2", "A-3", "A-1"]); - }); - - it("filters and sorts using valid nested field paths", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ id: "a", data: { project: { state: "open", priority: 2 } } }); - await handle.create({ id: "b", data: { project: { state: "done", priority: 1 } } }); - await handle.create({ id: "c", data: { project: { state: "open", priority: 3 } } }); - - const { entries, total } = await handle.list({ - where: { "project.state": "open" }, - sort: "-project.priority", - }); - - expect(total).toBe(2); - expect(entries.map((entry) => entry.id)).toEqual(["c", "a"]); - expect(await handle.count({ "project.state": "open" })).toBe(2); - }); - - it("rejects malformed where and sort field paths", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); - - await expect(handle.list({ where: { "status') OR 1=1 --": "open" } })).rejects.toThrow( - "Invalid collection field path", - ); - await expect(handle.list({ sort: "status; DELETE FROM docs" })).rejects.toThrow( - "Invalid collection field path", - ); - await expect(handle.count({ "project..status": "open" })).rejects.toThrow( - "Invalid collection field path", - ); - }); - - it("searches entries via FTS", async () => { - await handle.create({ - data: { ticket: "A-1", summary: "Payment retry", status: "open" }, - body: "The payment retry logic has a bug in the exponential backoff.", - }); - await handle.create({ - data: { ticket: "A-2", summary: "UI alignment", status: "open" }, - body: "The button is misaligned on mobile.", - }); - - const { entries } = await handle.search("payment retry"); - expect(entries.length).toBeGreaterThanOrEqual(1); - expect(entries[0]!.id).toBe("A-1"); - expect(entries[0]!.snippet).toBeTruthy(); - }); - - it("counts entries", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); - await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); - - expect(await handle.count()).toBe(2); - expect(await handle.count({ status: "open" })).toBe(1); - }); - - it("getCollection with filter", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); - await handle.create({ data: { ticket: "A-2", summary: "Two", status: "done" } }); - - const all = await handle.getCollection(); - expect(all.length).toBe(2); - - const open = await handle.getCollection((e) => e.data.status === "open"); - expect(open.length).toBe(1); - expect(open[0]!.id).toBe("A-1"); - }); - - it("reconciles hand-edited files on next init", async () => { - await handle.create({ - data: { ticket: "A-1", summary: "Original", status: "open" }, - body: "Original body", - }); - handle.close(); - - // Hand-edit the file - const filePath = join(dir, "A-1.md"); - await writeFile( - filePath, - "---\nticket: A-1\nsummary: Hand-edited\nstatus: done\n---\n\nEdited by hand\n", - ); - - // Re-init - handle = new CollectionHandleImplClass( - "test-collection", - dir, - { schema, generateId: (data: any) => data.ticket }, - new MemoryCollectionIndexClass(), - ); - await handle.init(); - - const entry = await handle.getEntry("A-1"); - expect(entry!.data.summary).toBe("Hand-edited"); - expect(entry!.data.status).toBe("done"); - expect(entry!.body).toBe("Edited by hand\n"); - }); - - it("clears all entries", async () => { - await handle.create({ data: { ticket: "A-1", summary: "One", status: "open" } }); - await handle.create({ data: { ticket: "A-2", summary: "Two", status: "open" } }); - - await handle.clear(); - expect(await handle.count()).toBe(0); - expect(await handle.getEntry("A-1")).toBeNull(); - }); -}); - -describe("CollectionHandle edge cases", () => { - let dir: string; - let handle: CollectionHandleImplClass; - - beforeEach(async () => { - dir = await mkdtemp(join(tmpdir(), "rig-collection-edge-")); - }); - - afterEach(async () => { - handle?.close(); - await rm(dir, { recursive: true, force: true }); - }); - - it("derives id from common fields when no generateId", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - const entry = await handle.create({ - data: { title: "My Great Note", content: "hello" }, - }); - expect(entry.id).toBe("my-great-note"); - }); - - it("derives id from slug field", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - const entry = await handle.create({ - data: { slug: "custom-slug", content: "hello" }, - }); - expect(entry.id).toBe("custom-slug"); - }); - - it("derives id from id field", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - const entry = await handle.create({ - data: { id: "my-id", content: "hello" }, - }); - expect(entry.id).toBe("my-id"); - }); - - it("throws when no id can be derived", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await expect(handle.create({ data: { count: 42 } })).rejects.toThrow("needs an id"); - }); - - it("throws on update of non-existent entry", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await expect(handle.update("ghost", { data: { x: 1 } })).rejects.toThrow("not found"); - }); - - it("handles complex frontmatter with nested objects and arrays", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ - id: "complex", - data: { - title: "Complex entry", - tags: ["a", "b", "c"], - prs: [ - { number: 123, url: "https://example.com", state: "OPEN" }, - { number: 456, url: "https://example.com/2", state: "MERGED" }, - ], - nested: { foo: "bar", baz: 42 }, - empty_arr: [], - flag: true, - nothing: null, - priority: 3, - }, - body: "## Content\nWith special chars: {foo} [bar] *baz*", - }); - - const fetched = await handle.getEntry("complex"); - expect(fetched!.data.title).toBe("Complex entry"); - expect(fetched!.data.tags).toEqual(["a", "b", "c"]); - expect(fetched!.data.prs).toHaveLength(2); - expect(fetched!.data.prs[0].number).toBe(123); - expect(fetched!.data.nested.foo).toBe("bar"); - expect(fetched!.data.empty_arr).toEqual([]); - expect(fetched!.data.flag).toBe(true); - expect(fetched!.data.nothing).toBeNull(); - expect(fetched!.data.priority).toBe(3); - }); - - it("handles frontmatter with quoted strings and special values", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ - id: "special", - data: { - url: "https://example.com/path?q=1&b=2", - description: "Has: colon in value", - }, - body: "", - }); - const fetched = await handle.getEntry("special"); - expect(fetched!.data.url).toBe("https://example.com/path?q=1&b=2"); - expect(fetched!.data.description).toBe("Has: colon in value"); - }); - - it("update with only body preserves data", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ id: "note1", data: { title: "Original" }, body: "old" }); - const updated = await handle.update("note1", { body: "new body" }); - expect(updated.data.title).toBe("Original"); - expect(updated.body).toBe("new body"); - }); - - it("list with offset and descending sort", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ id: "a", data: { title: "A", rank: 1 } }); - await handle.create({ id: "b", data: { title: "B", rank: 2 } }); - await handle.create({ id: "c", data: { title: "C", rank: 3 } }); - - const { entries } = await handle.list({ sort: "-rank", limit: 2, offset: 1 }); - expect(entries.map((e) => e.id)).toEqual(["b", "a"]); - }); - - it("search returns fallback snippet when no line matches", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ - id: "note1", - data: { title: "searchable" }, - body: "This body has no matching words for the search term.", - }); - // The memory index matches on title in data_json - const { entries } = await handle.search("searchable"); - expect(entries.length).toBe(1); - expect(entries[0]!.snippet).toBeTruthy(); - }); - - it("reconcile removes index entries for deleted files", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - await handle.create({ id: "keep", data: { title: "Keep" } }); - await handle.create({ id: "delete-me", data: { title: "Delete" } }); - handle.close(); - - // Delete the file manually - const { unlink: unlinkFile } = await import("node:fs/promises"); - await unlinkFile(join(dir, "delete-me.md")); - - // Re-init with fresh index - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - - expect(await handle.getEntry("keep")).not.toBeNull(); - // delete-me.md is gone, so getEntry reads from disk and fails - expect(await handle.getEntry("delete-me")).toBeNull(); - }); - - it("handles content without frontmatter", async () => { - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - - // Write a raw file without frontmatter - await writeFile(join(dir, "raw.md"), "Just plain content\n"); - - // Re-init to trigger reconcile - handle.close(); - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - - const entry = await handle.getEntry("raw"); - expect(entry!.data).toEqual({}); - expect(entry!.body).toBe("Just plain content\n"); - }); - - it("uses the fingerprint fast path for unchanged files", async () => { - const index = new TrackingMemoryCollectionIndex(); - handle = new CollectionHandleImplClass("notes", dir, {}, index); - await handle.init(); - await handle.create({ id: "stable", data: { title: "Stable" }, body: "unchanged" }); - expect(index.upsertCalls).toBe(1); - handle.close(); - - handle = new CollectionHandleImplClass("notes", dir, {}, index); - await handle.init(); - - expect(index.upsertCalls).toBe(1); - expect((await handle.list()).entries).toHaveLength(1); - }); -}); - -describe("CollectionHandle (schema-less)", () => { - let dir: string; - let handle: CollectionHandleImplClass; - - beforeEach(async () => { - dir = await mkdtemp(join(tmpdir(), "rig-collection-schemaless-")); - handle = new CollectionHandleImplClass("notes", dir, {}, new MemoryCollectionIndexClass()); - await handle.init(); - }); - - afterEach(async () => { - handle.close(); - await rm(dir, { recursive: true, force: true }); - }); - - it("creates and reads without schema validation", async () => { - const entry = await handle.create({ - id: "my-note", - data: { anything: "goes", nested: { foo: "bar" } }, - body: "Free-form content", - }); - - expect(entry.id).toBe("my-note"); - const fetched = await handle.getEntry("my-note"); - expect(fetched!.data.anything).toBe("goes"); - }); - - it("searches schema-less entries", async () => { - await handle.create({ - id: "note-1", - data: { topic: "deployment" }, - body: "We deployed the new service to production today.", - }); - - const { entries } = await handle.search("deployed production"); - expect(entries.length).toBe(1); - }); -}); - -describe("CollectionHandle real Bun SQLite reconciliation", () => { - const runner = new BunCollectionIntegrationRunner(); - - it("filters and sorts nested paths through SQLite without interpolating unsafe paths", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-query-")); - try { - const result = await runner.run( - integrationDir, - `const handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -await handle.create({ id: "a", data: { project: { state: "open", priority: 2 } } }); -await handle.create({ id: "b", data: { project: { state: "done", priority: 1 } } }); -await handle.create({ id: "c", data: { project: { state: "open", priority: 3 } } }); -const listed = await handle.list({ where: { "project.state": "open" }, sort: "-project.priority" }); -const count = await handle.count({ "project.state": "open" }); -let invalidMessage = ""; -try { - await handle.list({ where: { "state') OR 1=1 --": "open" } }); -} catch (error) { - invalidMessage = error instanceof Error ? error.message : String(error); -} -handle.close(); -console.log(JSON.stringify({ ids: listed.entries.map((entry) => entry.id), count, invalidMessage }));`, - ); - - expect(result).toEqual({ - ids: ["c", "a"], - count: 2, - invalidMessage: "Invalid collection field path: state') OR 1=1 --", - }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); - - it("detects same-size hand edits even when mtime is restored", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-edit-")); - try { - const result = await runner.run( - integrationDir, - `const filePath = join(dir, "note.md"); -const original = "---\\ntitle: one\\n---\\n\\nbody one\\n"; -const edited = "---\\ntitle: two\\n---\\n\\nbody two\\n"; -await writeFile(filePath, original); -const fixedTime = new Date(Math.floor(Date.now() / 1000) * 1000 - 10_000); -await utimes(filePath, fixedTime, fixedTime); -let handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -handle.close(); -const before = await stat(filePath); -await Bun.sleep(10); -await writeFile(filePath, edited); -await utimes(filePath, before.atime, before.mtime); -const after = await stat(filePath); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -const listed = await handle.list(); -handle.close(); -console.log(JSON.stringify({ entry: listed.entries[0], sameMtime: before.mtimeMs === after.mtimeMs, sameSize: before.size === after.size, ctimeChanged: before.ctimeMs !== after.ctimeMs }));`, - ); - - expect(result).toMatchObject({ - entry: { data: { title: "two" }, body: "body two\n" }, - sameMtime: true, - sameSize: true, - ctimeChanged: true, - }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); - - it("detects hand-added and hand-deleted files across reopen", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-files-")); - try { - const result = await runner.run( - integrationDir, - `let handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -handle.close(); -const filePath = join(dir, "added.md"); -await writeFile(filePath, "---\\ntitle: Added\\n---\\n\\nhello\\n"); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -const added = await handle.list(); -handle.close(); -await unlink(filePath); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -const deleted = await handle.list(); -handle.close(); -console.log(JSON.stringify({ added: added.entries.map((entry) => entry.id), afterDelete: deleted.entries.map((entry) => entry.id) }));`, - ); - - expect(result).toEqual({ added: ["added"], afterDelete: [] }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); - - it("rebuilds corrupt and incompatible derived indexes from Markdown", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-recovery-")); - try { - const result = await runner.run( - integrationDir, - `const indexPath = join(dir, ".index.sqlite"); -let handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -await handle.create({ id: "durable", data: { title: "Markdown survives" }, body: "source of truth" }); -handle.close(); - -await writeFile(indexPath, "corrupted sqlite\\n"); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -const afterCorruption = await handle.list(); -handle.close(); - -await unlink(indexPath); -const incompatible = new Database(indexPath, { create: true, strict: true }); -incompatible.run("CREATE TABLE docs (id TEXT PRIMARY KEY)"); -incompatible.close(false); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -const afterIncompatibleSchema = await handle.list(); -handle.close(); - -const rebuilt = new Database(indexPath, { strict: true }); -const integrity = rebuilt.query("PRAGMA quick_check").get(); -rebuilt.close(false); -console.log(JSON.stringify({ afterCorruption, afterIncompatibleSchema, integrity }));`, - ); - - expect(result).toMatchObject({ - afterCorruption: { - entries: [{ id: "durable", data: { title: "Markdown survives" } }], - total: 1, - }, - afterIncompatibleSchema: { - entries: [{ id: "durable", data: { title: "Markdown survives" } }], - total: 1, - }, - integrity: { quick_check: "ok" }, - }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); - - it("removes malformed files from the index and fingerprints them as invalid", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-invalid-")); - try { - const result = await runner.run( - integrationDir, - `const definition = { schema: z.object({ title: z.string() }) }; -let handle = new CollectionHandleImplClass("notes", dir, definition); -await handle.init(); -await handle.create({ id: "note", data: { title: "Valid" } }); -handle.close(); -await writeFile(join(dir, "note.md"), "---\\ntitle: 42\\n---\\n\\ninvalid\\n"); -handle = new CollectionHandleImplClass("notes", dir, definition); -await handle.init(); -const malformed = await handle.list(); -handle.close(); -const db = new Database(join(dir, ".index.sqlite"), { strict: true }); -const record = db.query("SELECT status, size FROM collection_files WHERE id = ?").get("note"); -db.close(false); -handle = new CollectionHandleImplClass("notes", dir, definition); -await handle.init(); -const unchanged = await handle.list(); -handle.close(); -console.log(JSON.stringify({ malformedTotal: malformed.total, unchangedTotal: unchanged.total, record }));`, - ); - - expect(result).toMatchObject({ - malformedTotal: 0, - unchangedTotal: 0, - record: { status: "invalid" }, - }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); - - it("preserves persisted fingerprints on an unchanged reopen", async () => { - const integrationDir = await mkdtemp(join(tmpdir(), "rig-collection-bun-fast-")); - try { - const result = await runner.run( - integrationDir, - `const filePath = join(dir, "stable.md"); -await writeFile(filePath, "---\\ntitle: Stable\\n---\\n\\nunchanged\\n"); -let handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -handle.close(); -let db = new Database(join(dir, ".index.sqlite"), { strict: true }); -const before = db.query("SELECT * FROM collection_files WHERE id = ?").get("stable"); -db.close(false); -handle = new CollectionHandleImplClass("notes", dir, {}); -await handle.init(); -handle.close(); -db = new Database(join(dir, ".index.sqlite"), { strict: true }); -const after = db.query("SELECT * FROM collection_files WHERE id = ?").get("stable"); -db.close(false); -console.log(JSON.stringify({ before, after }));`, - ); - - expect(result.before).toEqual(result.after); - expect(result.after).toMatchObject({ status: "indexed" }); - } finally { - await rm(integrationDir, { recursive: true, force: true }); - } - }); -}); - -class FaultInjectingCollectionHandleClass implements ManagedCollectionHandle { - readonly path = "/tmp/test"; - readonly calls: string[] = []; - initialized = false; - closeRuns = 0; - - constructor( - readonly name: string, - private readonly failure: Error | undefined, - ) {} - - async init(): Promise { - if (this.failure) throw this.failure; - this.initialized = true; - } - - close(): void { - this.closeRuns++; - } - - async create(): Promise { - this.calls.push("create"); - return { id: "created" } as never; - } - async getEntry(): Promise { - this.calls.push("getEntry"); - return null as never; - } - async update(): Promise { - this.calls.push("update"); - return { id: "updated" } as never; - } - async upsert(): Promise { - this.calls.push("upsert"); - return { id: "upserted", created: true } as never; - } - async remove(): Promise { - this.calls.push("remove"); - return true; - } - async list(): Promise { - this.calls.push("list"); - return { entries: [], total: 0 } as never; - } - async search(): Promise { - this.calls.push("search"); - return { entries: [] } as never; - } - async count(): Promise { - this.calls.push("count"); - return 1; - } - async getCollection(): Promise { - this.calls.push("getCollection"); - return [] as never; - } - async clear(): Promise { - this.calls.push("clear"); - } -} - -class FaultInjectingCollectionHandleFactoryClass implements CollectionHandleFactory { - readonly handles: FaultInjectingCollectionHandleClass[] = []; - - create(name: string): FaultInjectingCollectionHandleClass { - const handle = new FaultInjectingCollectionHandleClass( - name, - name === "broken" ? new Error("collection initialization failed") : undefined, - ); - this.handles.push(handle); - return handle; - } -} - -describe("ToolCollectionServiceClass cleanup", () => { - it("initializes collections on first use and closes failures", async () => { - const factory = new FaultInjectingCollectionHandleFactoryClass(); - const service = new ToolCollectionServiceClass(factory); - const handles = await service.setup({ - path: "/tmp/tool/index.rig.ts", - definition: { collections: { ready: {}, broken: {}, unused: undefined } }, - } as never); - - expect(factory.handles.every((handle) => !handle.initialized)).toBe(true); - await expect(handles?.broken?.count()).rejects.toThrow("collection initialization failed"); - expect(factory.handles.find((handle) => handle.name === "broken")?.closeRuns).toBe(1); - - const ready = handles!.ready!; - await ready.create({ data: {} }); - await ready.getEntry("created"); - await ready.update("created", { body: "updated" }); - await ready.upsert({ id: "upserted", data: {} }); - await ready.remove("created"); - await ready.list({ limit: 1 }); - await ready.search("query", { limit: 1 }); - expect(await ready.count({ status: "ready" })).toBe(1); - await ready.getCollection(() => true); - await ready.clear(); - - expect(factory.handles.find((handle) => handle.name === "ready")?.calls).toEqual([ - "create", - "getEntry", - "update", - "upsert", - "remove", - "list", - "search", - "count", - "getCollection", - "clear", - ]); - service.close(handles); - expect( - factory.handles.map((handle) => [handle.name, handle.initialized, handle.closeRuns]), - ).toEqual([ - ["ready", true, 1], - ["broken", false, 1], - ["unused", false, 0], - ]); - }); - - it("returns undefined for tools without collections", async () => { - const service = new ToolCollectionServiceClass(); - expect( - await service.setup({ path: "/tmp/tool/index.rig.ts", definition: {} } as never), - ).toBeUndefined(); - service.close(undefined); - }); -}); - -describe("collection codec and memory index edge cases", () => { - it("parses scalar, object, and malformed YAML branches", () => { - const codec = new FrontmatterCodecClass(); - const parsed = codec.parse(`--- -mixed: - nested: value - - scalar -items: - - name: first - invalid continuation -invalid top-level line -enabled: false -empty: null -single: 'quoted' ---- -Body`); - - expect(parsed).toMatchObject({ - data: { - mixed: ["scalar"], - items: [{ name: "first" }], - enabled: false, - empty: null, - single: "quoted", - }, - body: "Body", - }); - }); - - it("filters, sorts, and searches memory rows across fallback values", () => { - const index = new MemoryCollectionIndexClass(); - index.upsertDoc( - { - id: "two", - data: { meta: { state: { open: false } } }, - body: "alpha", - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-01T00:00:00.000Z", - }, - 1, - ); - index.upsertDoc( - { - id: "one", - data: { meta: { state: { open: true } }, rank: 1 }, - body: "alpha beta", - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-02T00:00:00.000Z", - }, - 1, - ); - index.upsertDoc( - { - id: "three", - data: { meta: { state: { open: true } }, rank: 1 }, - body: "gamma", - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-03T00:00:00.000Z", - }, - 1, - ); - - expect(index.listDocs({ where: { "meta.state": { open: true } } }).total).toBe(2); - expect(index.listDocs({ sort: "rank" }).rows.map((row) => row.id)).toEqual([ - "two", - "one", - "three", - ]); - expect(index.listDocs({ sort: "-rank" }).rows.map((row) => row.id)).toEqual([ - "one", - "three", - "two", - ]); - expect(index.listDocs({}).rows.map((row) => row.id)).toEqual(["three", "one", "two"]); - expect(index.searchDocs("alpha beta", 5).map((row) => row.id)).toEqual(["one", "two"]); - - const reverseIndex = new MemoryCollectionIndexClass(); - reverseIndex.upsertDoc( - { - id: "ranked", - data: { rank: 1 }, - body: "", - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-02T00:00:00.000Z", - }, - 1, - ); - reverseIndex.upsertDoc( - { - id: "missing", - data: {}, - body: "", - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-01T00:00:00.000Z", - }, - 1, - ); - expect(reverseIndex.listDocs({ sort: "rank" }).total).toBe(2); - expect(reverseIndex.listDocs({}).rows.map((row) => row.id)).toEqual(["ranked", "missing"]); - }); - - it("falls back to filesystem timestamps when an entry is absent from the index", async () => { - const dir = await mkdtemp(join(tmpdir(), "rig-collection-fallback-")); - const index = new MemoryCollectionIndexClass(); - const handle = new CollectionHandleImplClass("notes", dir, {}, index); - try { - await writeFile(join(dir, "manual.md"), "---\ntitle: Manual\n---\nBody", "utf8"); - await handle.init(); - index.deleteDoc("manual"); - const entry = await ( - handle as unknown as { readFromDisk(id: string): Promise<{ id: string }> } - ).readFromDisk("manual"); - expect(entry.id).toBe("manual"); - } finally { - handle.close(); - await rm(dir, { recursive: true, force: true }); - } - }); -}); diff --git a/src/tools/collection.ts b/src/tools/collection.ts deleted file mode 100644 index 3e3b8e4..0000000 --- a/src/tools/collection.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../collections/application/tool-collection"; diff --git a/src/tools/create.ts b/src/tools/create.ts deleted file mode 100644 index 26ca48c..0000000 --- a/src/tools/create.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./management/tool-files"; diff --git a/src/tools/cron.test.ts b/src/tools/cron.test.ts deleted file mode 100644 index d1d613d..0000000 --- a/src/tools/cron.test.ts +++ /dev/null @@ -1,737 +0,0 @@ -import { afterEach, describe, expect, test, vi } from "vite-plus/test"; -import { existsSync } from "node:fs"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { RigConfigStoreClass } from "../config/config"; -import { RigPathsClass } from "../config/paths"; -import { ToolCreatorClass } from "./create"; -import { - cronModuleUrl, - type CronRegistrar, - type CronRunResult, - RigCronServiceClass, - RigCronWorkerClass, -} from "./cron"; - -class TestHomeStore { - private readonly homes: string[] = []; - private readonly originalExitCode = process.exitCode; - - async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-cron-test-home-")); - this.homes.push(home); - return home; - } - - async cleanup(): Promise { - process.exitCode = this.originalExitCode; - vi.restoreAllMocks(); - vi.unstubAllGlobals(); - await Promise.all( - this.homes.splice(0).map((home) => rm(home, { recursive: true, force: true })), - ); - } -} - -class FakeCronRegistrar implements CronRegistrar { - readonly registered: { path: string; schedule: string; title: string }[] = []; - readonly removed: string[] = []; - readonly validated: string[] = []; - - register(params: { path: string; schedule: string; title: string }): Promise { - this.registered.push(params); - return Promise.resolve(); - } - - remove(params: { title: string }): Promise { - this.removed.push(params.title); - return Promise.resolve(); - } - - validate(params: { schedule: string }): void { - this.validated.push(params.schedule); - } -} - -const homes = new TestHomeStore(); - -function rejectNextConfigUpdate(message: string): void { - vi.spyOn(RigConfigStoreClass.prototype, "update").mockImplementationOnce( - async function (this: RigConfigStoreClass, mutator) { - await mutator(await this.read()); - throw new Error(message); - }, - ); -} - -afterEach(async () => { - await homes.cleanup(); -}); - -describe("cron tool commands", () => { - test("adds, lists, runs, and removes a scheduled command", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const added = await service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "0 9 * * MON", - input: '{"text":"Jira"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - - expect(added.job).toMatchObject({ - name: "weekly-jira", - command: "sample.example", - schedule: "0 9 * * MON", - input: { text: "Jira" }, - }); - expect(registrar.validated).toEqual(["0 9 * * MON"]); - expect(registrar.registered).toEqual([ - { - path: paths.cronWorkerPath("weekly-jira"), - schedule: "0 9 * * MON", - title: "weekly-jira", - }, - ]); - - const worker = await readFile(paths.cronWorkerPath("weekly-jira"), "utf8"); - expect(worker).toContain("--install=fallback"); - expect(worker).toContain('"cron"'); - expect(worker).toContain('"run"'); - expect(worker).toContain("weekly-jira"); - - const config = await new RigConfigStoreClass({ homeDir: home }).read(); - expect(config.cronJobs).toHaveLength(1); - expect(await service.list()).toMatchObject({ cronJobs: [added.job] }); - - const run = await service.run({ name: "weekly-jira" }); - expect(run.exitCode).toBe(0); - expect(run.envelope).toMatchObject({ - data: { text: "Jira" }, - errors: [], - }); - - const logs: string[] = []; - vi.spyOn(console, "log").mockImplementation((value) => logs.push(String(value))); - await new RigCronWorkerClass({ homeDir: home }, {}).scheduled({ - name: "weekly-jira", - controller: { - cron: "0 9 * * MON", - scheduledTime: 123, - type: "scheduled", - }, - }); - expect(logs.join("\n")).toContain('"name": "weekly-jira"'); - expect(logs.join("\n")).toContain('"scheduledTime": 123'); - expect(process.exitCode).toBe(0); - logs.length = 0; - await new RigCronWorkerClass({ homeDir: home }, {}).scheduled({ name: "weekly-jira" }); - expect(logs.join("\n")).toContain('"schedule": "0 9 * * MON"'); - - const removed = await service.remove({ name: "weekly-jira" }); - expect(removed).toMatchObject({ - name: "weekly-jira", - removed: true, - workerPath: paths.cronWorkerPath("weekly-jira"), - }); - expect(registrar.removed).toEqual(["weekly-jira"]); - expect(existsSync(paths.cronWorkerPath("weekly-jira"))).toBe(false); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([]); - }); - - test("runs workers through an injected service", async () => { - const run = vi.fn<() => Promise>(async () => ({ - job: { name: "injected", command: "sample.example", schedule: "@daily" }, - envelope: { data: { ok: true }, errors: [] }, - exitCode: 0, - })); - vi.spyOn(console, "log").mockImplementation(() => {}); - const worker = new RigCronWorkerClass({}, { service: { run } as never }); - - await worker.scheduled({ name: "injected" }); - - expect(run).toHaveBeenCalledWith({ name: "injected" }); - }); - - test("supports input files and no explicit input", async () => { - const home = await homes.create(); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - const inputPath = join(home, "input.json"); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await writeFile(inputPath, '{"text":"from file"}\n', "utf8"); - - const fromFile = await service.add({ - name: "from-file", - command: "sample.example", - schedule: "@weekly", - inputFile: inputPath, - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const defaultInput = await service.add({ - name: "default-input", - command: "sample.example", - schedule: "@daily", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - - expect((await service.list()).cronJobs.map((job) => job.name)).toEqual([ - "default-input", - "from-file", - ]); - expect(fromFile.job.input).toEqual({ text: "from file" }); - expect(defaultInput.job).not.toHaveProperty("input"); - expect(await service.run({ name: "default-input" })).toMatchObject({ - envelope: { data: { text: "example" }, errors: [] }, - exitCode: 0, - }); - }); - - test("replaces an existing job and worker", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - input: '{"text":"old"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const replacement = await service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@daily", - input: '{"text":"new"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "tools", "cron.ts")).href, - }); - - expect((await service.list()).cronJobs).toEqual([replacement.job]); - expect(registrar.registered.map((entry) => entry.schedule)).toEqual(["@weekly", "@daily"]); - expect(await readFile(paths.cronWorkerPath("weekly-jira"), "utf8")).toContain( - "/src/tools/cron.ts", - ); - }); - - test("preserves concurrent cron additions", async () => { - const home = await homes.create(); - const registrar = new FakeCronRegistrar(); - const names = Array.from({ length: 10 }, (_, index) => `job-${index}`); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - - await Promise.all( - names.map((name) => - new RigCronServiceClass({ homeDir: home }, { registrar }).add({ - name, - command: "sample.example", - schedule: "@daily", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ), - ); - - expect( - (await new RigCronServiceClass({ homeDir: home }, { registrar }).list()).cronJobs.map( - (job) => job.name, - ), - ).toEqual(names); - }); - - test("validates cron job names, inputs, command ids, and command input", async () => { - const home = await homes.create(); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - - await expect(service.run({ name: "missing" })).rejects.toThrow("Cron job not found: missing"); - await expect( - service.add({ - name: "bad name", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("Cron job names may only contain"); - await expect( - service.add({ - name: "bad-command", - command: "sample", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("Command id must use ."); - await expect( - service.add({ - name: "double-input", - command: "sample.example", - schedule: "@weekly", - input: '{"text":"inline"}', - inputFile: join(home, "input.json"), - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("Use --input or --input-file"); - await expect( - service.add({ - name: "bad-input", - command: "sample.example", - schedule: "@weekly", - input: '{"text":123}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("Cron command validation failed: sample.example"); - }); - - test("uses Bun cron registration and schedule validation", async () => { - const home = await homes.create(); - const originalBun = (globalThis as typeof globalThis & { Bun?: unknown }).Bun; - const cron = Object.assign( - vi.fn<(path: string, schedule: string, title: string) => Promise>(() => - Promise.resolve(), - ), - { - parse: vi.fn<(expression: string) => Date | null>( - () => new Date("2026-07-01T00:00:00.000Z"), - ), - remove: vi.fn<(title: string) => Promise>(() => Promise.resolve()), - }, - ); - vi.stubGlobal("Bun", { ...(originalBun as Record), cron }); - const service = new RigCronServiceClass({ homeDir: home }, {}); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await service.add({ - name: "bun-cron", - command: "sample.example", - schedule: "@weekly", - moduleUrl: cronModuleUrl(import.meta.url), - }); - await service.remove({ name: "bun-cron" }); - - expect(cron.parse).toHaveBeenCalledWith("@weekly"); - expect(cron).toHaveBeenCalledWith(expect.any(String), "@weekly", "bun-cron"); - expect(cron.remove).toHaveBeenCalledWith("bun-cron"); - - cron.parse.mockReturnValueOnce(null); - await expect( - service.add({ - name: "no-future-runs", - command: "sample.example", - schedule: "@never", - moduleUrl: cronModuleUrl(import.meta.url), - }), - ).rejects.toThrow("Cron schedule has no future runs"); - }); - - test("rolls back config and worker when OS registration fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const remove = vi.fn<() => Promise>(() => Promise.resolve()); - const registrar: CronRegistrar = { - validate: () => {}, - register: () => Promise.reject(new Error("launchd unavailable")), - remove, - }; - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - - await expect( - service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - input: '{"text":"Jira"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("launchd unavailable"); - - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([]); - expect(existsSync(paths.cronWorkerPath("weekly-jira"))).toBe(false); - expect(remove).toHaveBeenCalledWith({ title: "weekly-jira" }); - }); - - test("restores the previous job and worker when replacement registration fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const original = await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - input: '{"text":"old"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const originalWorker = await readFile(workerPath, "utf8"); - const register = vi - .fn<(params: { path: string; schedule: string; title: string }) => Promise>() - .mockRejectedValueOnce(new Error("replacement unavailable")) - .mockResolvedValueOnce(); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: { validate: () => {}, register, remove: () => Promise.resolve() } }, - ); - - await expect( - service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@daily", - input: '{"text":"new"}', - moduleUrl: pathToFileURL(join(process.cwd(), "src", "tools", "cron.ts")).href, - }), - ).rejects.toThrow("replacement unavailable"); - - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([ - original.job, - ]); - expect(await readFile(workerPath, "utf8")).toBe(originalWorker); - expect(register.mock.calls.map((call) => call[0].schedule)).toEqual(["@daily", "@weekly"]); - }); - - test("preserves the configured job and worker when removal fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const original = await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const originalWorker = await readFile(workerPath, "utf8"); - const service = new RigCronServiceClass( - { homeDir: home }, - { - registrar: { - validate: () => {}, - register: () => Promise.resolve(), - remove: () => Promise.reject(new Error("launchd remove unavailable")), - }, - }, - ); - - await expect(service.remove({ name: "weekly-jira" })).rejects.toThrow( - "launchd remove unavailable", - ); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([ - original.job, - ]); - expect(await readFile(workerPath, "utf8")).toBe(originalWorker); - }); - - test("restores local state and OS registration when removal persistence fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const original = await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const originalWorker = await readFile(workerPath, "utf8"); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - rejectNextConfigUpdate("config unavailable"); - - await expect(service.remove({ name: "weekly-jira" })).rejects.toThrow("config unavailable"); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([ - original.job, - ]); - expect(await readFile(workerPath, "utf8")).toBe(originalWorker); - expect(registrar.removed).toEqual(["weekly-jira"]); - expect(registrar.registered).toEqual([ - { path: workerPath, schedule: "@weekly", title: "weekly-jira" }, - ]); - }); - - test("restores the removed config job when worker deletion fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const original = await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const service = new RigCronServiceClass( - { homeDir: home }, - { - registrar: { - validate: () => {}, - register: () => Promise.resolve(), - remove: async () => { - await rm(workerPath); - await mkdir(workerPath); - await writeFile(join(workerPath, "keep"), "force deletion failure\n", "utf8"); - }, - }, - }, - ); - - await expect(service.remove({ name: "weekly-jira" })).rejects.toMatchObject({ - code: "CRON_ERROR", - message: "Cron state rollback was incomplete.", - }); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([ - original.job, - ]); - }); - - test("preserves a concurrent cron replacement during removal restoration", async () => { - const home = await homes.create(); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - const replacement = { name: "weekly-jira", command: "sample.example", schedule: "@daily" }; - const store = new RigConfigStoreClass({ homeDir: home }); - await store.update((config) => ({ ...config, cronJobs: [replacement] })); - const transaction = ( - service as unknown as { - transaction: Record< - "remove" | "restoreRemovedJob" | "snapshot", - (...params: unknown[]) => unknown - >; - } - ).transaction; - - await transaction.restoreRemovedJob({ ...replacement, schedule: "@weekly" }); - const workerPath = new RigPathsClass({ homeDir: home }).cronWorkerPath("missing"); - expect(await transaction.snapshot({ workerPath })).toEqual({ workerSource: undefined }); - expect(await transaction.remove({ snapshot: {}, workerPath, name: "missing" })).toBe(false); - - expect((await store.read()).cronJobs).toEqual([replacement]); - }); - - test("does not restore a failed replacement over missing or newer config state", async () => { - const home = await homes.create(); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - const store = new RigConfigStoreClass({ homeDir: home }); - const failedJob = { - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - }; - const transaction = ( - service as unknown as { - transaction: { - restoreReplacedJob(job: typeof failedJob, previousJob?: typeof failedJob): Promise; - }; - } - ).transaction; - - await transaction.restoreReplacedJob(failedJob); - expect((await store.read()).cronJobs).toEqual([]); - - const newerJob = { ...failedJob, schedule: "@daily" }; - await store.update((config) => ({ ...config, cronJobs: [newerJob] })); - await transaction.restoreReplacedJob(failedJob, { ...failedJob, schedule: "@monthly" }); - expect((await store.read()).cronJobs).toEqual([newerJob]); - }); - - test("rolls back a new worker when local replacement persistence fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - rejectNextConfigUpdate("config unavailable"); - - await expect( - service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }), - ).rejects.toThrow("config unavailable"); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([]); - expect(existsSync(paths.cronWorkerPath("weekly-jira"))).toBe(false); - expect(registrar.registered).toEqual([]); - expect(registrar.removed).toEqual([]); - }); - - test("leaves config unchanged when worker replacement cannot start", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - const store = new RigConfigStoreClass({ homeDir: home }); - await store.ensure(); - await writeFile(paths.cronDir, "blocks cron directory creation\n", "utf8"); - const job = { name: "weekly-jira", command: "sample.example", schedule: "@weekly" }; - const transaction = ( - service as unknown as { - transaction: { - replace(params: { - snapshot: { workerSource?: string }; - workerPath: string; - workerSource: string; - job: typeof job; - }): Promise; - }; - } - ).transaction; - - await expect( - transaction.replace({ - snapshot: {}, - workerPath: join(paths.cronDir, "weekly-jira.ts"), - workerSource: "worker\n", - job, - }), - ).rejects.toThrow(/EEXIST|ENOTDIR|not a directory/); - expect((await store.read()).cronJobs).toEqual([]); - }); - - test("restores config after local removal failure without a configured job", async () => { - const home = await homes.create(); - const registrar = new FakeCronRegistrar(); - const service = new RigCronServiceClass({ homeDir: home }, { registrar }); - - await new RigConfigStoreClass({ homeDir: home }).ensure(); - rejectNextConfigUpdate("config unavailable"); - - await expect(service.remove({ name: "missing" })).rejects.toThrow("config unavailable"); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([]); - expect(registrar.removed).toEqual(["missing"]); - expect(registrar.registered).toEqual([]); - }); - - test("reports local rollback failures after registration fails", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const originalWorker = await readFile(workerPath, "utf8"); - const register = vi - .fn<(params: { path: string; schedule: string; title: string }) => Promise>() - .mockRejectedValueOnce(new Error("replacement unavailable")) - .mockResolvedValueOnce(); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: { validate: () => {}, register, remove: () => Promise.resolve() } }, - ); - const originalUpdate = RigConfigStoreClass.prototype.update; - const update = vi.spyOn(RigConfigStoreClass.prototype, "update"); - update.mockImplementationOnce(async function (this: RigConfigStoreClass, mutator) { - update.mockRejectedValueOnce(new Error("config restore unavailable")); - return originalUpdate.call(this, mutator); - }); - - await expect( - service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@daily", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "tools", "cron.ts")).href, - }), - ).rejects.toMatchObject({ - code: "CRON_ERROR", - details: { rollbackErrors: ["config restore unavailable"] }, - }); - expect(await readFile(workerPath, "utf8")).toBe(originalWorker); - }); - - test("reports incomplete rollback while restoring local replacement state", async () => { - const home = await homes.create(); - const paths = new RigPathsClass({ homeDir: home }); - const initialService = new RigCronServiceClass( - { homeDir: home }, - { registrar: new FakeCronRegistrar() }, - ); - - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const original = await initialService.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@weekly", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "cli.ts")).href, - }); - const workerPath = paths.cronWorkerPath("weekly-jira"); - const originalWorker = await readFile(workerPath, "utf8"); - const register = vi - .fn<(params: { path: string; schedule: string; title: string }) => Promise>() - .mockRejectedValueOnce(new Error("replacement unavailable")) - .mockRejectedValueOnce("restore unavailable"); - const service = new RigCronServiceClass( - { homeDir: home }, - { registrar: { validate: () => {}, register, remove: () => Promise.resolve() } }, - ); - - await expect( - service.add({ - name: "weekly-jira", - command: "sample.example", - schedule: "@daily", - moduleUrl: pathToFileURL(join(process.cwd(), "src", "tools", "cron.ts")).href, - }), - ).rejects.toMatchObject({ - code: "CRON_ERROR", - message: "Cron state rollback was incomplete.", - details: { - cause: "replacement unavailable", - rollbackErrors: ["restore unavailable"], - }, - }); - expect((await new RigConfigStoreClass({ homeDir: home }).read()).cronJobs).toEqual([ - original.job, - ]); - expect(await readFile(workerPath, "utf8")).toBe(originalWorker); - }); -}); diff --git a/src/tools/cron.ts b/src/tools/cron.ts deleted file mode 100644 index 42f8e28..0000000 --- a/src/tools/cron.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../cron/application/rig-cron"; diff --git a/src/tools/db.ts b/src/tools/db.ts deleted file mode 100644 index db766f4..0000000 --- a/src/tools/db.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../persistence/sqlite/tool-database"; diff --git a/src/tools/domain/identifiers.ts b/src/tools/domain/identifiers.ts deleted file mode 100644 index d85c2d9..0000000 --- a/src/tools/domain/identifiers.ts +++ /dev/null @@ -1,193 +0,0 @@ -import { defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; - -const RoutedNamePattern = /^[A-Za-z0-9_-]+$/; - -export const ToolNameSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { value: string }) { - if (typeof params.value !== "string" || !RoutedNamePattern.test(params.value)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid tool name: ${params.value}`, { - expected: "letters, numbers, hyphens, or underscores", - }); - } - return { value: params.value }; - }, - parse(params: { value: string }) { - return ToolNameSingleton.create(params); - }, -}); - -export const CommandNameSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { value: string }) { - if (typeof params.value !== "string" || !RoutedNamePattern.test(params.value)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid command name: ${params.value}`, { - expected: "letters, numbers, hyphens, or underscores", - }); - } - return { value: params.value }; - }, - parse(params: { value: string }) { - return CommandNameSingleton.create(params); - }, -}); - -export const CollectionNameSingleton = defineSingleton({ - params: {}, - deps: {}, - create(params: { value: string }) { - if (typeof params.value !== "string" || !RoutedNamePattern.test(params.value)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid collection name: ${params.value}`, { - expected: "letters, numbers, hyphens, or underscores", - }); - } - return { value: params.value }; - }, - parse(params: { value: string }) { - return CollectionNameSingleton.create(params); - }, -}); - -export const CommandTargetSingleton = defineSingleton({ - params: { idSeparator: ".", expectedIdFormat: "." }, - deps: { ToolNameSingleton, CommandNameSingleton }, - create(params: { tool: string; command: string }) { - return { - tool: params.tool, - command: params.command, - id: `${params.tool}${this.params.idSeparator}${params.command}`, - }; - }, - from(params: { tool: string; command: string }) { - return CommandTargetSingleton.create({ - tool: this.deps.ToolNameSingleton.create({ value: params.tool }).value, - command: this.deps.CommandNameSingleton.create({ value: params.command }).value, - }); - }, - invalid(params: { id: unknown }) { - return new RigErrorClass( - "INPUT_ERROR", - `Command id must use ${this.params.expectedIdFormat}: ${String(params.id)}`, - ); - }, - parse(params: { id: string }) { - if (typeof params.id !== "string") throw CommandTargetSingleton.invalid({ id: params.id }); - const match = params.id.match(/^([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)$/); - if (!match) throw CommandTargetSingleton.invalid({ id: params.id }); - return CommandTargetSingleton.create({ tool: match[1]!, command: match[2]! }); - }, -}); - -export type ToolNameClass = { - readonly value: string; - parse(value: string): ToolNameClass; -}; - -type ToolNameConstructor = { - new (value: string): ToolNameClass; - readonly prototype: ToolNameClass; -}; - -export const ToolNameClass = function (this: ToolNameClass, value: string) { - Object.assign(this, ToolNameSingleton.create({ value })); -} as unknown as ToolNameConstructor; - -Object.defineProperty(ToolNameClass.prototype, "parse", { - configurable: true, - value: function parse(value: string) { - return new ToolNameClass(value); - }, - writable: true, -}); - -export type CommandNameClass = { - readonly value: string; - parse(value: string): CommandNameClass; -}; - -type CommandNameConstructor = { - new (value: string): CommandNameClass; - readonly prototype: CommandNameClass; -}; - -export const CommandNameClass = function (this: CommandNameClass, value: string) { - Object.assign(this, CommandNameSingleton.create({ value })); -} as unknown as CommandNameConstructor; - -Object.defineProperty(CommandNameClass.prototype, "parse", { - configurable: true, - value: function parse(value: string) { - return new CommandNameClass(value); - }, - writable: true, -}); - -export type CollectionNameClass = { - readonly value: string; - parse(value: string): CollectionNameClass; -}; - -type CollectionNameConstructor = { - new (value: string): CollectionNameClass; - readonly prototype: CollectionNameClass; -}; - -export const CollectionNameClass = function (this: CollectionNameClass, value: string) { - Object.assign(this, CollectionNameSingleton.create({ value })); -} as unknown as CollectionNameConstructor; - -Object.defineProperty(CollectionNameClass.prototype, "parse", { - configurable: true, - value: function parse(value: string) { - return new CollectionNameClass(value); - }, - writable: true, -}); - -export type CommandTargetClass = { - readonly tool: string; - readonly command: string; - readonly id: string; - from(tool: string, command: string): CommandTargetClass; - parse(id: string): CommandTargetClass; -}; - -type CommandTargetConstructor = { - new (tool: string, command: string): CommandTargetClass; - readonly prototype: CommandTargetClass; -}; - -export const CommandTargetClass = function ( - this: CommandTargetClass, - tool: string, - command: string, -) { - Object.assign(this, CommandTargetSingleton.create({ tool, command })); -} as unknown as CommandTargetConstructor; - -Object.defineProperties(CommandTargetClass.prototype, { - from: { - configurable: true, - value: function from(tool: string, command: string) { - const target = CommandTargetSingleton.from({ tool, command }); - return new CommandTargetClass(target.tool, target.command); - }, - writable: true, - }, - parse: { - configurable: true, - value: function parse(id: string) { - const target = CommandTargetSingleton.parse({ id }); - return new CommandTargetClass(target.tool, target.command); - }, - writable: true, - }, -}); - -export const toolNames = new ToolNameClass("tool"); -export const commandNames = new CommandNameClass("command"); -export const collectionNames = new CollectionNameClass("collection"); -export const commandTargets = new CommandTargetClass("tool", "command"); diff --git a/src/tools/domain/schema-renderer.test.ts b/src/tools/domain/schema-renderer.test.ts deleted file mode 100644 index 70ffe99..0000000 --- a/src/tools/domain/schema-renderer.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; -import { z } from "zod"; -import { - schemaRenderer, - SchemaRendererClass, - SchemaRendererService, - SchemaRenderer, -} from "./schema-renderer"; - -describe("schema rendering", () => { - it("renders injected JSON schemas and summaries", () => { - const Renderer = new SchemaRendererService({ - params: {}, - deps: { - toJsonSchema(schema) { - return { type: "injected", schema }; - }, - }, - }); - - expect(Renderer.toJsonSchema({ schema: "value" })).toEqual({ - type: "injected", - schema: "value", - }); - expect(Renderer.summary({ schema: "value" })).toBe( - JSON.stringify({ type: "injected", schema: "value" }, null, 2), - ); - }); - - it("reports unavailable and failed conversion", () => { - const UnavailableRenderer = new SchemaRendererService({ - params: {}, - deps: { toJsonSchema: undefined }, - }); - const FailedRenderer = new SchemaRendererService({ - params: {}, - deps: { - toJsonSchema() { - throw new Error("failed"); - }, - }, - }); - - expect(UnavailableRenderer.toJsonSchema({ schema: {} })).toEqual({ - type: "unknown", - note: "JSON Schema conversion is unavailable.", - }); - expect(FailedRenderer.toJsonSchema({ schema: {} })).toEqual({ - type: "unknown", - note: "JSON Schema conversion failed.", - }); - }); - - it("preserves the production value and positional adapters", () => { - const adapter = new SchemaRendererClass(); - const schema = z.object({ title: z.string() }); - - expect(adapter).toBeInstanceOf(SchemaRendererClass); - expect(adapter.summary(schema)).toContain("title"); - expect(schemaRenderer.summary(schema)).toBe(SchemaRenderer.summary({ schema })); - }); -}); diff --git a/src/tools/domain/schema-renderer.ts b/src/tools/domain/schema-renderer.ts deleted file mode 100644 index f5a473f..0000000 --- a/src/tools/domain/schema-renderer.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { defineService } from "../../define"; -import { z } from "zod"; - -const schemaRendererDeps: { - toJsonSchema: ((schema: unknown) => unknown) | undefined; -} = { - toJsonSchema: (z as unknown as { toJSONSchema?: (schema: unknown) => unknown }).toJSONSchema, -}; - -export class SchemaRendererService extends defineService({ params: {}, deps: schemaRendererDeps }) { - public toJsonSchema(params: { schema: unknown }): unknown { - if (typeof this.deps.toJsonSchema !== "function") { - return { type: "unknown", note: "JSON Schema conversion is unavailable." }; - } - - try { - return this.deps.toJsonSchema(params.schema); - } catch { - return { type: "unknown", note: "JSON Schema conversion failed." }; - } - } - - public summary(params: { schema: unknown }): string { - return JSON.stringify(this.toJsonSchema(params), null, 2); - } -} - -export const SchemaRenderer = new SchemaRendererService(); - -type SchemaRendererConstructor = { - new (): SchemaRendererClass; - readonly prototype: SchemaRendererClass; -}; - -export type SchemaRendererClass = { - toJsonSchema(schema: unknown): unknown; - summary(schema: unknown): string; -}; - -const SchemaRendererClassAdapter = function constructSchemaRenderer(): void {}; -Object.defineProperty(SchemaRendererClassAdapter, "name", { value: "SchemaRendererClass" }); -Object.defineProperty(SchemaRendererClassAdapter.prototype, "toJsonSchema", { - configurable: true, - value: function toJsonSchema(schema: unknown): unknown { - return SchemaRenderer.toJsonSchema({ schema }); - }, - writable: true, -}); -Object.defineProperty(SchemaRendererClassAdapter.prototype, "summary", { - configurable: true, - value: function summary(schema: unknown): string { - return SchemaRenderer.summary({ schema }); - }, - writable: true, -}); - -export const SchemaRendererClass = - SchemaRendererClassAdapter as unknown as SchemaRendererConstructor; - -export const schemaRenderer = new SchemaRendererClass(); diff --git a/src/tools/domain/tool-search.ts b/src/tools/domain/tool-search.ts deleted file mode 100644 index 42024bf..0000000 --- a/src/tools/domain/tool-search.ts +++ /dev/null @@ -1,329 +0,0 @@ -import { defineSingleton } from "../../define"; - -export type ToolSearchField = { - name: string; - value: string; - weight: number; -}; - -export type ToolSearchDocument = { - id: string; - fields: ToolSearchField[]; -}; - -export type ToolSearchFieldMatch = { - field: string; - value: string; - score: number; -}; - -export type ToolSearchRankedDocument = { - id: string; - score: number; - matches: ToolSearchFieldMatch[]; -}; - -type CompiledToolSearchField = ToolSearchField & { - normalizedValue: string; - tokens: string[]; -}; - -type CompiledToolSearchDocument = { - id: string; - fields: CompiledToolSearchField[]; - tokens: string[]; -}; - -type ToolSearchTextOps = { - normalize(params: { value: string }): string; - tokensFromNormalized(params: { value: string; removeStopWords?: boolean }): string[]; -}; - -function normalizeToolSearchText(params: { value: string }): string { - return params.value - .normalize("NFKD") - .replace(/[\u0300-\u036f]/g, "") - .toLowerCase() - .replace(/[^a-z0-9]+/g, " ") - .trim(); -} - -function roundToolSearchScore(value: number): number { - return Math.round(value * 1000) / 1000; -} - -function createToolSearchText(): ToolSearchTextOps { - const stopWords = new Set([ - "a", - "an", - "and", - "for", - "in", - "my", - "of", - "on", - "or", - "the", - "to", - "with", - ]); - - function tokensFromNormalized(params: { value: string; removeStopWords?: boolean }): string[] { - const tokens = params.value.split(/\s+/).filter(Boolean); - if (!params.removeStopWords) return [...new Set(tokens)]; - const meaningful = tokens.filter((token) => !stopWords.has(token)); - return [...new Set(meaningful.length > 0 ? meaningful : tokens)]; - } - - return { normalize: normalizeToolSearchText, tokensFromNormalized }; -} - -type ToolSearchSimilarityParams = { - query: string; - candidate: string; - queryTrigrams: Set; - candidateTrigrams: Set; -}; - -function scoreToolSearchSimilarity(params: ToolSearchSimilarityParams): number { - if (params.query === params.candidate) return 1; - if ( - Math.min(params.query.length, params.candidate.length) >= 3 && - (params.candidate.startsWith(params.query) || params.query.startsWith(params.candidate)) - ) { - return 0.9; - } - - const longest = Math.max(params.query.length, params.candidate.length); - /* v8 ignore next -- empty normalized queries return before similarity scoring */ - if (longest === 0) return 0; - const minimumDistance = Math.abs(params.query.length - params.candidate.length); - - const editSimilarity = 1 - minimumDistance / longest >= 0.58 ? 1 - distance(params) / longest : 0; - const trigramSimilarity = trigramDice(params); - const similarity = Math.max(editSimilarity, trigramSimilarity); - return similarity >= 0.58 ? similarity * 0.82 : 0; -} - -function distance(params: { query: string; candidate: string }): number { - const columns = params.candidate.length + 1; - let previousPrevious = new Uint32Array(columns); - let previous = new Uint32Array(columns); - let current = new Uint32Array(columns); - for (let column = 0; column < columns; column++) previous[column] = column; - - for (let row = 1; row <= params.query.length; row++) { - current[0] = row; - for (let column = 1; column < columns; column++) { - const substitution = params.query[row - 1] === params.candidate[column - 1] ? 0 : 1; - current[column] = Math.min( - previous[column]! + 1, - current[column - 1]! + 1, - previous[column - 1]! + substitution, - ); - if ( - row > 1 && - column > 1 && - params.query[row - 1] === params.candidate[column - 2] && - params.query[row - 2] === params.candidate[column - 1] - ) { - current[column] = Math.min(current[column]!, previousPrevious[column - 2]! + substitution); - } - } - const recycled = previousPrevious; - previousPrevious = previous; - previous = current; - current = recycled; - } - return previous[columns - 1]!; -} - -function trigramDice(params: ToolSearchSimilarityParams): number { - if (params.queryTrigrams.size === 0 || params.candidateTrigrams.size === 0) return 0; - let intersection = 0; - for (const value of params.queryTrigrams) if (params.candidateTrigrams.has(value)) intersection++; - return (2 * intersection) / (params.queryTrigrams.size + params.candidateTrigrams.size); -} - -type ToolSearchSimilarityMemoOps = { - score(params: { query: string; candidate: string }): number; -}; - -function createToolSearchSimilarityMemo(): ToolSearchSimilarityMemoOps { - const scores = new Map(); - const trigramSets = new Map>(); - - function trigrams(value: string): Set { - const cached = trigramSets.get(value); - if (cached) return cached; - const result = - value.length < 3 - ? new Set() - : new Set( - Array.from({ length: value.length - 2 }, (_, index) => value.slice(index, index + 3)), - ); - trigramSets.set(value, result); - return result; - } - - function score(params: { query: string; candidate: string }): number { - const key = `${params.query}\0${params.candidate}`; - const cached = scores.get(key); - if (cached !== undefined) return cached; - const result = scoreToolSearchSimilarity({ - ...params, - queryTrigrams: trigrams(params.query), - candidateTrigrams: trigrams(params.candidate), - }); - scores.set(key, result); - return result; - } - - return { score }; -} - -export const ToolSearchSingleton = defineSingleton({ - params: {}, - deps: { text: createToolSearchText() }, - compile(document: ToolSearchDocument): CompiledToolSearchDocument { - const fields = document.fields.map((field) => { - const normalizedValue = this.deps.text.normalize({ value: field.value }); - return { - ...field, - normalizedValue, - tokens: this.deps.text.tokensFromNormalized({ value: normalizedValue }), - }; - }); - return { - id: document.id, - fields, - tokens: [...new Set(fields.flatMap((field) => field.tokens))], - }; - }, - - matchField(params: { - field: CompiledToolSearchField; - normalizedQuery: string; - queryTokens: string[]; - similarity: ToolSearchSimilarityMemoOps; - }): ToolSearchFieldMatch | undefined { - if (!params.field.normalizedValue) return undefined; - const tokenScores = params.queryTokens.map((queryToken) => - Math.max( - 0, - ...params.field.tokens.map((candidate) => - params.similarity.score({ query: queryToken, candidate }), - ), - ), - ); - const matchedTokens = tokenScores.filter((score) => score > 0); - const phraseBonus = params.field.normalizedValue.includes(params.normalizedQuery) ? 0.5 : 0; - if (matchedTokens.length === 0 && phraseBonus === 0) return undefined; - const coverage = matchedTokens.length / params.queryTokens.length; - const similarity = - tokenScores.reduce((total, score) => total + score, 0) / params.queryTokens.length; - const score = params.field.weight * (similarity + coverage * 0.35 + phraseBonus); - if (score < 0.5) return undefined; - return { - field: params.field.name, - value: params.field.value, - score: roundToolSearchScore(score), - }; - }, - - rank(params: { - document: CompiledToolSearchDocument; - normalizedQuery: string; - queryTokens: string[]; - similarity: ToolSearchSimilarityMemoOps; - }): ToolSearchRankedDocument | undefined { - const rawMatches = params.document.fields - .map((field) => - ToolSearchSingleton.matchField({ - field, - normalizedQuery: params.normalizedQuery, - queryTokens: params.queryTokens, - similarity: params.similarity, - }), - ) - .filter((match): match is ToolSearchFieldMatch => match !== undefined) - .toSorted((left, right) => right.score - left.score); - const matches = [ - ...new Map(rawMatches.map((match) => [match.field, match] as const)).values(), - ].toSorted((left, right) => right.score - left.score); - if (matches.length === 0) return undefined; - const coveredTokens = params.queryTokens.filter((queryToken) => - params.document.tokens.some( - (candidate) => params.similarity.score({ query: queryToken, candidate }) > 0, - ), - ).length; - const coverage = coveredTokens / params.queryTokens.length; - const coverageMultiplier = 0.2 + coverage ** 2 * 0.8; - return { - id: params.document.id, - score: roundToolSearchScore( - matches.reduce((total, match) => total + match.score, 0) * coverageMultiplier, - ), - matches: matches.slice(0, 3), - }; - }, - - search(params: { - query: string; - documents: ToolSearchDocument[]; - limit: number; - }): ToolSearchRankedDocument[] { - const normalizedQuery = this.deps.text.normalize({ value: params.query }); - const queryTokens = this.deps.text.tokensFromNormalized({ - value: normalizedQuery, - removeStopWords: true, - }); - if (!normalizedQuery || queryTokens.length === 0) return []; - - const similarity = createToolSearchSimilarityMemo(); - return params.documents - .map((document) => - ToolSearchSingleton.rank({ - document: ToolSearchSingleton.compile(document), - normalizedQuery, - queryTokens, - similarity, - }), - ) - .filter((result): result is ToolSearchRankedDocument => result !== undefined) - .toSorted((left, right) => right.score - left.score || left.id.localeCompare(right.id)) - .slice(0, params.limit); - }, -}); - -type ToolSearchEngineConstructor = { - new (): ToolSearchEngineClass; - readonly prototype: ToolSearchEngineClass; -}; - -export type ToolSearchEngineClass = { - search(params: { - query: string; - documents: ToolSearchDocument[]; - limit: number; - }): ToolSearchRankedDocument[]; -}; - -const ToolSearchEngineClassAdapter = function constructToolSearchEngine(): void {}; -Object.defineProperty(ToolSearchEngineClassAdapter, "name", { value: "ToolSearchEngineClass" }); -Object.defineProperty(ToolSearchEngineClassAdapter.prototype, "search", { - configurable: true, - value: function search(params: { - query: string; - documents: ToolSearchDocument[]; - limit: number; - }): ToolSearchRankedDocument[] { - return ToolSearchSingleton.search(params); - }, - writable: true, -}); - -export const ToolSearchEngineClass = - ToolSearchEngineClassAdapter as unknown as ToolSearchEngineConstructor; - -export const toolSearchEngine = new ToolSearchEngineClass(); diff --git a/src/tools/env.ts b/src/tools/env.ts deleted file mode 100644 index d202169..0000000 --- a/src/tools/env.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./management/tool-env"; diff --git a/src/tools/execution/tool-runner.ts b/src/tools/execution/tool-runner.ts deleted file mode 100644 index 2be1404..0000000 --- a/src/tools/execution/tool-runner.ts +++ /dev/null @@ -1,439 +0,0 @@ -import { readFile } from "node:fs/promises"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass, rigErrors } from "../../errors/RigError"; -import { envelopeFactory } from "../../runtime/envelope"; -import { RigLoggerFactoryClass } from "../../runtime/logger"; -import { RigOutputTruncatorClass } from "../../runtime/truncation"; -import { ToolCollectionServiceClass, type CollectionHandle } from "../collection"; -import { ToolDatabaseServiceClass, UnavailableToolDatabaseFactoryClass } from "../db"; -import { type ManagedRigToolCache, ToolCacheServiceClass } from "../cache"; -import { type ManagedRigToolKvStore, ToolKvStoreServiceClass } from "../kv"; -import { ToolLoaderClass } from "../loader"; -import { schemaRenderer } from "../schema"; -import { createRigToolKit } from "../sdk"; -import { commandIds, type CommandDefinition, type RigToolDatabase } from "../types"; - -export type RunCommandOptions = ConfigOptions & { - input?: string; - inputFile?: string; - args?: string[]; - dryRun?: boolean; - pipeContext?: Record; -}; - -export type RunCommandResult = { - envelope: unknown; - exitCode: number; -}; - -export class RunInputReaderClass { - async read( - command: CommandDefinition, - options: RunCommandOptions, - ): Promise<{ value: unknown; source: string }> { - const resolver = new PipelineReferenceResolverClass(options.pipeContext ?? {}); - const args = resolver.interpolateStringArray(options.args ?? []); - const inputSources = [ - Boolean(options.input), - Boolean(options.inputFile), - args.length > 0, - ].filter(Boolean).length; - - if (inputSources > 1) { - throw new RigErrorClass( - "INPUT_ERROR", - "Use args, --input, or --input-file, not more than one.", - ); - } - - if (options.inputFile) { - /* v8 ignore next -- the built CLI always uses Bun.file */ - const source = - typeof Bun !== "undefined" - ? await Bun.file(options.inputFile).text() - : await readFile(options.inputFile, "utf8"); - const value = this.parseJson(source); - return { value: resolver.interpolate(value), source: `--input-file ${options.inputFile}` }; - } - - if (options.input) { - return { - value: resolver.interpolate(this.parseJson(options.input)), - source: `--input '${options.input}'`, - }; - } - - if (args.length > 0) { - const parser = new InputArgumentParserClass(command.input); - return { value: parser.parse(args), source: new InputSourceRendererClass().render(args) }; - } - - return { value: {}, source: "--input '{}'" }; - } - - private parseJson(source: string): unknown { - try { - return JSON.parse(source); - } catch (error) { - const message = error instanceof Error ? error.message : String(error); - throw new RigErrorClass("INPUT_ERROR", "Input JSON is invalid.", { message }); - } - } -} - -class PipelineReferenceResolverClass { - constructor(private readonly context: Record) {} - - interpolateStringArray(values: string[]): string[] { - return values.map((value) => String(this.interpolateString(value))); - } - - interpolate(value: unknown): unknown { - if (typeof value === "string") return this.interpolateString(value); - if (Array.isArray(value)) return value.map((item) => this.interpolate(item)); - if (this.isRecord(value)) { - return Object.fromEntries( - Object.entries(value).map(([key, item]) => [key, this.interpolate(item)]), - ); - } - return value; - } - - private interpolateString(value: string): unknown { - const exact = value.match(/^@([A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_.-]+))?$/); - if (exact) return this.lookup(exact[1], exact[2]); - return value.replace(/@([A-Za-z0-9_-]+)(?:\.([A-Za-z0-9_.-]+))?/g, (_match, id, path) => - String(this.lookup(id, path)), - ); - } - - private lookup(id: string, path?: string): unknown { - if (!(id in this.context)) { - throw new RigErrorClass("INPUT_ERROR", `Pipeline reference is unknown: @${id}`, { - reference: `@${id}`, - available: Object.keys(this.context), - }); - } - - let value = this.context[id]; - for (const part of path?.split(".").filter(Boolean) ?? []) { - if (!this.isRecord(value) && !Array.isArray(value)) { - throw new RigErrorClass("INPUT_ERROR", `Pipeline reference cannot access: @${id}.${path}`, { - reference: `@${id}.${path}`, - missing: part, - }); - } - value = (value as Record)[part]; - if (value === undefined) { - throw new RigErrorClass("INPUT_ERROR", `Pipeline reference is missing: @${id}.${path}`, { - reference: `@${id}.${path}`, - missing: part, - }); - } - } - return value; - } - - private isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null; - } -} - -class InputArgumentParserClass { - constructor(private readonly schema: unknown) {} - - parse(args: string[]): unknown { - if (args.length === 1) { - const maybeJson = this.tryParseJson(args[0]); - if (maybeJson.parsed && this.shouldUseJsonValue(maybeJson.value)) return maybeJson.value; - } - - if (args.every((arg) => arg.includes("="))) { - return Object.fromEntries(args.map((arg) => this.parseKeyValueArg(arg))); - } - - const fields = this.inputFieldNames(); - if (fields.length === 0) { - if (args.length === 1) return this.parseScalar(args[0]); - throw new RigErrorClass( - "INPUT_ERROR", - "This command does not declare positional input fields.", - { - args, - }, - ); - } - - if (args.length > fields.length) { - throw new RigErrorClass("INPUT_ERROR", "Too many positional arguments.", { - args, - expectedFields: fields, - }); - } - - return Object.fromEntries(args.map((arg, index) => [fields[index], this.parseScalar(arg)])); - } - - private parseKeyValueArg(arg: string): [string, unknown] { - const separatorIndex = arg.indexOf("="); - const key = arg.slice(0, separatorIndex); - const value = arg.slice(separatorIndex + 1); - if (!key) { - throw new RigErrorClass("INPUT_ERROR", "Argument keys must not be empty.", { arg }); - } - return [key, this.parseScalar(value)]; - } - - private parseScalar(value: string): unknown { - const maybeJson = this.tryParseJson(value); - return maybeJson.parsed ? maybeJson.value : value; - } - - private tryParseJson(value: string): { parsed: true; value: unknown } | { parsed: false } { - try { - return { parsed: true, value: JSON.parse(value) }; - } catch { - return { parsed: false }; - } - } - - private shouldUseJsonValue(value: unknown): boolean { - return typeof value === "object" && value !== null; - } - - private inputFieldNames(): string[] { - const jsonSchema = schemaRenderer.toJsonSchema(this.schema); - if (!this.isRecord(jsonSchema) || !this.isRecord(jsonSchema.properties)) return []; - return Object.keys(jsonSchema.properties); - } - - private isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); - } -} - -class InputSourceRendererClass { - render(args: string[]): string { - return args.map((arg) => this.shellArg(arg)).join(" "); - } - - private shellArg(value: string): string { - if (/^[A-Za-z0-9_./:=@+-]+$/.test(value)) return value; - return `'${value.replaceAll("'", "'\\''")}'`; - } -} - -class ZodErrorPresenterClass { - present(error: { flatten: () => unknown; issues?: unknown[] }): Record { - return { - ...this.flattenObject(error.flatten()), - /* v8 ignore next */ - issues: (error.issues ?? []).map((issue) => this.presentIssue(issue)), - }; - } - - private presentIssue(issue: unknown): Record { - /* v8 ignore next */ - if (!this.isRecord(issue)) return { message: String(issue) }; - return Object.fromEntries( - Object.entries({ - /* v8 ignore next */ - path: Array.isArray(issue.path) ? issue.path.join(".") : "", - code: issue.code, - message: issue.message, - expected: issue.expected, - received: issue.received, - }).filter(([, value]) => value !== undefined), - ); - } - - private flattenObject(value: unknown): Record { - /* v8 ignore next */ - return this.isRecord(value) ? value : {}; - } - - private isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); - } -} - -class DryRunPresenterClass { - present(params: { - tool: string; - command: string; - input: unknown; - inputSource: string; - }): Record { - return { - dryRun: true, - wouldRun: false, - tool: params.tool, - command: params.command, - id: commandIds.from(params.tool, params.command), - input: params.input, - commandLine: `rig run ${commandIds.from(params.tool, params.command)} ${params.inputSource}`, - }; - } -} - -class ToolExecutionSessionClass { - private readonly commands = new Map< - string, - Awaited> - >(); - - constructor(readonly loader: ToolLoaderClass) {} - - async loadCommand(toolName: string, commandName: string) { - const id = commandIds.from(toolName, commandName); - const cached = this.commands.get(id); - if (cached) return cached; - const loaded = await this.loader.loadCommand(toolName, commandName); - this.commands.set(id, loaded); - return loaded; - } -} - -export class ToolRunnerClass { - private readonly databases = new ToolDatabaseServiceClass(); - private readonly kvStores = new ToolKvStoreServiceClass(); - private readonly caches = new ToolCacheServiceClass(); - private readonly collections = new ToolCollectionServiceClass(); - private readonly loggerFactory: RigLoggerFactoryClass; - private readonly inputReader = new RunInputReaderClass(); - private readonly outputTruncator = new RigOutputTruncatorClass(); - private readonly unavailableDatabases = new UnavailableToolDatabaseFactoryClass(); - - constructor(private readonly options: ConfigOptions = {}) { - this.loggerFactory = new RigLoggerFactoryClass(options); - } - - async run( - toolName: string, - commandName: string, - options: RunCommandOptions = {}, - ): Promise { - const session = new ToolExecutionSessionClass(new ToolLoaderClass(this.options)); - return this.runInSession(session, toolName, commandName, options); - } - - private async runInSession( - session: ToolExecutionSessionClass, - toolName: string, - commandName: string, - options: RunCommandOptions, - ): Promise { - let db: RigToolDatabase | undefined; - let kv: ManagedRigToolKvStore | undefined; - let cache: ManagedRigToolCache | undefined; - let collectionHandles: Record> | undefined; - const log = this.loggerFactory.tool(toolName, commandName); - try { - const { tool, command } = await session.loadCommand(toolName, commandName); - const input = await this.inputReader.read(command, options); - - const inputResult = command.input.safeParse(input.value); - if (!inputResult.success) { - throw new RigErrorClass( - "VALIDATION_ERROR", - "Invalid input.", - new ZodErrorPresenterClass().present(inputResult.error), - ); - } - - if (options.dryRun) { - const data = new DryRunPresenterClass().present({ - tool: toolName, - command: commandName, - input: inputResult.data, - inputSource: input.source, - }); - return { - envelope: envelopeFactory.success({ - data: await this.outputTruncator.truncateData(data), - }), - exitCode: 0, - }; - } - - const rig = createRigToolKit(this.options, { - run: (nestedTool, nestedCommand, nestedOptions) => - this.runInSession(session, nestedTool, nestedCommand, nestedOptions), - }); - db = await this.databases.setup(tool); - kv = await this.kvStores.setup(tool); - cache = await this.caches.setup(tool, log); - collectionHandles = await this.collections.setup(tool); - log.info("Tool command started."); - const data = await command.run({ - input: inputResult.data, - env: tool.env, - processEnv: process.env, - cwd: process.cwd(), - db: db ?? this.unavailableDatabases.create(toolName), - kv, - cache, - log, - rig, - collections: collectionHandles ?? {}, - }); - - const outputResult = command.output.safeParse(data); - if (!outputResult.success) { - throw new RigErrorClass( - "OUTPUT_VALIDATION_ERROR", - "Command returned invalid output.", - new ZodErrorPresenterClass().present(outputResult.error), - ); - } - - log.info("Tool command finished."); - return { - envelope: envelopeFactory.success({ - data: await this.outputTruncator.truncateData(outputResult.data), - }), - exitCode: 0, - }; - } catch (error) { - const rigError = rigErrors.from(error); - log.error({ err: rigError }, "Tool command failed."); - return { - envelope: envelopeFactory.error({ - code: rigError.code, - message: rigError.message, - details: rigError.details, - }), - exitCode: 1, - }; - } finally { - this.collections.close(collectionHandles); - this.closeCache(cache); - this.closeKv(kv); - this.closeDatabase(db); - } - } - - private closeCache(cache: ManagedRigToolCache | undefined): void { - try { - cache?.close(); - } catch { - // The command already finished. Ignore close failures so they do not mask command results. - } - } - - private closeKv(kv: ManagedRigToolKvStore | undefined): void { - try { - kv?.close(); - } catch { - // The command already finished. Ignore close failures so they do not mask command results. - } - } - - private closeDatabase(db: RigToolDatabase | undefined): void { - try { - db?.close(false); - } catch { - // The command already finished. Ignore close failures so they do not mask command results. - } - } -} diff --git a/src/tools/find.ts b/src/tools/find.ts deleted file mode 100644 index 5760846..0000000 --- a/src/tools/find.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./presentation/tool-find"; diff --git a/src/tools/help-topics.ts b/src/tools/help-topics.ts deleted file mode 100644 index 30bd9e2..0000000 --- a/src/tools/help-topics.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./presentation/help-topics"; diff --git a/src/tools/help.ts b/src/tools/help.ts deleted file mode 100644 index d48b31d..0000000 --- a/src/tools/help.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./presentation/tool-help"; diff --git a/src/tools/identifiers.test.ts b/src/tools/identifiers.test.ts deleted file mode 100644 index 2ed4f65..0000000 --- a/src/tools/identifiers.test.ts +++ /dev/null @@ -1,150 +0,0 @@ -import { existsSync } from "node:fs"; -import { mkdtemp, rm } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, describe, expect, it } from "vite-plus/test"; -import { RigPathsClass } from "../config/paths"; -import { ToolCreatorClass } from "./create"; -import { - collectionNames, - CollectionNameClass, - CollectionNameSingleton, - commandNames, - CommandNameClass, - CommandNameSingleton, - commandTargets, - CommandTargetClass, - CommandTargetSingleton, - toolNames, - ToolNameClass, - ToolNameSingleton, -} from "./identifiers"; -import { commandIds, CommandIdsClass, CommandIdsSingleton } from "./types"; - -describe("routed identifiers", () => { - const homes: string[] = []; - - afterEach(async () => { - await Promise.all(homes.splice(0).map((home) => rm(home, { recursive: true, force: true }))); - }); - - it("builds routed identifier and command id behavior", () => { - const ToolNames = ToolNameSingleton; - const CommandNames = CommandNameSingleton; - const CollectionNames = CollectionNameSingleton; - const CommandTargets = CommandTargetSingleton; - const CommandIds = CommandIdsSingleton; - - expect(ToolNames.create({ value: "clean-image-metadata" })).toEqual({ - value: "clean-image-metadata", - }); - expect(ToolNames.parse({ value: "tool_2" })).toEqual({ value: "tool_2" }); - expect(CommandNames.create({ value: "get-status" })).toEqual({ value: "get-status" }); - expect(CommandNames.parse({ value: "get_status" })).toEqual({ value: "get_status" }); - expect(CollectionNames.parse({ value: "release-notes" })).toEqual({ - value: "release-notes", - }); - expect(CommandTargets.create({ tool: "../raw", command: "nested.command" })).toEqual({ - tool: "../raw", - command: "nested.command", - id: "../raw.nested.command", - }); - expect(CommandTargets.from({ tool: "clean-image-metadata", command: "get_metadata" })).toEqual({ - tool: "clean-image-metadata", - command: "get_metadata", - id: "clean-image-metadata.get_metadata", - }); - expect(CommandTargets.parse({ id: "socials.post-message" })).toEqual({ - tool: "socials", - command: "post-message", - id: "socials.post-message", - }); - expect(CommandIds.from({ tool: "clean-image-metadata", command: "get_metadata" })).toBe( - "clean-image-metadata.get_metadata", - ); - expect(() => CommandTargets.from({ tool: "../tool", command: "run" })).toThrow( - "Invalid tool name", - ); - expect(() => CommandTargets.parse({ id: "tool.bad.command" })).toThrow( - "Command id must use .", - ); - }); - - it("preserves constructible adapters, enumerable data, and instanceof behavior", () => { - const toolName = new ToolNameClass("clean-image-metadata"); - const commandName = new CommandNameClass("get-status"); - const collectionName = new CollectionNameClass("release-notes"); - const rawTarget = new CommandTargetClass("../raw", "nested.command"); - const ids = new CommandIdsClass(); - - expect(toolName).toBeInstanceOf(ToolNameClass); - expect(toolName.parse("tool_2")).toBeInstanceOf(ToolNameClass); - expect(commandName).toBeInstanceOf(CommandNameClass); - expect(collectionName).toBeInstanceOf(CollectionNameClass); - expect(rawTarget).toBeInstanceOf(CommandTargetClass); - expect(rawTarget.from("tool", "command")).toBeInstanceOf(CommandTargetClass); - expect(rawTarget.parse("tool.command")).toBeInstanceOf(CommandTargetClass); - expect(ids).toBeInstanceOf(CommandIdsClass); - expect(Object.keys(toolName)).toEqual(["value"]); - expect(Object.keys(commandName)).toEqual(["value"]); - expect(Object.keys(collectionName)).toEqual(["value"]); - expect(Object.keys(rawTarget)).toEqual(["tool", "command", "id"]); - expect(Object.keys(ids)).toEqual([]); - expect(rawTarget).toEqual({ - tool: "../raw", - command: "nested.command", - id: "../raw.nested.command", - }); - expect(ids.from("clean-image-metadata", "get_metadata")).toBe( - "clean-image-metadata.get_metadata", - ); - expect(toolNames).toBeInstanceOf(ToolNameClass); - expect(commandNames).toBeInstanceOf(CommandNameClass); - expect(collectionNames).toBeInstanceOf(CollectionNameClass); - expect(commandTargets).toBeInstanceOf(CommandTargetClass); - expect(commandIds).toBeInstanceOf(CommandIdsClass); - }); - - it("rejects tool and command names that cannot be routed safely", () => { - for (const name of ["", ".", "..", "../tool", "/tmp/tool", "folder/tool", "folder\\tool"]) { - expect(() => new ToolNameClass(name)).toThrow("Invalid tool name"); - } - expect(() => new ToolNameClass(null as never)).toThrow("Invalid tool name"); - - for (const name of ["", ".", "..", "nested.command", "folder/command", "folder\\command"]) { - expect(() => new CommandNameClass(name)).toThrow("Invalid command name"); - } - expect(() => new CommandNameClass(null as never)).toThrow("Invalid command name"); - expect(() => new CollectionNameClass("../notes")).toThrow("Invalid collection name"); - expect(() => new CollectionNameClass(null as never)).toThrow("Invalid collection name"); - }); - - it("parses exactly one valid tool and command segment", () => { - expect(commandTargets.parse("socials.post-message")).toMatchObject({ - id: "socials.post-message", - tool: "socials", - command: "post-message", - }); - expect(commandTargets.from("socials", "post-message").id).toBe("socials.post-message"); - - for (const id of ["tool", ".command", "tool.", "tool.bad.command", "../tool.command"]) { - expect(() => commandTargets.parse(id)).toThrow("Command id must use ."); - } - expect(() => commandTargets.parse(undefined as never)).toThrow( - "Command id must use .", - ); - }); - - it("prevents tool creation outside the base registry", async () => { - const home = await mkdtemp(join(tmpdir(), "rig-identifier-test-")); - homes.push(home); - const creator = new ToolCreatorClass({ homeDir: home }); - - await expect(creator.create("../escaped")).rejects.toThrow("Invalid tool name"); - await expect(creator.create("/tmp/escaped")).rejects.toThrow("Invalid tool name"); - - const paths = new RigPathsClass({ homeDir: home }); - expect(existsSync(join(paths.rigDir, "escaped"))).toBe(false); - expect(existsSync(join(home, "escaped"))).toBe(false); - }); -}); diff --git a/src/tools/identifiers.ts b/src/tools/identifiers.ts deleted file mode 100644 index 5dc120b..0000000 --- a/src/tools/identifiers.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./domain/identifiers"; diff --git a/src/tools/inspect.ts b/src/tools/inspect.ts deleted file mode 100644 index ed1fe8f..0000000 --- a/src/tools/inspect.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./presentation/tool-inspector"; diff --git a/src/tools/kv.ts b/src/tools/kv.ts deleted file mode 100644 index 78054e0..0000000 --- a/src/tools/kv.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../persistence/kv/tool-kv"; diff --git a/src/tools/list.ts b/src/tools/list.ts deleted file mode 100644 index 1921b19..0000000 --- a/src/tools/list.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./presentation/tool-list"; diff --git a/src/tools/loader.ts b/src/tools/loader.ts deleted file mode 100644 index 2e18180..0000000 --- a/src/tools/loader.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./loading/tool-loader"; diff --git a/src/tools/loading/tool-loader.ts b/src/tools/loading/tool-loader.ts deleted file mode 100644 index 5e9f8f6..0000000 --- a/src/tools/loading/tool-loader.ts +++ /dev/null @@ -1,550 +0,0 @@ -import { existsSync } from "node:fs"; -import { readFile, stat } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import { pathToFileURL } from "node:url"; -import { defineRuntime, defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import { ToolDiscoveryServiceClass, type DiscoveredTool } from "../../registry/discover"; -import { collectionNames, commandNames, toolNames } from "../identifiers"; -import { createRigToolKit } from "../sdk"; -import { commandIds, type CommandDefinition, type LoadedTool, type ToolDefinition } from "../types"; - -export type LoadedToolDefinition = Omit; - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function hasSafeParse(params: { value: unknown }): boolean { - return isRecord(params.value) && typeof params.value.safeParse === "function"; -} - -function validateSchema(params: { value: unknown; role: string; id: string }): void { - if (!hasSafeParse({ value: params.value })) { - throw new RigErrorClass( - "TOOL_INVALID", - `Command ${params.id} needs a Zod schema for ${params.role}.`, - { expected: "rig.z.object({ ... })" }, - ); - } -} - -function validateExamples(params: { value: unknown; path: string }): void { - if (params.value === undefined) return; - if (!Array.isArray(params.value)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid examples at ${params.path}.`, { - expected: "array", - }); - } - - for (const [index, example] of params.value.entries()) { - if (!isRecord(example)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid example at ${params.path}[${index}].`, { - expected: "object", - }); - } - if (typeof example.title !== "string" || example.title.length === 0) { - throw new RigErrorClass( - "TOOL_INVALID", - `Invalid example title at ${params.path}[${index}].`, - { expected: "non-empty string" }, - ); - } - if (typeof example.text !== "string" || example.text.length === 0) { - throw new RigErrorClass("TOOL_INVALID", `Invalid example text at ${params.path}[${index}].`, { - expected: "non-empty string", - }); - } - } -} - -function validateToolName(params: { name: string }): void { - toolNames.parse(params.name); -} - -function validateCommandName(params: { name: string }): void { - commandNames.parse(params.name); -} - -function validateCommand(params: { commandName: string; value: unknown; toolName: string }): void { - validateCommandName({ name: params.commandName }); - const id = commandIds.from(params.toolName, params.commandName); - - if (!isRecord(params.value)) { - throw new RigErrorClass("TOOL_INVALID", `Invalid command ${id}.`, { expected: "object" }); - } - if (typeof params.value.description !== "string" || params.value.description.length === 0) { - throw new RigErrorClass("TOOL_INVALID", `Command ${id} needs a description.`, { - expected: "non-empty string", - }); - } - validateSchema({ value: params.value.input, role: "input", id }); - validateSchema({ value: params.value.output, role: "output", id }); - validateExamples({ value: params.value.examples, path: `${id}.examples` }); - if (typeof params.value.run !== "function") { - throw new RigErrorClass("TOOL_INVALID", `Command ${id} needs a run function.`, { - expected: "function", - }); - } -} - -function validateToolDefinition(params: { value: unknown; expectedName?: string }): ToolDefinition { - if (!isRecord(params.value)) { - throw new RigErrorClass("TOOL_INVALID", "Tool default export must be an object."); - } - const name = - typeof params.value.name === "string" && params.value.name.length > 0 - ? params.value.name - : params.expectedName; - if (!name) { - throw new RigErrorClass("TOOL_INVALID", "Tool needs a name.", { - expected: "non-empty string or a discovered tool folder", - }); - } - - validateToolName({ name }); - if (params.expectedName && name !== params.expectedName) { - throw new RigErrorClass( - "TOOL_INVALID", - `Tool name does not match its folder: ${name} should be ${params.expectedName}.`, - { expectedName: params.expectedName, actualName: name }, - ); - } - if (typeof params.value.description !== "string" || params.value.description.length === 0) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${name} needs a description.`, { - expected: "non-empty string", - }); - } - if (params.value.setupDb !== undefined && typeof params.value.setupDb !== "function") { - throw new RigErrorClass("TOOL_INVALID", `Tool ${name} setupDb must be a function.`, { - expected: "function", - }); - } - if (params.value.env !== undefined) - validateSchema({ value: params.value.env, role: "env", id: name }); - if (params.value.collections !== undefined) { - if (!isRecord(params.value.collections)) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${name} needs a collections object.`, { - expected: "object", - }); - } - for (const collectionName of Object.keys(params.value.collections)) { - collectionNames.parse(collectionName); - } - } - if (!isRecord(params.value.commands)) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${name} needs a commands object.`, { - expected: "object", - }); - } - const entries = Object.entries(params.value.commands); - if (entries.length === 0) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${name} must define at least one command.`); - } - for (const [commandName, command] of entries) { - validateCommand({ commandName, value: command, toolName: name }); - } - - return (params.value.name === name ? params.value : { ...params.value, name }) as ToolDefinition; -} - -export const ToolDefinitionValidatorSingleton = defineSingleton({ - params: {}, - deps: {}, - validateToolName, - validateCommandName, - validateToolDefinition, -}); - -export type ToolDefinitionValidatorClass = { - validateToolName(name: string): void; - validateCommandName(name: string): void; - validateToolDefinition(value: unknown, expectedName?: string): ToolDefinition; -}; - -type ToolDefinitionValidatorConstructor = { - new (): ToolDefinitionValidatorClass; - readonly prototype: ToolDefinitionValidatorClass; -}; - -const ToolDefinitionValidatorClassAdapter = function constructToolDefinitionValidator(): void {}; -Object.defineProperty(ToolDefinitionValidatorClassAdapter, "name", { - value: "ToolDefinitionValidatorClass", -}); -Object.defineProperties(ToolDefinitionValidatorClassAdapter.prototype, { - validateToolName: { - configurable: true, - value: function validateLegacyToolName(_name: string) { - return ToolDefinitionValidatorSingleton.validateToolName({ name: _name }); - }, - writable: true, - }, - validateCommandName: { - configurable: true, - value: function validateLegacyCommandName(name: string) { - return ToolDefinitionValidatorSingleton.validateCommandName({ name }); - }, - writable: true, - }, - validateToolDefinition: { - configurable: true, - value: function validateLegacyToolDefinition(value: unknown, expectedName?: string) { - return ToolDefinitionValidatorSingleton.validateToolDefinition({ value, expectedName }); - }, - writable: true, - }, -}); - -export const ToolDefinitionValidatorClass = - ToolDefinitionValidatorClassAdapter as unknown as ToolDefinitionValidatorConstructor; - -function parseEnvValue(params: { value: string }): string { - const trimmed = params.value.trim(); - if (trimmed.length < 2) return trimmed; - const quote = trimmed[0]; - if ((quote !== '"' && quote !== "'") || trimmed.at(-1) !== quote) return trimmed; - const inner = trimmed.slice(1, -1); - if (quote === "'") return inner; - return inner - .replaceAll("\\n", "\n") - .replaceAll("\\r", "\r") - .replaceAll("\\t", "\t") - .replaceAll('\\"', '"') - .replaceAll("\\\\", "\\"); -} - -function parseEnvLine(params: { - line: string; - path: string; - lineNumber: number; -}): { key: string; value: string } | undefined { - const trimmed = params.line.trim(); - if (!trimmed || trimmed.startsWith("#")) return undefined; - const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(trimmed); - if (!match) { - throw new RigErrorClass("TOOL_INVALID", "Invalid .env line.", { - path: params.path, - line: params.lineNumber, - }); - } - return { key: match[1]!, value: parseEnvValue({ value: match[2]! }) }; -} - -function parseEnvFile(params: { source: string; path: string }): Record { - const env: Record = {}; - for (const [index, line] of params.source.split(/\r?\n/).entries()) { - const parsed = parseEnvLine({ line, path: params.path, lineNumber: index + 1 }); - if (parsed) env[parsed.key] = parsed.value; - } - return env; -} - -export const ToolEnvFileParserSingleton = defineSingleton({ - params: {}, - deps: {}, - parse: parseEnvFile, -}); - -type ToolEnvLoaderDeps = { - exists: (path: string) => Promise; - readText: (path: string) => Promise; - dirname: typeof dirname; - join: typeof join; -}; - -function bunFile(): - | ((path: string) => { exists(): Promise; text(): Promise }) - | undefined { - const candidate = (globalThis as typeof globalThis & { Bun?: { file?: unknown } }).Bun?.file; - /* v8 ignore next */ - return typeof candidate === "function" ? (candidate as never) : undefined; -} - -const ToolEnvLoaderProductionDeps: ToolEnvLoaderDeps = { - async exists(path) { - const bun = bunFile(); - /* v8 ignore next 3 */ - if (bun) return await bun(path).exists(); - return existsSync(path); - }, - async readText(path) { - const bun = bunFile(); - /* v8 ignore next */ - if (bun) return await bun(path).text(); - return await readFile(path, "utf8"); - }, - dirname, - join, -}; - -export class ToolEnvLoaderService extends defineService({ - params: {}, - deps: ToolEnvLoaderProductionDeps, -}) { - public async load(params: { - tool: DiscoveredTool; - definition: ToolDefinition; - }): Promise { - const envPath = this.deps.join(this.deps.dirname(params.tool.toolPath), ".env"); - const fileExists = await this.deps.exists(envPath); - if (!params.definition.env) { - if (fileExists) { - throw new RigErrorClass( - "TOOL_INVALID", - `Tool ${params.definition.name} has .env but no env schema.`, - { path: envPath }, - ); - } - return {}; - } - const rawEnv = fileExists - ? ToolEnvFileParserSingleton.parse({ - source: await this.deps.readText(envPath), - path: envPath, - }) - : {}; - const result = params.definition.env.safeParse(rawEnv); - if (!result.success) { - throw new RigErrorClass("TOOL_INVALID", `Tool ${params.definition.name} env is invalid.`, { - path: envPath, - errors: result.error.flatten(), - }); - } - return result.data; - } -} - -export const ToolEnvLoader = new ToolEnvLoaderService(); - -type ToolLoaderDeps = { - findTool: ToolDiscoveryServiceClass["find"]; - stat: typeof stat; - pathToFileUrl: typeof pathToFileURL; - importModule: (url: string) => Promise; - createToolKit: typeof createRigToolKit; - loadEnv: ToolEnvLoaderService["load"]; -}; - -function createToolLoaderDeps(options: ConfigOptions): ToolLoaderDeps { - const discovery = new ToolDiscoveryServiceClass(options); - const envLoader = new ToolEnvLoaderService({ params: {}, deps: ToolEnvLoaderProductionDeps }); - return { - findTool: discovery.find.bind(discovery), - stat, - pathToFileUrl: pathToFileURL, - importModule(url) { - return import(url) as Promise; - }, - createToolKit: createRigToolKit, - loadEnv(params) { - return envLoader.load(params); - }, - }; -} - -const ToolLoaderProductionDeps = createToolLoaderDeps({}); - -export class ToolLoaderService extends defineRuntime({ - params: {}, - deps: ToolLoaderProductionDeps, -}) { - private readonly discoveredTools = new Map(); - private readonly definitions = new Map< - string, - { modifiedAtMs: number; size: number; value: LoadedToolDefinition } - >(); - - private async find(params: { name: string }): Promise { - const cached = this.discoveredTools.get(params.name); - if (cached) return cached; - const discovered = await this.deps.findTool(params.name); - this.discoveredTools.set(params.name, discovered); - return discovered; - } - - private async evaluateModuleDefault(params: { - value: unknown; - toolName: string; - }): Promise { - const awaitedValue = await Promise.resolve(params.value); - if (typeof awaitedValue !== "function") return awaitedValue; - try { - return await awaitedValue(this.deps.createToolKit()); - } catch (error) { - throw new RigErrorClass( - "TOOL_INVALID", - `Could not evaluate tool factory ${params.toolName}.`, - { tool: params.toolName, error }, - ); - } - } - - public async loadDefinitionDiscovered(params: { - tool: DiscoveredTool; - }): Promise { - const metadata = await this.deps.stat(params.tool.toolPath); - const cached = this.definitions.get(params.tool.toolPath); - if (cached?.modifiedAtMs === metadata.mtimeMs && cached.size === metadata.size) { - return cached.value; - } - - const url = `${this.deps.pathToFileUrl(params.tool.toolPath).href}?rig=${metadata.mtimeMs}-${metadata.size}`; - let moduleValue: unknown; - try { - moduleValue = await this.deps.importModule(url); - } catch (error) { - throw new RigErrorClass("TOOL_INVALID", `Could not load tool ${params.tool.name}.`, { - path: params.tool.toolPath, - error, - }); - } - - const moduleRecord = moduleValue as { default?: unknown }; - const definitionValue = await this.evaluateModuleDefault({ - value: moduleRecord.default, - toolName: params.tool.name, - }); - const definition = ToolDefinitionValidatorSingleton.validateToolDefinition({ - value: definitionValue, - expectedName: params.tool.name, - }); - const loaded = { name: definition.name, path: params.tool.toolPath, definition }; - this.definitions.set(params.tool.toolPath, { - modifiedAtMs: metadata.mtimeMs, - size: metadata.size, - value: loaded, - }); - return loaded; - } - - public async loadDefinition(params: { name: string }): Promise { - return await this.loadDefinitionDiscovered({ tool: await this.find(params) }); - } - - public async loadDiscovered(params: { tool: DiscoveredTool }): Promise { - const loaded = await this.loadDefinitionDiscovered(params); - const env = await this.deps.loadEnv({ tool: params.tool, definition: loaded.definition }); - return { ...loaded, env }; - } - - public async load(params: { name: string }): Promise { - ToolDefinitionValidatorSingleton.validateToolName(params); - return await this.loadDiscovered({ tool: await this.find(params) }); - } - - public async loadCommand(params: { toolName: string; commandName: string }) { - const tool = await this.load({ name: params.toolName }); - ToolDefinitionValidatorSingleton.validateCommandName({ name: params.commandName }); - const command = tool.definition.commands[params.commandName]; - if (!command) { - throw new RigErrorClass( - "COMMAND_NOT_FOUND", - `Command not found: ${commandIds.from(params.toolName, params.commandName)}`, - { - tool: params.toolName, - command: params.commandName, - available: Object.keys(tool.definition.commands), - }, - ); - } - return { tool, commandName: params.commandName, command }; - } - - public validateToolName(params: { name: string }): void { - validateToolName(params); - } - - public validateCommandName(params: { name: string }): void { - validateCommandName(params); - } -} - -export const ToolLoader = new ToolLoaderService(); - -export type ToolLoaderClass = { - validateToolName(name: string): void; - validateCommandName(name: string): void; - loadDefinition(name: string): Promise; - loadDefinitionDiscovered(tool: DiscoveredTool): Promise; - loadDiscovered(tool: DiscoveredTool): Promise; - load(name: string): Promise; - loadCommand( - toolName: string, - commandName: string, - ): Promise<{ - tool: LoadedTool; - commandName: string; - command: CommandDefinition; - }>; -}; - -type ToolLoaderConstructor = { - new (options?: ConfigOptions): ToolLoaderClass; - readonly prototype: ToolLoaderClass; -}; - -type ToolLoaderAdapter = ToolLoaderClass & { readonly resource: ToolLoaderService }; - -const ToolLoaderClassAdapter = function constructToolLoader( - this: ToolLoaderAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolLoaderService({ params: {}, deps: createToolLoaderDeps(options) }), - }); -}; -Object.defineProperty(ToolLoaderClassAdapter, "name", { value: "ToolLoaderClass" }); -Object.defineProperties(ToolLoaderClassAdapter.prototype, { - validateToolName: { - configurable: true, - value: function validateLegacyToolName(this: ToolLoaderAdapter, name: string) { - return this.resource.validateToolName({ name }); - }, - writable: true, - }, - validateCommandName: { - configurable: true, - value: function validateLegacyCommandName(this: ToolLoaderAdapter, name: string) { - return this.resource.validateCommandName({ name }); - }, - writable: true, - }, - loadDefinition: { - configurable: true, - value: function loadDefinition(this: ToolLoaderAdapter, name: string) { - return this.resource.loadDefinition({ name }); - }, - writable: true, - }, - loadDefinitionDiscovered: { - configurable: true, - value: function loadDefinitionDiscovered(this: ToolLoaderAdapter, tool: DiscoveredTool) { - return this.resource.loadDefinitionDiscovered({ tool }); - }, - writable: true, - }, - loadDiscovered: { - configurable: true, - value: function loadDiscovered(this: ToolLoaderAdapter, tool: DiscoveredTool) { - return this.resource.loadDiscovered({ tool }); - }, - writable: true, - }, - load: { - configurable: true, - value: function load(this: ToolLoaderAdapter, name: string) { - return this.resource.load({ name }); - }, - writable: true, - }, - loadCommand: { - configurable: true, - value: function loadCommand(this: ToolLoaderAdapter, toolName: string, commandName: string) { - return this.resource.loadCommand({ toolName, commandName }); - }, - writable: true, - }, -}); - -export const ToolLoaderClass = ToolLoaderClassAdapter as unknown as ToolLoaderConstructor; diff --git a/src/tools/management/tool-env.ts b/src/tools/management/tool-env.ts deleted file mode 100644 index 2fb653c..0000000 --- a/src/tools/management/tool-env.ts +++ /dev/null @@ -1,343 +0,0 @@ -import { existsSync } from "node:fs"; -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import { defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import { ToolLoaderClass } from "../loader"; -import { schemaRenderer } from "../schema"; - -export type ToolEnvEntry = { - key: string; - required: boolean; - set: boolean; -}; - -export type ToolEnvResult = { - tool: string; - envPath: string; - updated: boolean; - entries: ToolEnvEntry[]; - updatedKeys: string[]; - removedKeys: string[]; -}; - -type ToolEnvTarget = { tool: string }; - -function parseTarget(params: { value: string }): ToolEnvTarget { - if (params.value && !params.value.includes(".")) return { tool: params.value }; - throw new RigErrorClass("INPUT_ERROR", `Env target must use : ${params.value}`); -} - -function parseKey(params: { key: string }): string { - if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(params.key)) { - throw new RigErrorClass( - "INPUT_ERROR", - `Env key must be a valid shell variable name: ${params.key}`, - ); - } - return params.key; -} - -function parseKeys(params: { keys: string[] }): string[] { - if (params.keys.length === 0) { - throw new RigErrorClass("INPUT_ERROR", "Env remove expects at least one KEY."); - } - return params.keys.map(function parseEnvKey(key) { - return parseKey({ key }); - }); -} - -function parseAssignment(params: { assignment: string }): [string, string] { - const separator = params.assignment.indexOf("="); - const key = params.assignment.slice(0, separator); - if (separator <= 0 || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) { - throw new RigErrorClass( - "INPUT_ERROR", - `Env assignment must use KEY=VALUE: ${params.assignment}`, - ); - } - return [key, params.assignment.slice(separator + 1)]; -} - -function parseAssignments(params: { assignments: string[] }): Record { - return Object.fromEntries( - params.assignments.map(function parseEnvAssignment(assignment) { - return parseAssignment({ assignment }); - }), - ); -} - -function parseValue(params: { value: string }): string { - const trimmed = params.value.trim(); - if (trimmed.length < 2) return trimmed; - if (trimmed[0] === '"' && trimmed.at(-1) === '"') return JSON.parse(trimmed) as string; - if (trimmed[0] === "'" && trimmed.at(-1) === "'") return trimmed.slice(1, -1); - return trimmed; -} - -function parseLine(params: { - line: string; - path: string; - lineNumber: number; -}): { key: string; value: string } | undefined { - const trimmed = params.line.trim(); - if (!trimmed || trimmed.startsWith("#")) return undefined; - - const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(trimmed); - if (!match) { - throw new RigErrorClass("TOOL_INVALID", "Invalid .env line.", { - path: params.path, - line: params.lineNumber, - }); - } - return { key: match[1]!, value: parseValue({ value: match[2]! }) }; -} - -function parseDocument(params: { source: string; path: string }): Record { - const values: Record = {}; - for (const [index, line] of params.source.split(/\r?\n/).entries()) { - const parsed = parseLine({ line, path: params.path, lineNumber: index + 1 }); - if (parsed) values[parsed.key] = parsed.value; - } - return values; -} - -function renderValue(params: { value: string }): string { - if (/^[A-Za-z0-9_./:=@%+,-]*$/.test(params.value)) return params.value; - return JSON.stringify(params.value); -} - -function serializeDocument(params: { values: Record }): string { - const lines = Object.entries(params.values) - .toSorted(function compareKeys([left], [right]) { - return left.localeCompare(right); - }) - .map(function renderEntry([key, value]) { - return `${key}=${renderValue({ value })}`; - }); - return `${lines.join("\n")}\n`; -} - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function schemaEntries(params: { - schema: unknown; - values: Record; -}): ToolEnvEntry[] { - const jsonSchema = schemaRenderer.toJsonSchema(params.schema); - if (!isRecord(jsonSchema) || !isRecord(jsonSchema.properties)) return []; - - /* v8 ignore next */ - const required = Array.isArray(jsonSchema.required) ? jsonSchema.required : []; - return Object.keys(jsonSchema.properties) - .toSorted() - .map(function createEntry(key) { - return { - key, - required: required.includes(key), - set: params.values[key] !== undefined, - }; - }); -} - -export const ToolEnvSingleton = defineSingleton({ - params: {}, - deps: {}, - parseTarget, - parseKeys, - parseAssignments, - parseDocument, - serializeDocument, - schemaEntries, -}); - -type ToolEnvSchema = { - safeParse(value: unknown): { success: boolean; error?: { flatten(): unknown } }; -}; - -function validateEnv(params: { - target: ToolEnvTarget; - envPath: string; - schema: ToolEnvSchema; - values: Record; -}): void { - const validation = params.schema.safeParse(params.values); - if (validation.success) return; - throw new RigErrorClass("TOOL_INVALID", `Tool ${params.target.tool} env would be invalid.`, { - path: params.envPath, - errors: validation.error?.flatten(), - }); -} - -type ToolEnvServiceDeps = { - loadDefinition: ToolLoaderClass["loadDefinition"]; - exists: typeof existsSync; - mkdir: typeof mkdir; - readFile: typeof readFile; - writeFile: typeof writeFile; - dirname: typeof dirname; - join: typeof join; -}; - -function createToolEnvServiceDeps(options: ConfigOptions): ToolEnvServiceDeps { - const loader = new ToolLoaderClass(options); - return { - loadDefinition: loader.loadDefinition.bind(loader), - exists: existsSync, - mkdir, - readFile, - writeFile, - dirname, - join, - }; -} - -const ToolEnvServiceProductionDeps = createToolEnvServiceDeps({}); - -export class ToolEnvService extends defineService({ - params: {}, - deps: ToolEnvServiceProductionDeps, -}) { - private async readEnv(params: { path: string }): Promise> { - if (!this.deps.exists(params.path)) return {}; - return ToolEnvSingleton.parseDocument({ - source: await this.deps.readFile(params.path, "utf8"), - path: params.path, - }); - } - - private async writeEnv(params: { path: string; values: Record }): Promise { - await this.deps.mkdir(this.deps.dirname(params.path), { recursive: true }); - await this.deps.writeFile( - params.path, - ToolEnvSingleton.serializeDocument({ values: params.values }), - "utf8", - ); - } - - private result(params: { - target: ToolEnvTarget; - envPath: string; - updated: boolean; - values: Record; - schema: unknown; - updatedKeys: string[]; - removedKeys: string[]; - }): ToolEnvResult { - return { - tool: params.target.tool, - envPath: params.envPath, - updated: params.updated, - entries: ToolEnvSingleton.schemaEntries({ - schema: params.schema, - values: params.values, - }), - updatedKeys: params.updatedKeys.toSorted(), - removedKeys: params.removedKeys.toSorted(), - }; - } - - public async configure(params: { - targetValue: string; - assignments: string[]; - }): Promise { - const assignments = params.assignments; - const target = ToolEnvSingleton.parseTarget({ value: params.targetValue }); - const loaded = await this.deps.loadDefinition(target.tool); - if (!loaded.definition.env) { - throw new RigErrorClass( - "TOOL_INVALID", - `Tool ${target.tool} does not define an env schema.`, - { tool: target.tool }, - ); - } - - const envPath = this.deps.join(this.deps.dirname(loaded.path), ".env"); - const existing = await this.readEnv({ path: envPath }); - if (assignments.length === 0) { - return this.result({ - target, - envPath, - updated: false, - values: existing, - schema: loaded.definition.env, - updatedKeys: [], - removedKeys: [], - }); - } - - if (assignments[0] === "remove") { - const keys = ToolEnvSingleton.parseKeys({ keys: assignments.slice(1) }); - const nextValues = { ...existing }; - const removedKeys = keys.filter(function existingKey(key) { - return key in nextValues; - }); - for (const key of keys) delete nextValues[key]; - validateEnv({ target, envPath, schema: loaded.definition.env, values: nextValues }); - if (removedKeys.length > 0) await this.writeEnv({ path: envPath, values: nextValues }); - return this.result({ - target, - envPath, - updated: removedKeys.length > 0, - values: nextValues, - schema: loaded.definition.env, - updatedKeys: [], - removedKeys, - }); - } - - const updates = ToolEnvSingleton.parseAssignments({ assignments }); - const nextValues = { ...existing, ...updates }; - validateEnv({ target, envPath, schema: loaded.definition.env, values: nextValues }); - await this.writeEnv({ path: envPath, values: nextValues }); - return this.result({ - target, - envPath, - updated: true, - values: nextValues, - schema: loaded.definition.env, - updatedKeys: Object.keys(updates), - removedKeys: [], - }); - } -} - -export const ToolEnv = new ToolEnvService(); - -export type ToolEnvServiceClass = { - configure(targetValue: string, assignments?: string[]): Promise; -}; - -type ToolEnvServiceConstructor = { - new (options?: ConfigOptions): ToolEnvServiceClass; - readonly prototype: ToolEnvServiceClass; -}; - -type ToolEnvServiceAdapter = ToolEnvServiceClass & { readonly resource: ToolEnvService }; - -const ToolEnvServiceClassAdapter = function constructToolEnvService( - this: ToolEnvServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolEnvService({ params: {}, deps: createToolEnvServiceDeps(options) }), - }); -}; -Object.defineProperty(ToolEnvServiceClassAdapter, "name", { value: "ToolEnvServiceClass" }); -Object.defineProperty(ToolEnvServiceClassAdapter.prototype, "configure", { - configurable: true, - value: function configure( - this: ToolEnvServiceAdapter, - targetValue: string, - assignments: string[] = [], - ) { - return this.resource.configure({ targetValue, assignments }); - }, - writable: true, -}); - -export const ToolEnvServiceClass = - ToolEnvServiceClassAdapter as unknown as ToolEnvServiceConstructor; diff --git a/src/tools/management/tool-files.ts b/src/tools/management/tool-files.ts deleted file mode 100644 index 93282b3..0000000 --- a/src/tools/management/tool-files.ts +++ /dev/null @@ -1,261 +0,0 @@ -import { existsSync } from "node:fs"; -import { mkdir, rm, writeFile } from "node:fs/promises"; -import { join } from "node:path"; -import { defineService, defineSingleton } from "../../define"; -import { RigConfigStoreClass, type ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import { RigToolEntryFiles, ToolDiscoveryServiceClass } from "../../registry/discover"; -import { CurrentRigToolApiVersion } from "../domain/tool-api"; -import { ToolLoaderClass } from "../loader"; - -export type ToolFileResult = { - name: string; - toolDir: string; - toolPath: string; -}; - -function generatedToolSource(_params: {}): string { - return `// rig:tool-api-version ${CurrentRigToolApiVersion} -export default (rig: RigToolKit) => rig.defineTool({ - description: "Describe what this tool does.", - commands: (command) => ({ - example: command({ - description: "Example command. Replace this with a real command.", - input: rig.z.object({ - text: rig.z.string().default("example"), - }), - output: rig.z.object({ - text: rig.z.string(), - }), - examples: [ - { - title: "Run the example command", - text: "Use this to verify Rig can run a local command.", - input: { text: "example" }, - output: { text: "example" }, - }, - ], - run: async ({ input }) => { - return { - text: input.text, - }; - }, - }), - }), -}); -`; -} - -export const ToolTemplateSingleton = defineSingleton({ - params: {}, - deps: {}, - render: generatedToolSource, -}); - -type ToolCreatorDeps = { - ensureConfig: RigConfigStoreClass["ensure"]; - resolveBaseRegistry: RigConfigStoreClass["resolvedBaseRegistry"]; - validateToolName: ToolLoaderClass["validateToolName"]; - exists: typeof existsSync; - mkdir: typeof mkdir; - writeFile: typeof writeFile; - join: typeof join; - generatedToolSource: typeof generatedToolSource; -}; - -function createToolCreatorDeps(options: ConfigOptions): ToolCreatorDeps { - const configStore = new RigConfigStoreClass(options); - const loader = new ToolLoaderClass(options); - return { - ensureConfig: configStore.ensure.bind(configStore), - resolveBaseRegistry: configStore.resolvedBaseRegistry.bind(configStore), - validateToolName: loader.validateToolName.bind(loader), - exists: existsSync, - mkdir, - writeFile, - join, - generatedToolSource, - }; -} - -const ToolCreatorProductionDeps = createToolCreatorDeps({}); - -export class ToolCreatorService extends defineService({ - params: {}, - deps: ToolCreatorProductionDeps, -}) { - public async create(params: { name: string }) { - this.deps.validateToolName(params.name); - const config = await this.deps.ensureConfig(); - const baseRegistry = this.deps.resolveBaseRegistry(config); - const toolDir = this.deps.join(baseRegistry, params.name); - const toolPath = this.deps.join(toolDir, RigToolEntryFiles[0]); - - if (this.deps.exists(toolDir)) { - throw new RigErrorClass("TOOL_INVALID", `Tool already exists: ${params.name}`, { - path: toolDir, - }); - } - - await this.deps.mkdir(toolDir, { recursive: true }); - await this.deps.writeFile(toolPath, this.deps.generatedToolSource({}), "utf8"); - - return { - name: params.name, - command: "example", - id: `${params.name}.example`, - toolDir, - toolPath, - files: [toolPath], - }; - } -} - -export const ToolCreator = new ToolCreatorService(); - -export type ToolCreatorClass = { - create(name: string): ReturnType; -}; - -type ToolCreatorConstructor = { - new (options?: ConfigOptions): ToolCreatorClass; - readonly prototype: ToolCreatorClass; -}; - -type ToolCreatorAdapter = ToolCreatorClass & { readonly resource: ToolCreatorService }; - -const ToolCreatorClassAdapter = function constructToolCreator( - this: ToolCreatorAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolCreatorService({ params: {}, deps: createToolCreatorDeps(options) }), - }); -}; -Object.defineProperty(ToolCreatorClassAdapter, "name", { value: "ToolCreatorClass" }); -Object.defineProperty(ToolCreatorClassAdapter.prototype, "create", { - configurable: true, - value: function create(this: ToolCreatorAdapter, name: string) { - return this.resource.create({ name }); - }, - writable: true, -}); - -export const ToolCreatorClass = ToolCreatorClassAdapter as unknown as ToolCreatorConstructor; - -type ToolFileServiceDeps = { - validateToolName: ToolLoaderClass["validateToolName"]; - findTool: ToolDiscoveryServiceClass["find"]; -}; - -function createToolFileServiceDeps(options: ConfigOptions): ToolFileServiceDeps { - const discovery = new ToolDiscoveryServiceClass(options); - const loader = new ToolLoaderClass(options); - return { - validateToolName: loader.validateToolName.bind(loader), - findTool: discovery.find.bind(discovery), - }; -} - -const ToolFileServiceProductionDeps = createToolFileServiceDeps({}); - -export class ToolFileService extends defineService({ - params: {}, - deps: ToolFileServiceProductionDeps, -}) { - public async path(params: { name: string }): Promise { - this.deps.validateToolName(params.name); - const tool = await this.deps.findTool(params.name); - return { name: tool.name, toolDir: tool.toolDir, toolPath: tool.toolPath }; - } -} - -export const ToolFile = new ToolFileService(); - -export type ToolFileServiceClass = { - path(name: string): Promise; -}; - -type ToolFileServiceConstructor = { - new (options?: ConfigOptions): ToolFileServiceClass; - readonly prototype: ToolFileServiceClass; -}; - -type ToolFileServiceAdapter = ToolFileServiceClass & { - readonly resource: ToolFileService; -}; - -const ToolFileServiceClassAdapter = function constructToolFileService( - this: ToolFileServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolFileService({ params: {}, deps: createToolFileServiceDeps(options) }), - }); -}; -Object.defineProperty(ToolFileServiceClassAdapter, "name", { value: "ToolFileServiceClass" }); -Object.defineProperty(ToolFileServiceClassAdapter.prototype, "path", { - configurable: true, - value: function path(this: ToolFileServiceAdapter, name: string) { - return this.resource.path({ name }); - }, - writable: true, -}); - -export const ToolFileServiceClass = - ToolFileServiceClassAdapter as unknown as ToolFileServiceConstructor; - -type ToolRemoverDeps = { - findPath: ToolFileServiceClass["path"]; - rm: typeof rm; -}; - -function createToolRemoverDeps(options: ConfigOptions): ToolRemoverDeps { - const files = new ToolFileServiceClass(options); - return { findPath: files.path.bind(files), rm }; -} - -const ToolRemoverProductionDeps = createToolRemoverDeps({}); - -export class ToolRemoverService extends defineService({ - params: {}, - deps: ToolRemoverProductionDeps, -}) { - public async remove(params: { name: string }): Promise { - const tool = await this.deps.findPath(params.name); - await this.deps.rm(tool.toolDir, { recursive: true, force: false }); - return tool; - } -} - -export const ToolRemover = new ToolRemoverService(); - -export type ToolRemoverClass = { - remove(name: string): Promise; -}; - -type ToolRemoverConstructor = { - new (options?: ConfigOptions): ToolRemoverClass; - readonly prototype: ToolRemoverClass; -}; - -type ToolRemoverAdapter = ToolRemoverClass & { readonly resource: ToolRemoverService }; - -const ToolRemoverClassAdapter = function constructToolRemover( - this: ToolRemoverAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolRemoverService({ params: {}, deps: createToolRemoverDeps(options) }), - }); -}; -Object.defineProperty(ToolRemoverClassAdapter, "name", { value: "ToolRemoverClass" }); -Object.defineProperty(ToolRemoverClassAdapter.prototype, "remove", { - configurable: true, - value: function remove(this: ToolRemoverAdapter, name: string) { - return this.resource.remove({ name }); - }, - writable: true, -}); - -export const ToolRemoverClass = ToolRemoverClassAdapter as unknown as ToolRemoverConstructor; diff --git a/src/tools/management/tool-typecheck.ts b/src/tools/management/tool-typecheck.ts deleted file mode 100644 index 7875255..0000000 --- a/src/tools/management/tool-typecheck.ts +++ /dev/null @@ -1,268 +0,0 @@ -import { mkdir, writeFile } from "node:fs/promises"; -import { createRequire } from "node:module"; -import { dirname, join } from "node:path"; -import ts from "typescript"; -import { defineService } from "../../define"; -import { RigConfigStoreClass, type ConfigOptions } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import { RigErrorClass } from "../../errors/RigError"; -import { ToolDiscoveryServiceClass } from "../../registry/discover"; -import { ToolLoaderClass } from "../loader"; - -export type ToolTypecheckResult = { - ok: boolean; - exitCode: number; - stdout: string; - stderr: string; - checked: string[]; - tsconfigPath: string; -}; - -type ToolTypecheckConfigStore = Pick; -type ToolTypecheckDiscovery = Pick; -type ToolTypecheckLoader = Pick; -type ToolTypecheckPaths = Pick< - RigPathsClass, - "runtimeDir" | "runtimeGlobalsPath" | "runtimeToolTsconfigPath" | "runtimeTypesPath" ->; - -type ToolTypecheckServiceDeps = { - createConfigStore: (options: ConfigOptions) => ToolTypecheckConfigStore; - createDiscovery: (options: ConfigOptions) => ToolTypecheckDiscovery; - createLoader: (options: ConfigOptions) => ToolTypecheckLoader; - createPaths: (options: ConfigOptions) => ToolTypecheckPaths; - mkdir: typeof mkdir; - writeFile: typeof writeFile; - dirname: typeof dirname; - join: typeof join; - resolvePackage: (specifier: string) => string; - typescript: typeof ts; - cwd: () => string; -}; - -const requireFromModule = createRequire(import.meta.url); - -const ToolTypecheckServiceProductionDeps: ToolTypecheckServiceDeps = { - createConfigStore(options) { - return new RigConfigStoreClass(options); - }, - createDiscovery(options) { - return new ToolDiscoveryServiceClass(options); - }, - createLoader(options) { - return new ToolLoaderClass(options); - }, - createPaths(options) { - return new RigPathsClass(options); - }, - mkdir, - writeFile, - dirname, - join, - resolvePackage(specifier) { - return requireFromModule.resolve(specifier); - }, - typescript: ts, - cwd: process.cwd.bind(process), -}; - -export class ToolTypecheckService extends defineService({ - params: {} as ConfigOptions, - deps: ToolTypecheckServiceProductionDeps, -}) { - private get configStore() { - return this.deps.createConfigStore(this.params); - } - private get discovery() { - return this.deps.createDiscovery(this.params); - } - private get loader() { - return this.deps.createLoader(this.params); - } - private get paths() { - return this.deps.createPaths(this.params); - } - - private formatHost(_params: {}): ts.FormatDiagnosticsHost { - return { - getCanonicalFileName(fileName) { - return fileName; - }, - getCurrentDirectory: () => this.deps.cwd(), - getNewLine() { - return "\n"; - }, - }; - } - - public parseHost(_params: {}): ts.ParseConfigFileHost { - return { - ...this.deps.typescript.sys, - onUnRecoverableConfigFileDiagnostic: (diagnostic) => { - throw new RigErrorClass("TYPECHECK_ERROR", "Unable to parse generated Rig tool tsconfig.", { - diagnostic: this.deps.typescript.formatDiagnostic(diagnostic, this.formatHost({})), - }); - }, - }; - } - - private packagePath(params: { packageName: string }): string { - return this.deps.dirname(this.deps.resolvePackage(`${params.packageName}/package.json`)); - } - - private tsconfig(params: { toolPaths: string[] }) { - const bunTypesRoot = this.deps.dirname(this.packagePath({ packageName: "@types/bun" })); - const zodRoot = this.packagePath({ packageName: "zod" }); - return { - compilerOptions: { - target: "ES2022", - module: "ESNext", - moduleResolution: "Bundler", - strict: true, - noEmit: true, - skipLibCheck: true, - allowImportingTsExtensions: true, - lib: ["ES2023", "DOM"], - jsx: "preserve", - types: ["bun"], - typeRoots: [bunTypesRoot], - paths: { - zod: [this.deps.join(zodRoot, "index.d.ts")], - }, - }, - files: [this.paths.runtimeGlobalsPath, this.paths.runtimeTypesPath, ...params.toolPaths], - }; - } - - public runTypeScript(params: { tsconfigPath: string }): { - stdout: string; - exitCode: number; - } { - const host = this.parseHost({}); - const parsedConfig = this.deps.typescript.getParsedCommandLineOfConfigFile( - params.tsconfigPath, - {}, - host, - ); - /* v8 ignore next */ - if (!parsedConfig) { - return { - stdout: "Unable to parse generated Rig tool tsconfig.\n", - exitCode: 2, - }; - } - - const program = this.deps.typescript.createProgram( - parsedConfig.fileNames, - parsedConfig.options, - ); - const diagnostics = [ - ...parsedConfig.errors, - ...this.deps.typescript.getPreEmitDiagnostics(program), - ]; - if (diagnostics.length === 0) return { stdout: "", exitCode: 0 }; - return { - stdout: this.deps.typescript.formatDiagnostics(diagnostics, this.formatHost({})), - exitCode: 2, - }; - } - - public async typecheck(params: { toolName?: string }): Promise { - await this.configStore.ensure(); - const discovered = await this.discovery.discover(); - const selected = params.toolName - ? discovered.filter(function selectTool(tool) { - return tool.name === params.toolName; - }) - : discovered; - - if (params.toolName) { - this.loader.validateToolName(params.toolName); - if (selected.length === 0) { - throw new RigErrorClass("TOOL_NOT_FOUND", `Tool not found: ${params.toolName}`, { - available: discovered.map(function toolName(tool) { - return tool.name; - }), - }); - } - } - - await this.deps.mkdir(this.paths.runtimeDir, { recursive: true }); - const checked = selected.map(function toolPath(tool) { - return tool.toolPath; - }); - await this.deps.writeFile( - this.paths.runtimeToolTsconfigPath, - JSON.stringify(this.tsconfig({ toolPaths: checked }), null, 2), - "utf8", - ); - - const result = this.runTypeScript({ tsconfigPath: this.paths.runtimeToolTsconfigPath }); - return { - ok: result.exitCode === 0, - exitCode: result.exitCode, - stdout: result.stdout, - stderr: "", - checked, - tsconfigPath: this.paths.runtimeToolTsconfigPath, - }; - } -} - -export const ToolTypecheck = new ToolTypecheckService(); - -export type ToolTypecheckServiceClass = { - typecheck(toolName?: string): Promise; -}; - -type ToolTypecheckServiceConstructor = { - new (options?: ConfigOptions): ToolTypecheckServiceClass; - readonly prototype: ToolTypecheckServiceClass; -}; - -type ToolTypecheckServiceAdapter = ToolTypecheckServiceClass & { - readonly resource: ToolTypecheckService; - runTypeScript(tsconfigPath: string): { stdout: string; exitCode: number }; - parseHost(): ts.ParseConfigFileHost; -}; - -const ToolTypecheckServiceClassAdapter = function constructToolTypecheckService( - this: ToolTypecheckServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolTypecheckService({ - params: options, - deps: ToolTypecheckServiceProductionDeps, - }), - }); -}; -Object.defineProperty(ToolTypecheckServiceClassAdapter, "name", { - value: "ToolTypecheckServiceClass", -}); -Object.defineProperties(ToolTypecheckServiceClassAdapter.prototype, { - typecheck: { - configurable: true, - value: function typecheck(this: ToolTypecheckServiceAdapter, toolName?: string) { - return this.resource.typecheck({ toolName }); - }, - writable: true, - }, - runTypeScript: { - configurable: true, - value: function runTypeScript(this: ToolTypecheckServiceAdapter, tsconfigPath: string) { - return this.resource.runTypeScript({ tsconfigPath }); - }, - writable: true, - }, - parseHost: { - configurable: true, - value: function parseHost(this: ToolTypecheckServiceAdapter) { - return this.resource.parseHost({}); - }, - writable: true, - }, -}); - -export const ToolTypecheckServiceClass = - ToolTypecheckServiceClassAdapter as unknown as ToolTypecheckServiceConstructor; diff --git a/src/tools/migration/tool-api-migration.test.ts b/src/tools/migration/tool-api-migration.test.ts deleted file mode 100644 index 5dd0992..0000000 --- a/src/tools/migration/tool-api-migration.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { describe, expect, it, vi } from "vite-plus/test"; -import { ToolDiscoveryServiceClass } from "../../registry/discover"; -import { ToolApiMigration } from "./tool-api-migration"; - -describe("Tool API migration", () => { - it("creates the production discovery service", async () => { - const discover = vi - .spyOn(ToolDiscoveryServiceClass.prototype, "discover") - .mockResolvedValue([]); - - await expect(ToolApiMigration.inspect({})).resolves.toEqual({ - currentVersion: 2, - ready: true, - migrations: [], - unsupported: [], - }); - expect(discover).toHaveBeenCalledWith({}); - }); -}); diff --git a/src/tools/migration/tool-api-migration.ts b/src/tools/migration/tool-api-migration.ts deleted file mode 100644 index f6307fe..0000000 --- a/src/tools/migration/tool-api-migration.ts +++ /dev/null @@ -1,212 +0,0 @@ -import { readFile } from "node:fs/promises"; -import { defineService } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { ToolDiscoveryServiceClass, type DiscoveredTool } from "../../registry/discover"; -import { CurrentRigToolApiVersion } from "../domain/tool-api"; - -export type ToolApiMigrationEntry = { - name: string; - path: string; - fromVersion: number; - toVersion: number; -}; - -export type ToolApiMigrationReport = { - currentVersion: number; - ready: boolean; - migrations: ToolApiMigrationEntry[]; - unsupported: ToolApiMigrationEntry[]; -}; - -export type ToolApiMigrationServiceDeps = { - discovery?: ToolDiscoveryServiceClass; - readSource?: (path: string) => Promise; -}; - -type ToolApiMigrationDeps = { - createDiscovery: (params: { options: ConfigOptions }) => ToolDiscoveryServiceClass; - readSource: (params: { path: string }) => Promise; -}; - -/* v8 ignore next 4 -- Bun path is covered by distribution integration */ -async function readToolSource(params: { path: string }): Promise { - return typeof Bun !== "undefined" - ? await Bun.file(params.path).text() - : await readFile(params.path, "utf8"); -} - -const ToolApiMigrationProductionDeps: ToolApiMigrationDeps = { - createDiscovery: function createDiscovery(params: { options: ConfigOptions }) { - return new ToolDiscoveryServiceClass(params.options); - }, - readSource: readToolSource, -}; - -function sourceVersion(params: { source: string }): number { - const match = /^\/\/ rig:tool-api-version (\d+)\s*$/m.exec(params.source); - if (!match) return 1; - const version = Number.parseInt(match[1]!, 10); - return Number.isSafeInteger(version) && version > 0 ? version : 1; -} - -function renderToolApiAgentInstructions(params: { report: ToolApiMigrationReport }): string { - if (params.report.ready) return ""; - - const lines = ["### Rig tool migration required", ""]; - if (params.report.migrations.length > 0) { - lines.push( - `Migrate these tools to Rig tool API v${params.report.currentVersion}:`, - "", - ...params.report.migrations.map( - (entry) => `- ${entry.name}: v${entry.fromVersion} to v${entry.toVersion} (${entry.path})`, - ), - "", - "For each tool:", - "", - "1. Export the factory directly as `(rig: RigToolKit) => rig.defineTool({ ... })`.", - "2. Remove the redundant `name`; Rig derives it from the folder.", - "3. Define `commands` as `(command) => ({ ... })` and replace each inline `rig.defineCommand({ ... })` with `command({ ... })`.", - "4. Move predeclared commands into the `commands` callback. Declare reusable command factories inside the callback or pass `command` into them.", - `5. Set the header to \`// rig:tool-api-version ${params.report.currentVersion}\`.`, - "6. Run `rig typecheck ` and execute the command examples before considering the migration complete.", - ); - } - - if (params.report.unsupported.length > 0) { - lines.push( - "", - "These tools declare a newer API than this Rig installation supports:", - "", - ...params.report.unsupported.map( - (entry) => `- ${entry.name}: v${entry.fromVersion} (${entry.path})`, - ), - "", - "Update Rig before editing or running these tools.", - ); - } - - return lines.join("\n"); -} - -function renderToolApiCli(params: { report: ToolApiMigrationReport }): string { - if (params.report.ready) { - return `All tools use Rig tool API v${params.report.currentVersion}.`; - } - return renderToolApiAgentInstructions(params).replace(/^### /, ""); -} - -export class ToolApiMigrationService extends defineService({ - params: {} as ConfigOptions, - deps: ToolApiMigrationProductionDeps, -}) { - private get discovery() { - return this.deps.createDiscovery({ options: this.params }); - } - - private async inspectTool(params: { tool: DiscoveredTool }): Promise { - const source = await this.deps.readSource({ path: params.tool.toolPath }); - return { - name: params.tool.name, - path: params.tool.toolPath, - fromVersion: sourceVersion({ source }), - toVersion: CurrentRigToolApiVersion, - }; - } - - public async inspect(params: { visibleFromPath?: string }): Promise { - const tools = await this.discovery.discover(params); - const entries = await Promise.all(tools.map((tool) => this.inspectTool({ tool }))); - const migrations = entries.filter((entry) => entry.fromVersion < entry.toVersion); - const unsupported = entries.filter((entry) => entry.fromVersion > entry.toVersion); - return { - currentVersion: CurrentRigToolApiVersion, - ready: migrations.length === 0 && unsupported.length === 0, - migrations, - unsupported, - }; - } - - public renderAgentInstructions(params: { report: ToolApiMigrationReport }): string { - return renderToolApiAgentInstructions(params); - } - - public renderCli(params: { report: ToolApiMigrationReport }): string { - return renderToolApiCli(params); - } -} - -export const ToolApiMigration = new ToolApiMigrationService(); - -export type ToolApiMigrationServiceClass = { - inspect(params?: { visibleFromPath?: string }): Promise; - renderAgentInstructions(params: { report: ToolApiMigrationReport }): string; - renderCli(params: { report: ToolApiMigrationReport }): string; -}; - -type ToolApiMigrationServiceConstructor = { - new (params: ConfigOptions, deps: ToolApiMigrationServiceDeps): ToolApiMigrationServiceClass; - readonly prototype: ToolApiMigrationServiceClass; -}; - -type ToolApiMigrationAdapter = ToolApiMigrationServiceClass & { - readonly resource: ToolApiMigrationService; -}; - -const ToolApiMigrationServiceClassAdapter = function constructToolApiMigration( - this: ToolApiMigrationAdapter, - params: ConfigOptions, - deps: ToolApiMigrationServiceDeps, -): void { - const resource = new ToolApiMigrationService({ - params, - deps: { - createDiscovery: function createDiscovery() { - return deps.discovery ?? new ToolDiscoveryServiceClass(params); - }, - readSource: deps.readSource - ? function readSource(readParams: { path: string }) { - return deps.readSource!(readParams.path); - } - : readToolSource, - }, - }); - Object.defineProperty(this, "resource", { value: resource }); -}; -Object.defineProperty(ToolApiMigrationServiceClassAdapter, "name", { - value: "ToolApiMigrationServiceClass", -}); -Object.defineProperties(ToolApiMigrationServiceClassAdapter.prototype, { - inspect: { - configurable: true, - value: function inspect( - this: ToolApiMigrationAdapter, - params: { visibleFromPath?: string } = {}, - ) { - return this.resource.inspect(params); - }, - writable: true, - }, - renderAgentInstructions: { - configurable: true, - value: function renderAgentInstructions( - this: ToolApiMigrationAdapter, - params: { report: ToolApiMigrationReport }, - ) { - return this.resource.renderAgentInstructions(params); - }, - writable: true, - }, - renderCli: { - configurable: true, - value: function renderCli( - this: ToolApiMigrationAdapter, - params: { report: ToolApiMigrationReport }, - ) { - return this.resource.renderCli(params); - }, - writable: true, - }, -}); - -export const ToolApiMigrationServiceClass = - ToolApiMigrationServiceClassAdapter as unknown as ToolApiMigrationServiceConstructor; diff --git a/src/tools/presentation/help-topics.test.ts b/src/tools/presentation/help-topics.test.ts deleted file mode 100644 index 350b326..0000000 --- a/src/tools/presentation/help-topics.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; -import { helpTopicService, HelpTopicServiceClass, HelpTopicSingleton } from "./help-topics"; - -const ExpectedTopics = [ - "collections", - "kv", - "cache", - "db", - "env", - "log", - "shell", - "run", - "tool", - "args", - "paths", -]; - -describe("help topics", () => { - it("lists, recognizes, and renders topics", () => { - const Topics = HelpTopicSingleton; - - expect(Topics.listTopics({})).toEqual(ExpectedTopics); - expect(Topics.isKnownTopic({ name: "collections" })).toBe(true); - expect(Topics.isKnownTopic({ name: "missing" })).toBe(false); - expect(Topics.render({ name: "collections" })).toContain("# Help • Collections"); - expect(Topics.render({ name: "missing" })).toBeUndefined(); - expect(Topics.renderTopicList({})).toContain("Usage: `rig help `"); - expect(Topics.renderTopicList({})).toContain(" collections Collections"); - }); - - it("preserves production and positional compatibility values", () => { - const adapter = new HelpTopicServiceClass(); - - expect(adapter).toBeInstanceOf(HelpTopicServiceClass); - expect(adapter.listTopics()).toEqual(HelpTopicSingleton.listTopics({})); - expect(adapter.isKnownTopic("kv")).toBe(true); - expect(adapter.render("kv")).toContain("# Help • Key-Value State"); - expect(adapter.renderTopicList()).toBe(helpTopicService.renderTopicList()); - }); -}); diff --git a/src/tools/presentation/tool-find.test.ts b/src/tools/presentation/tool-find.test.ts deleted file mode 100644 index 695d8cf..0000000 --- a/src/tools/presentation/tool-find.test.ts +++ /dev/null @@ -1,160 +0,0 @@ -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { ToolFindServiceClass } from "./tool-find"; - -class ToolFindTestHomeClass { - private readonly paths: string[] = []; - - public async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-find-test-")); - this.paths.push(home); - await this.writeTool({ - home, - name: "languagetool", - source: `export default (rig) => rig.defineTool({ - name: "languagetool", - description: "Check grammar, spelling, and writing style.", - commands: { - "check-file": rig.defineCommand({ - description: "Check grammar and spelling in a local Markdown file.", - input: rig.z.object({ input: rig.z.string().describe("Markdown file path") }), - output: rig.z.object({ issues: rig.z.number() }), - examples: [{ - title: "Proofread notes", - text: "Check a Markdown document for writing problems.", - input: { input: "notes.md" }, - output: { issues: 0 }, - }], - run: async () => ({ issues: 0 }), - }), - "check-text": rig.defineCommand({ - description: "Check a text string.", - input: rig.z.object({ - text: rig.z.string(), - categories: rig.z.array(rig.z.enum(["grammar", "style"]).describe("Rule category")), - strict: rig.z.boolean().optional(), - maxIssues: rig.z.number().optional(), - }), - output: rig.z.object({ issues: rig.z.number() }), - examples: [ - { title: "Default check", text: "Check without explicit input." }, - { - title: "Configured check", - text: "Check selected categories.", - input: { text: "Hello", categories: ["grammar"], strict: true, maxIssues: 2, extra: null }, - output: { issues: 0 }, - }, - ], - run: async () => ({ issues: 0 }), - }), - }, -});`, - }); - await this.writeTool({ - home, - name: "image", - source: `export default (rig) => rig.defineTool({ - name: "image", - description: "Resize and convert images.", - commands: { - convert: rig.defineCommand({ - description: "Resize an image and convert its format.", - input: rig.z.object({ input: rig.z.string(), width: rig.z.number().optional() }), - output: rig.z.object({ output: rig.z.string() }), - run: async () => ({ output: "image.webp" }), - }), - }, -});`, - }); - return home; - } - - public async cleanup(): Promise { - await Promise.all( - this.paths.splice(0).map((path) => rm(path, { recursive: true, force: true })), - ); - } - - private async writeTool(params: { home: string; name: string; source: string }): Promise { - const directory = join(params.home, "rig", "tools", params.name); - await mkdir(directory, { recursive: true }); - await writeFile(join(directory, "index.rig.ts"), params.source, "utf8"); - } -} - -describe("ToolFindServiceClass", () => { - const homes = new ToolFindTestHomeClass(); - - afterEach(async () => { - await homes.cleanup(); - }); - - test("finds commands through descriptions, examples, fields, and typos", async () => { - const homeDir = await homes.create(); - const service = new ToolFindServiceClass({ homeDir }); - const result = await service.find({ query: "grammer chek markdown" }); - - expect(result.results[0]).toMatchObject({ - rank: 1, - id: "languagetool.check-file", - tool: "languagetool", - command: "check-file", - }); - expect(result.results[0]?.runExample).toContain("notes.md"); - expect(result.results[0]?.matches.length).toBeGreaterThan(0); - expect(service.renderPlain({ data: result })).toContain("1. languagetool.check-file"); - }); - - test("supports tool filters, limits, input-field discovery, and no-result output", async () => { - const homeDir = await homes.create(); - const service = new ToolFindServiceClass({ homeDir }); - const filtered = await service.find({ - query: "width resize", - options: { tool: "image", limit: "1" }, - }); - - expect(filtered).toMatchObject({ tool: "image", limit: 1 }); - expect(filtered.results).toMatchObject([{ id: "image.convert" }]); - - const empty = await service.find({ query: "quantum payroll" }); - expect(service.renderPlain({ data: empty })).toBe( - 'No Rig commands found for "quantum payroll".', - ); - const scopedEmpty = await service.find({ - query: "quantum payroll", - options: { tool: "image" }, - }); - expect(service.renderPlain({ data: scopedEmpty })).toBe( - 'No Rig commands found for "quantum payroll" in tool image.', - ); - expect( - service.renderPlain({ - data: { - ...filtered, - results: filtered.results.map((result) => ({ - ...result, - runExample: "rig run image.convert input=a\r\nb\rc\td", - })), - }, - }), - ).toContain("input=a\\nb\\rc\\td"); - }); - - test("rejects invalid queries, limits, and tool filters", async () => { - const homeDir = await homes.create(); - const service = new ToolFindServiceClass({ homeDir }); - - await expect(service.find({ query: " " })).rejects.toThrow("Find query cannot be empty"); - await expect(service.find({ query: "image", options: { limit: 0 } })).rejects.toThrow( - "between 1 and 50", - ); - await expect(service.find({ query: "image", options: { limit: "1.5" } })).rejects.toThrow( - "between 1 and 50", - ); - await expect(service.find({ query: "image", options: { tool: "missing" } })).rejects.toThrow( - "Tool not found", - ); - }); -}); diff --git a/src/tools/presentation/tool-find.ts b/src/tools/presentation/tool-find.ts deleted file mode 100644 index eb8c3bf..0000000 --- a/src/tools/presentation/tool-find.ts +++ /dev/null @@ -1,322 +0,0 @@ -import { defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import { ToolDiscoveryServiceClass } from "../../registry/discover"; -import { toolSearchEngine, type ToolSearchDocument } from "../domain/tool-search"; -import { ToolNameClass } from "../identifiers"; -import { commandIds, type ToolExample } from "../types"; -import { ToolMetadataCacheClass, type ToolCommandMetadata } from "./tool-list"; - -export type ToolFindOptions = { - limit?: number | string; - tool?: string; -}; - -export type ToolFindMatch = { - field: string; - value: string; - score: number; -}; - -export type ToolFindResult = { - rank: number; - score: number; - id: string; - tool: string; - command: string; - description: string; - runExample: string; - matches: ToolFindMatch[]; -}; - -export type ToolFindData = { - query: string; - tool?: string; - limit: number; - results: ToolFindResult[]; -}; - -type SearchableCommand = { - id: string; - tool: string; - command: string; - description: string; - runExample: string; - document: ToolSearchDocument; -}; - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function searchableValue(params: { value: unknown }): string { - if (typeof params.value === "string") return params.value; - if (typeof params.value === "number" || typeof params.value === "boolean") { - return String(params.value); - } - if (Array.isArray(params.value)) { - return params.value - .map(function renderValue(item) { - return searchableValue({ value: item }); - }) - .join(" "); - } - if (isRecord(params.value)) { - return Object.entries(params.value) - .flatMap(function renderEntry([key, item]) { - return [key, searchableValue({ value: item })]; - }) - .join(" "); - } - return ""; -} - -function walkSchema(params: { schema: unknown; path: string[] }): ToolSearchDocument["fields"] { - /* v8 ignore next -- schemaRenderer always returns a JSON Schema object */ - if (!isRecord(params.schema)) return []; - const fields: ToolSearchDocument["fields"] = []; - if (params.path.length > 0) { - fields.push({ name: "input.field", value: params.path.join("."), weight: 3 }); - } - if (typeof params.schema.description === "string") { - fields.push({ name: "input.description", value: params.schema.description, weight: 3 }); - } - if (Array.isArray(params.schema.enum)) { - fields.push({ name: "input.enum", value: params.schema.enum.join(" "), weight: 1.5 }); - } - if (isRecord(params.schema.properties)) { - for (const [name, child] of Object.entries(params.schema.properties)) { - fields.push(...walkSchema({ schema: child, path: [...params.path, name] })); - } - } - if (params.schema.items !== undefined) { - fields.push(...walkSchema({ schema: params.schema.items, path: params.path })); - } - return fields; -} - -function exampleFields(params: { examples: ToolExample[] }): ToolSearchDocument["fields"] { - return params.examples.flatMap(function renderExample(example) { - return [ - { name: "example.title", value: example.title, weight: 4 }, - { name: "example.text", value: example.text, weight: 4 }, - ...(example.input === undefined - ? [] - : [ - { - name: "example.input", - value: searchableValue({ value: example.input }), - weight: 2, - }, - ]), - ]; - }); -} - -function metadataCommand(params: { - toolName: string; - toolDescription: string; - commandName: string; - command: ToolCommandMetadata; -}): SearchableCommand { - const id = commandIds.from(params.toolName, params.commandName); - return { - id, - tool: params.toolName, - command: params.commandName, - description: params.command.description, - runExample: params.command.runExample, - document: { - id, - fields: [ - { name: "command.id", value: id, weight: 12 }, - { name: "command.name", value: params.commandName, weight: 10 }, - { name: "tool.name", value: params.toolName, weight: 8 }, - { name: "command.description", value: params.command.description, weight: 7 }, - { name: "tool.description", value: params.toolDescription, weight: 5 }, - ...exampleFields({ examples: params.command.examples }), - ...walkSchema({ schema: params.command.inputSchema, path: [] }), - ], - }, - }; -} - -export const ToolFindMetadataSingleton = defineSingleton({ - params: {}, - deps: {}, - command: metadataCommand, -}); - -function oneLine(params: { value: string }): string { - return params.value - .replace(/\r\n/g, "\\n") - .replace(/\n/g, "\\n") - .replace(/\r/g, "\\r") - .replace(/\t/g, "\\t"); -} - -function renderPlainFind(params: { data: ToolFindData }): string { - if (params.data.results.length === 0) { - const scope = params.data.tool ? ` in tool ${params.data.tool}` : ""; - return `No Rig commands found for ${JSON.stringify(params.data.query)}${scope}.`; - } - return params.data.results - .map(function renderResult(result) { - return [ - `${result.rank}. ${result.id}`, - ` ${result.description}`, - ` ${oneLine({ value: result.runExample })}`, - ].join("\n"); - }) - .join("\n\n"); -} - -export const ToolFindPlainRendererSingleton = defineSingleton({ - params: {}, - deps: {}, - render: renderPlainFind, -}); - -function findLimit(params: { value: number | string | undefined }): number { - const parsed = params.value === undefined ? 5 : Number(params.value); - if (!Number.isInteger(parsed) || parsed < 1 || parsed > 50) { - throw new RigErrorClass("INPUT_ERROR", "Find limit must be an integer between 1 and 50.", { - value: params.value, - }); - } - return parsed; -} - -type ToolFindServiceDeps = { - discover: ToolDiscoveryServiceClass["discover"]; - loadMetadata: ToolMetadataCacheClass["load"]; -}; - -function createToolFindServiceDeps(options: ConfigOptions): ToolFindServiceDeps { - const discovery = new ToolDiscoveryServiceClass(options); - const metadataCache = new ToolMetadataCacheClass(options); - return { - discover: discovery.discover.bind(discovery), - loadMetadata: metadataCache.load.bind(metadataCache), - }; -} - -const ToolFindServiceProductionDeps = createToolFindServiceDeps({}); - -export class ToolFindService extends defineService({ - params: {}, - deps: ToolFindServiceProductionDeps, -}) { - public async find(params: { query: string; options?: ToolFindOptions }): Promise { - const query = params.query.trim(); - if (!query) throw new RigErrorClass("INPUT_ERROR", "Find query cannot be empty."); - const limit = findLimit({ value: params.options?.limit }); - const tool = params.options?.tool ? new ToolNameClass(params.options.tool).value : undefined; - const discovered = await this.deps.discover(); - const selected = tool - ? discovered.filter(function selectTool(entry) { - return entry.name === tool; - }) - : discovered; - if (tool && selected.length === 0) { - throw new RigErrorClass("TOOL_NOT_FOUND", `Tool not found: ${tool}`, { - available: discovered.map(function toolName(entry) { - return entry.name; - }), - }); - } - - const loadedMetadata = await this.deps.loadMetadata(selected, { prune: tool === undefined }); - const searchable = loadedMetadata.flatMap(function searchableTool(loaded) { - return loaded.commands.map(function searchableCommand(command) { - return ToolFindMetadataSingleton.command({ - toolName: loaded.name, - toolDescription: loaded.description, - commandName: command.name, - command, - }); - }); - }); - const byId = new Map( - searchable.map(function commandEntry(command) { - return [command.id, command]; - }), - ); - const ranked = toolSearchEngine.search({ - query, - documents: searchable.map(function searchDocument(command) { - return command.document; - }), - limit, - }); - - return { - query, - ...(tool ? { tool } : {}), - limit, - results: ranked.map(function rankedResult(result, index) { - const command = byId.get(result.id)!; - return { - rank: index + 1, - score: result.score, - id: command.id, - tool: command.tool, - command: command.command, - description: command.description, - runExample: command.runExample, - matches: result.matches, - }; - }), - }; - } - - public renderPlain(params: { data: ToolFindData }): string { - return ToolFindPlainRendererSingleton.render(params); - } -} - -export const ToolFind = new ToolFindService(); - -export type ToolFindServiceClass = { - find(params: { query: string; options?: ToolFindOptions }): Promise; - renderPlain(params: { data: ToolFindData }): string; -}; - -type ToolFindServiceConstructor = { - new (options?: ConfigOptions): ToolFindServiceClass; - readonly prototype: ToolFindServiceClass; -}; - -type ToolFindServiceAdapter = ToolFindServiceClass & { readonly resource: ToolFindService }; - -const ToolFindServiceClassAdapter = function constructToolFindService( - this: ToolFindServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolFindService({ params: {}, deps: createToolFindServiceDeps(options) }), - }); -}; -Object.defineProperty(ToolFindServiceClassAdapter, "name", { value: "ToolFindServiceClass" }); -Object.defineProperties(ToolFindServiceClassAdapter.prototype, { - find: { - configurable: true, - value: function find( - this: ToolFindServiceAdapter, - params: { query: string; options?: ToolFindOptions }, - ) { - return this.resource.find(params); - }, - writable: true, - }, - renderPlain: { - configurable: true, - value: function renderPlain(this: ToolFindServiceAdapter, params: { data: ToolFindData }) { - return this.resource.renderPlain(params); - }, - writable: true, - }, -}); - -export const ToolFindServiceClass = - ToolFindServiceClassAdapter as unknown as ToolFindServiceConstructor; diff --git a/src/tools/presentation/tool-help.ts b/src/tools/presentation/tool-help.ts deleted file mode 100644 index 6cff70d..0000000 --- a/src/tools/presentation/tool-help.ts +++ /dev/null @@ -1,323 +0,0 @@ -import type { ConfigOptions } from "../../config/config"; -import { defineService, defineSingleton } from "../../define"; -import { RigErrorClass } from "../../errors/RigError"; -import type { CollectionDefinition } from "../collection"; -import { commandTargets, ToolNameClass } from "../identifiers"; -import { ToolLoaderClass } from "../loader"; -import { schemaRenderer } from "../schema"; -import { commandIds, type CommandDefinition, type ToolDefinition } from "../types"; - -type ToolDefinitionWithCollections = { - collections?: Record; -}; - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function helpTypeName(params: { schema: unknown }): string { - if (!isRecord(params.schema)) return "unknown"; - const type = params.schema.type; - if (Array.isArray(type)) return type.join(" | "); - if (typeof type === "string") return type; - return "unknown"; -} - -function renderFields(params: { schema: unknown }): string { - const jsonSchema = schemaRenderer.toJsonSchema(params.schema); - if (!isRecord(jsonSchema) || !isRecord(jsonSchema.properties)) { - return "- value: unknown"; - } - - const required = Array.isArray(jsonSchema.required) ? jsonSchema.required : []; - const fields = Object.entries(jsonSchema.properties).map(function renderField([name, property]) { - const hasDefault = isRecord(property) && property.default !== undefined; - const requiredText = required.includes(name) && !hasDefault ? "required" : "optional"; - const defaultText = hasDefault ? `, default ${JSON.stringify(property.default)}` : ""; - return `- ${name}: ${helpTypeName({ schema: property })} (${requiredText}${defaultText})`; - }); - - return fields.join("\n"); -} - -function shellArg(params: { value: string }): string { - if (/^[A-Za-z0-9_./:=@+-]+$/.test(params.value)) return params.value; - return `'${params.value.replaceAll("'", "'\\''")}'`; -} - -function renderHelpExampleArgs(params: { input: unknown }): string { - if (!isRecord(params.input)) return shellArg({ value: String(params.input) }); - const entries = Object.entries(params.input); - if (entries.length === 1) return shellArg({ value: String(entries[0]?.[1]) }); - return entries - .map(function renderEntry([key, value]) { - return `${key}=${shellArg({ value: String(value) })}`; - }) - .join(" "); -} - -function renderExamples(params: { - toolName: string; - commandName: string; - command: CommandDefinition; -}): string { - const examples = params.command.examples ?? []; - if (examples.length === 0) return "No examples declared."; - - return examples - .map(function renderExample(example) { - const args = - example.input === undefined ? "" : ` ${renderHelpExampleArgs({ input: example.input })}`; - return [ - `$ rig run ${commandIds.from(params.toolName, params.commandName)}${args}`, - `# ${example.title}: ${example.text}`, - ].join("\n"); - }) - .join("\n\n"); -} - -function renderHelpCommand(params: { - toolName: string; - commandName: string; - command: CommandDefinition; - options?: { detailed?: boolean; headingLevel?: number }; -}): string { - const options = params.options ?? {}; - const id = commandIds.from(params.toolName, params.commandName); - const heading = "#".repeat(options.headingLevel ?? 3); - const lines = options.detailed - ? [ - `Tool: ${params.toolName}`, - `Command: ${params.commandName}`, - `Run: rig run ${id} [args...]`, - "", - ] - : [`${heading} ${id}`, "", `Run: rig run ${id} [args...]`, ""]; - - if (options.detailed) lines.push(`${heading} ${id}`, ""); - - lines.push( - params.command.description, - "", - "Input:", - "", - renderFields({ schema: params.command.input }), - "", - "Output:", - "", - renderFields({ schema: params.command.output }), - "", - "Examples:", - "", - renderExamples(params), - ); - - return lines.join("\n"); -} - -/* v8 ignore start */ -function renderCollection(params: { - name: string; - definition: CollectionDefinition | undefined; -}): string { - const lines = [`### ${params.name}`, "", `Directory: /${params.name}/`]; - lines.push(`Access: context.collections.${params.name}`); - if (params.definition?.schema) { - lines.push("", "Schema:", "", renderFields({ schema: params.definition.schema })); - } else { - lines.push("", "Schema: (none, any frontmatter allowed)"); - } - return lines.join("\n"); -} -/* v8 ignore stop */ - -function renderToolHelp(params: { definition: ToolDefinition; selectedCommand?: string }): string { - if (params.selectedCommand) { - const command = params.definition.commands[params.selectedCommand]; - if (!command) { - throw new RigErrorClass( - "COMMAND_NOT_FOUND", - `Command not found: ${commandIds.from(params.definition.name, params.selectedCommand)}`, - { available: Object.keys(params.definition.commands) }, - ); - } - return renderHelpCommand({ - toolName: params.definition.name, - commandName: params.selectedCommand, - command, - options: { detailed: true, headingLevel: 1 }, - }); - } - - const lines = [`# ${params.definition.name}`, "", params.definition.description, ""]; - - /* v8 ignore start */ - const collections = (params.definition as ToolDefinitionWithCollections).collections; - if (collections && Object.keys(collections).length > 0) { - lines.push("## Collections", ""); - for (const [name, definition] of Object.entries(collections)) { - lines.push(renderCollection({ name, definition }), ""); - } - } - /* v8 ignore stop */ - - lines.push("## Commands", ""); - for (const [commandName, command] of Object.entries(params.definition.commands)) { - lines.push( - renderHelpCommand({ - toolName: params.definition.name, - commandName, - command, - options: { headingLevel: 3 }, - }), - "", - ); - } - return lines.join("\n").trimEnd(); -} - -export const ToolHelpRendererSingleton = defineSingleton({ - params: {}, - deps: {}, - render: renderToolHelp, - renderCommand: renderHelpCommand, - renderExampleArgs: renderHelpExampleArgs, - typeName: helpTypeName, -}); - -export type ToolHelpRendererClass = { - render(definition: ToolDefinition, selectedCommand?: string): string; -}; - -type ToolHelpRendererConstructor = { - new (): ToolHelpRendererClass; - readonly prototype: ToolHelpRendererClass; -}; - -type ToolHelpRendererAdapter = ToolHelpRendererClass & { - renderCommand(toolName: string, commandName: string, command: CommandDefinition): string; - renderExampleArgs(input: unknown): string; - typeName(schema: unknown): string; -}; - -const ToolHelpRendererClassAdapter = function constructToolHelpRenderer(): void {}; -Object.defineProperty(ToolHelpRendererClassAdapter, "name", { value: "ToolHelpRendererClass" }); -Object.defineProperties(ToolHelpRendererClassAdapter.prototype, { - render: { - configurable: true, - value: function render( - this: ToolHelpRendererAdapter, - definition: ToolDefinition, - selectedCommand?: string, - ) { - return ToolHelpRendererSingleton.render({ definition, selectedCommand }); - }, - writable: true, - }, - renderCommand: { - configurable: true, - value: function renderLegacyCommand( - this: ToolHelpRendererAdapter, - toolName: string, - commandName: string, - command: CommandDefinition, - ) { - return ToolHelpRendererSingleton.renderCommand({ toolName, commandName, command }); - }, - writable: true, - }, - renderExampleArgs: { - configurable: true, - value: function renderLegacyExampleArgs(this: ToolHelpRendererAdapter, input: unknown) { - return ToolHelpRendererSingleton.renderExampleArgs({ input }); - }, - writable: true, - }, - typeName: { - configurable: true, - value: function legacyTypeName(this: ToolHelpRendererAdapter, schema: unknown) { - return ToolHelpRendererSingleton.typeName({ schema }); - }, - writable: true, - }, -}); - -export const ToolHelpRendererClass = - ToolHelpRendererClassAdapter as unknown as ToolHelpRendererConstructor; - -function helpTarget(params: { toolName: string; commandName?: string }): { - toolName: string; - commandName?: string; -} { - if (params.commandName) { - const target = commandTargets.from(params.toolName, params.commandName); - return { toolName: target.tool, commandName: target.command }; - } - if (!params.toolName.includes(".")) { - return { toolName: new ToolNameClass(params.toolName).value }; - } - const target = commandTargets.parse(params.toolName); - return { toolName: target.tool, commandName: target.command }; -} - -type ToolHelpServiceDeps = { - loadDefinition: ToolLoaderClass["loadDefinition"]; - render: typeof ToolHelpRendererSingleton.render; -}; - -function createToolHelpServiceDeps(options: ConfigOptions): ToolHelpServiceDeps { - const loader = new ToolLoaderClass(options); - return { - loadDefinition: loader.loadDefinition.bind(loader), - render: ToolHelpRendererSingleton.render, - }; -} - -const ToolHelpServiceProductionDeps = createToolHelpServiceDeps({}); - -export class ToolHelpService extends defineService({ - params: {}, - deps: ToolHelpServiceProductionDeps, -}) { - public async render(params: { toolName: string; commandName?: string }): Promise { - const target = helpTarget(params); - const tool = await this.deps.loadDefinition(target.toolName); - return this.deps.render({ - definition: tool.definition, - selectedCommand: target.commandName, - }); - } -} - -export const ToolHelp = new ToolHelpService(); - -export type ToolHelpServiceClass = { - render(toolName: string, commandName?: string): Promise; -}; - -type ToolHelpServiceConstructor = { - new (options?: ConfigOptions): ToolHelpServiceClass; - readonly prototype: ToolHelpServiceClass; -}; - -type ToolHelpServiceAdapter = ToolHelpServiceClass & { readonly resource: ToolHelpService }; - -const ToolHelpServiceClassAdapter = function constructToolHelpService( - this: ToolHelpServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolHelpService({ params: {}, deps: createToolHelpServiceDeps(options) }), - }); -}; -Object.defineProperty(ToolHelpServiceClassAdapter, "name", { value: "ToolHelpServiceClass" }); -Object.defineProperty(ToolHelpServiceClassAdapter.prototype, "render", { - configurable: true, - value: function render(this: ToolHelpServiceAdapter, toolName: string, commandName?: string) { - return this.resource.render({ toolName, commandName }); - }, - writable: true, -}); - -export const ToolHelpServiceClass = - ToolHelpServiceClassAdapter as unknown as ToolHelpServiceConstructor; diff --git a/src/tools/presentation/tool-inspector.ts b/src/tools/presentation/tool-inspector.ts deleted file mode 100644 index 311900b..0000000 --- a/src/tools/presentation/tool-inspector.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { defineService } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import { ToolLoaderClass } from "../loader"; -import { SchemaRenderer } from "../schema"; -import { CommandIdsSingleton, type CommandDefinition } from "../types"; -import { CommandTargetSingleton, ToolNameSingleton } from "../identifiers"; - -type ToolInspectorLoader = Pick; - -const ToolInspectorServiceDeps: { - createLoader: (params: { options: ConfigOptions }) => ToolInspectorLoader; - commandIds: typeof CommandIdsSingleton; - commandTargets: typeof CommandTargetSingleton; - toolNames: typeof ToolNameSingleton; - schemaRenderer: typeof SchemaRenderer; -} = { - createLoader: function createLoader(params: { options: ConfigOptions }) { - return new ToolLoaderClass(params.options); - }, - commandIds: CommandIdsSingleton, - commandTargets: CommandTargetSingleton, - toolNames: ToolNameSingleton, - schemaRenderer: SchemaRenderer, -}; - -export class ToolInspectorService extends defineService({ - params: {} as ConfigOptions, - deps: ToolInspectorServiceDeps, -}) { - private get loader() { - return this.deps.createLoader({ options: this.params }); - } - - private inspectTarget(params: { toolName: string; commandName?: string }): { - toolName: string; - commandName?: string; - } { - if (params.commandName) { - const target = this.deps.commandTargets.from({ - tool: params.toolName, - command: params.commandName, - }); - return { toolName: target.tool, commandName: target.command }; - } - if (!params.toolName.includes(".")) { - return { toolName: this.deps.toolNames.create({ value: params.toolName }).value }; - } - const target = this.deps.commandTargets.parse({ id: params.toolName }); - return { toolName: target.tool, commandName: target.command }; - } - - private commandMetadata(params: { toolName: string; name: string; command: CommandDefinition }) { - return { - name: params.name, - id: this.deps.commandIds.from({ tool: params.toolName, command: params.name }), - description: params.command.description, - inputSchema: this.deps.schemaRenderer.toJsonSchema({ schema: params.command.input }), - outputSchema: this.deps.schemaRenderer.toJsonSchema({ schema: params.command.output }), - run: `rig run ${this.deps.commandIds.from({ tool: params.toolName, command: params.name })} [args...]`, - examples: params.command.examples ?? [], - }; - } - - public async inspect(params: { toolName: string; commandName?: string }) { - const target = this.inspectTarget(params); - const loaded = await this.loader.loadDefinition(target.toolName); - const definition = loaded.definition; - - if (target.commandName) { - const command = definition.commands[target.commandName]; - if (!command) { - throw new RigErrorClass( - "COMMAND_NOT_FOUND", - `Command not found: ${this.deps.commandIds.from({ tool: target.toolName, command: target.commandName })}`, - { - tool: target.toolName, - command: target.commandName, - available: Object.keys(definition.commands), - }, - ); - } - return { - tool: definition.name, - command: target.commandName, - path: loaded.path, - ...this.commandMetadata({ - toolName: definition.name, - name: target.commandName, - command, - }), - }; - } - - return { - name: definition.name, - description: definition.description, - path: loaded.path, - commands: Object.entries(definition.commands).map(([name, command]) => - this.commandMetadata({ toolName: definition.name, name, command }), - ), - }; - } -} - -export const ToolInspector = new ToolInspectorService(); - -export type ToolInspectorClass = { - inspect(toolName: string, commandName?: string): ReturnType; -}; - -type ToolInspectorConstructor = { - new (options?: ConfigOptions): ToolInspectorClass; - readonly prototype: ToolInspectorClass; -}; - -type ToolInspectorAdapter = ToolInspectorClass & { readonly resource: ToolInspectorService }; - -const ToolInspectorClassAdapter = function constructToolInspector( - this: ToolInspectorAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolInspectorService({ params: options, deps: ToolInspectorServiceDeps }), - }); -}; -Object.defineProperty(ToolInspectorClassAdapter, "name", { value: "ToolInspectorClass" }); -Object.defineProperty(ToolInspectorClassAdapter.prototype, "inspect", { - configurable: true, - value: function inspect(this: ToolInspectorAdapter, toolName: string, commandName?: string) { - return this.resource.inspect({ toolName, commandName }); - }, - writable: true, -}); - -export const ToolInspectorClass = ToolInspectorClassAdapter as unknown as ToolInspectorConstructor; diff --git a/src/tools/presentation/tool-list.ts b/src/tools/presentation/tool-list.ts deleted file mode 100644 index 7e6bec8..0000000 --- a/src/tools/presentation/tool-list.ts +++ /dev/null @@ -1,516 +0,0 @@ -import { readFile, stat } from "node:fs/promises"; -import { defineService, defineSingleton } from "../../define"; -import { AtomicFileWriterClass } from "../../config/file-lock"; -import type { ConfigOptions } from "../../config/config"; -import { RigPathsClass } from "../../config/paths"; -import { ToolDiscoveryServiceClass, type DiscoveredTool } from "../../registry/discover"; -import type { CollectionDefinition } from "../collection"; -import { CurrentRigToolApiVersion } from "../domain/tool-api"; -import { ToolLoaderClass, type LoadedToolDefinition } from "../loader"; -import { schemaRenderer } from "../schema"; -import { commandIds, type CommandDefinition, type ToolExample } from "../types"; - -export type ToolListOptions = { visibleFromPath?: string }; - -export type ListedCommand = { - name: string; - id: string; - description: string; - runExample: string; - helpExample: string; -}; - -export type ListedCollection = { name: string; hasSchema: boolean }; - -export type ListedTool = { - name: string; - description: string; - registryKind: string; - registryPath: string; - toolPath: string; - commands: ListedCommand[]; - collections: ListedCollection[]; -}; - -export type ToolListData = { tools: ListedTool[]; visibleFromPath?: string }; - -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function shellArg(params: { value: string }): string { - if (/^[A-Za-z0-9_./:=@%+,-]+$/.test(params.value)) return params.value; - return `'${params.value.replaceAll("'", "'\\''")}'`; -} - -function placeholder(params: { schema: unknown }): string { - /* v8 ignore next */ - if (!isRecord(params.schema)) return "VALUE"; - /* v8 ignore next */ - const type = Array.isArray(params.schema.type) ? params.schema.type[0] : params.schema.type; - if (type === "number" || type === "integer") return "0"; - if (type === "boolean") return "true"; - if (type === "array") return "[]"; - if (type === "object") return "{}"; - return "VALUE"; -} - -function renderRequiredInput(params: { schema: unknown }): string { - const jsonSchema = schemaRenderer.toJsonSchema(params.schema); - if (!isRecord(jsonSchema) || !isRecord(jsonSchema.properties)) return ""; - - const required = Array.isArray(jsonSchema.required) ? jsonSchema.required : []; - const fields = Object.entries(jsonSchema.properties) - .filter(function requiredWithoutDefault([name, property]) { - return required.includes(name) && !(isRecord(property) && property.default !== undefined); - }) - .map(function inputField([name, property]) { - return [name, placeholder({ schema: property })] as const; - }); - - if (fields.length === 0) return ""; - if (fields.length <= 3) { - return fields - .map(function renderField([name, value]) { - return `${name}=${shellArg({ value })}`; - }) - .join(" "); - } - - return `--input ${shellArg({ value: JSON.stringify(Object.fromEntries(fields)) })}`; -} - -function inputScalar(params: { value: unknown }): string { - if (typeof params.value === "string") return params.value; - /* v8 ignore next */ - return JSON.stringify(params.value) ?? String(params.value); -} - -function renderInputValue(params: { input: unknown }): string { - if (!isRecord(params.input)) return shellArg({ value: String(params.input) }); - return Object.entries(params.input) - .map(function renderEntry([key, value]) { - return `${key}=${shellArg({ value: inputScalar({ value }) })}`; - }) - .join(" "); -} - -function renderCommandRunExample(params: { - toolName: string; - commandName: string; - command: CommandDefinition; -}): string { - const id = commandIds.from(params.toolName, params.commandName); - const exampleInput = params.command.examples?.find(function withInput(example) { - return example.input !== undefined; - })?.input; - if (exampleInput !== undefined) { - return `rig run ${id} ${renderInputValue({ input: exampleInput })}`.trimEnd(); - } - const args = renderRequiredInput({ schema: params.command.input }); - return `rig run ${id}${args ? ` ${args}` : ""}`; -} - -export const CommandRunExampleRendererSingleton = defineSingleton({ - params: {}, - deps: {}, - render: renderCommandRunExample, -}); - -export type CommandRunExampleRendererClass = { - render(toolName: string, commandName: string, command: CommandDefinition): string; -}; - -type CommandRunExampleRendererConstructor = { - new (): CommandRunExampleRendererClass; - readonly prototype: CommandRunExampleRendererClass; -}; - -const CommandRunExampleRendererClassAdapter = - function constructCommandRunExampleRenderer(): void {}; -Object.defineProperty(CommandRunExampleRendererClassAdapter, "name", { - value: "CommandRunExampleRendererClass", -}); -Object.defineProperty(CommandRunExampleRendererClassAdapter.prototype, "render", { - configurable: true, - value: function renderLegacyExample( - toolName: string, - commandName: string, - command: CommandDefinition, - ) { - return CommandRunExampleRendererSingleton.render({ toolName, commandName, command }); - }, - writable: true, -}); - -export const CommandRunExampleRendererClass = - CommandRunExampleRendererClassAdapter as unknown as CommandRunExampleRendererConstructor; - -export type ToolCommandMetadata = { - name: string; - description: string; - inputSchema: unknown; - outputSchema: unknown; - examples: ToolExample[]; - runExample: string; -}; - -export type ToolMetadata = { - name: string; - description: string; - commands: ToolCommandMetadata[]; - collections: ListedCollection[]; -}; - -type ToolMetadataCacheEntry = { - modifiedAtMs: number; - changedAtMs: number; - size: number; - metadata: ToolMetadata; -}; - -type ToolMetadataCache = { - version: 1; - toolApiVersion: number; - entries: Record; -}; - -function emptyMetadataCache(_params: {}): ToolMetadataCache { - return { version: 1, toolApiVersion: CurrentRigToolApiVersion, entries: {} }; -} - -function isMetadata(params: { value: unknown }): params is { value: ToolMetadata } { - return ( - isRecord(params.value) && - typeof params.value.name === "string" && - typeof params.value.description === "string" && - Array.isArray(params.value.commands) && - Array.isArray(params.value.collections) - ); -} - -function listedCollections(params: { - definition: Record & { - collections?: Record; - }; -}): ListedCollection[] { - const collections = params.definition.collections; - if (!collections) return []; - return Object.entries(collections).map(function listedCollection([name, value]) { - return { name, hasSchema: Boolean(value?.schema) }; - }); -} - -function toolMetadata(params: { loaded: LoadedToolDefinition }): ToolMetadata { - return { - name: params.loaded.definition.name, - description: params.loaded.definition.description, - commands: Object.entries(params.loaded.definition.commands).map(function metadataCommand([ - name, - command, - ]) { - return { - name, - description: command.description, - inputSchema: schemaRenderer.toJsonSchema(command.input), - outputSchema: schemaRenderer.toJsonSchema(command.output), - examples: command.examples ?? [], - runExample: CommandRunExampleRendererSingleton.render({ - toolName: params.loaded.definition.name, - commandName: name, - command, - }), - }; - }), - collections: listedCollections({ definition: params.loaded.definition }), - }; -} - -type ToolMetadataCacheDeps = { - cachePath: string; - loadDefinition: ToolLoaderClass["loadDefinitionDiscovered"]; - readJson: (path: string) => Promise; - write: AtomicFileWriterClass["write"]; - stat: typeof stat; -}; - -function createToolMetadataCacheDeps(options: ConfigOptions): ToolMetadataCacheDeps { - const paths = new RigPathsClass(options); - const loader = new ToolLoaderClass(options); - const writer = new AtomicFileWriterClass(); - return { - cachePath: paths.toolMetadataCachePath, - loadDefinition: loader.loadDefinitionDiscovered.bind(loader), - async readJson(path) { - /* v8 ignore next 3 */ - return typeof Bun !== "undefined" - ? await Bun.file(path).json() - : JSON.parse(await readFile(path, "utf8")); - }, - write: writer.write.bind(writer), - stat, - }; -} - -const ToolMetadataCacheProductionDeps = createToolMetadataCacheDeps({}); - -export class ToolMetadataCacheService extends defineService({ - params: {}, - deps: ToolMetadataCacheProductionDeps, -}) { - private async read(_params: {}): Promise { - try { - const value = await this.deps.readJson(this.deps.cachePath); - if (!isRecord(value) || value.version !== 1) return emptyMetadataCache({}); - if (value.toolApiVersion !== CurrentRigToolApiVersion || !isRecord(value.entries)) { - return emptyMetadataCache({}); - } - return value as ToolMetadataCache; - } catch { - return emptyMetadataCache({}); - } - } - - private async write(params: { cache: ToolMetadataCache }): Promise { - try { - await this.deps.write(this.deps.cachePath, `${JSON.stringify(params.cache, null, 2)}\n`); - } catch { - // Metadata caching should never block discovery commands. - } - } - - public async load(params: { - entries: DiscoveredTool[]; - options: { prune?: boolean }; - }): Promise { - const options = params.options; - const current = await this.read({}); - const nextEntries: Record = - options.prune === false ? { ...current.entries } : {}; - let changed = false; - const metadata = await Promise.all( - params.entries.map(async (entry) => { - const status = await this.deps.stat(entry.toolPath); - const cached = current.entries[entry.toolPath]; - const candidate = { value: cached?.metadata }; - if ( - cached?.modifiedAtMs === status.mtimeMs && - cached.changedAtMs === status.ctimeMs && - cached.size === status.size && - isMetadata(candidate) - ) { - nextEntries[entry.toolPath] = cached; - return candidate.value; - } - - changed = true; - const loaded = await this.deps.loadDefinition(entry); - const value = toolMetadata({ loaded }); - nextEntries[entry.toolPath] = { - modifiedAtMs: status.mtimeMs, - changedAtMs: status.ctimeMs, - size: status.size, - metadata: value, - }; - return value; - }), - ); - if ( - options.prune !== false && - Object.keys(current.entries).some(function missingEntry(path) { - return nextEntries[path] === undefined; - }) - ) { - changed = true; - } - if (changed) { - await this.write({ - cache: { - version: 1, - toolApiVersion: CurrentRigToolApiVersion, - entries: nextEntries, - }, - }); - } - return metadata; - } -} - -export const ToolMetadataCache = new ToolMetadataCacheService(); - -export type ToolMetadataCacheClass = { - load(entries: DiscoveredTool[], options?: { prune?: boolean }): Promise; -}; - -type ToolMetadataCacheConstructor = { - new (options?: ConfigOptions): ToolMetadataCacheClass; - readonly prototype: ToolMetadataCacheClass; -}; - -type ToolMetadataCacheAdapter = ToolMetadataCacheClass & { - readonly resource: ToolMetadataCacheService; -}; - -const ToolMetadataCacheClassAdapter = function constructToolMetadataCache( - this: ToolMetadataCacheAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolMetadataCacheService({ - params: {}, - deps: createToolMetadataCacheDeps(options), - }), - }); -}; -Object.defineProperty(ToolMetadataCacheClassAdapter, "name", { value: "ToolMetadataCacheClass" }); -Object.defineProperty(ToolMetadataCacheClassAdapter.prototype, "load", { - configurable: true, - value: function load( - this: ToolMetadataCacheAdapter, - entries: DiscoveredTool[], - options: { prune?: boolean } = {}, - ) { - return this.resource.load({ entries, options }); - }, - writable: true, -}); - -export const ToolMetadataCacheClass = - ToolMetadataCacheClassAdapter as unknown as ToolMetadataCacheConstructor; - -function plainDescription(params: { value: string }): string { - return params.value.replace(/\s+/g, " ").trim(); -} - -function plainExample(params: { value: string }): string { - return params.value - .replace(/\r\n/g, "\\n") - .replace(/\n/g, "\\n") - .replace(/\r/g, "\\r") - .replace(/\t/g, "\\t") - .trim(); -} - -/* v8 ignore start */ -function renderToolHeader(params: { tool: ListedTool }): string { - const collections = - params.tool.collections.length > 0 - ? ` [collections: ${params.tool.collections.map((collection) => collection.name).join(", ")}]` - : ""; - return `${params.tool.name} # ${plainDescription({ value: params.tool.description })}${collections}`; -} -/* v8 ignore stop */ - -function renderPlainList(params: { data: ToolListData }): string { - if (params.data.tools.length === 0) return "No Rig tools found."; - return params.data.tools - .map(function renderTool(tool) { - return [ - renderToolHeader({ tool }), - ...tool.commands.map(function renderCommand(command) { - return ` ${plainExample({ value: command.runExample })} # ${plainDescription({ value: command.description })}`; - }), - ].join("\n"); - }) - .join("\n\n"); -} - -export const ToolListPlainRendererSingleton = defineSingleton({ - params: {}, - deps: {}, - render: renderPlainList, -}); - -type ToolListServiceDeps = { - discover: ToolDiscoveryServiceClass["discover"]; - loadMetadata: ToolMetadataCacheClass["load"]; -}; - -function createToolListServiceDeps(options: ConfigOptions): ToolListServiceDeps { - const discovery = new ToolDiscoveryServiceClass(options); - const metadataCache = new ToolMetadataCacheClass(options); - return { - discover: discovery.discover.bind(discovery), - loadMetadata: metadataCache.load.bind(metadataCache), - }; -} - -const ToolListServiceProductionDeps = createToolListServiceDeps({}); - -export class ToolListService extends defineService({ - params: {}, - deps: ToolListServiceProductionDeps, -}) { - public async list(options: ToolListOptions = {}): Promise { - const discovered = await this.deps.discover({ visibleFromPath: options.visibleFromPath }); - const metadata = await this.deps.loadMetadata(discovered); - const tools = discovered.map(function listedTool(entry, index) { - const tool = metadata[index]!; - return { - name: tool.name, - description: tool.description, - registryKind: entry.registryKind, - registryPath: entry.registryPath, - toolPath: entry.toolPath, - commands: tool.commands.map(function listedCommand(command) { - return { - name: command.name, - id: commandIds.from(tool.name, command.name), - description: command.description, - runExample: command.runExample, - helpExample: `rig help ${commandIds.from(tool.name, command.name)}`, - }; - }), - collections: tool.collections, - }; - }); - return { tools, visibleFromPath: options.visibleFromPath }; - } - - public renderPlain(params: { data: ToolListData }): string { - return ToolListPlainRendererSingleton.render(params); - } -} - -export const ToolList = new ToolListService(); - -export type ToolListServiceClass = { - list(options?: ToolListOptions): Promise; - renderPlain(data: ToolListData): string; -}; - -type ToolListServiceConstructor = { - new (options?: ConfigOptions): ToolListServiceClass; - readonly prototype: ToolListServiceClass; -}; - -type ToolListServiceAdapter = ToolListServiceClass & { readonly resource: ToolListService }; - -const ToolListServiceClassAdapter = function constructToolListService( - this: ToolListServiceAdapter, - options: ConfigOptions = {}, -): void { - Object.defineProperty(this, "resource", { - value: new ToolListService({ params: {}, deps: createToolListServiceDeps(options) }), - }); -}; -Object.defineProperty(ToolListServiceClassAdapter, "name", { value: "ToolListServiceClass" }); -Object.defineProperties(ToolListServiceClassAdapter.prototype, { - list: { - configurable: true, - value: function list(this: ToolListServiceAdapter, options: ToolListOptions = {}) { - return this.resource.list(options); - }, - writable: true, - }, - renderPlain: { - configurable: true, - value: function renderPlain(this: ToolListServiceAdapter, data: ToolListData) { - return this.resource.renderPlain({ data }); - }, - writable: true, - }, -}); - -export const ToolListServiceClass = - ToolListServiceClassAdapter as unknown as ToolListServiceConstructor; diff --git a/src/tools/run.test.ts b/src/tools/run.test.ts deleted file mode 100644 index 05e72b9..0000000 --- a/src/tools/run.test.ts +++ /dev/null @@ -1,1545 +0,0 @@ -import { afterEach, describe, expect, test } from "vite-plus/test"; -import { existsSync, writeFileSync } from "node:fs"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import { ToolCreatorClass } from "./create"; -import { ToolCacheServiceClass } from "./cache"; -import { ToolDatabaseServiceClass } from "./db"; -import { ToolHelpServiceClass } from "./help"; -import { ToolInspectorClass } from "./inspect"; -import { ToolKvStoreServiceClass } from "./kv"; -import { ToolListServiceClass } from "./list"; -import { RunInputReaderClass, ToolRunnerClass } from "./run"; -import type { CommandDefinition } from "./types"; -import { ToolTypecheckServiceClass } from "./typecheck"; - -class TestHomeStore { - private readonly homes: string[] = []; - - async create(): Promise { - const home = await mkdtemp(join(tmpdir(), "rig-test-home-")); - this.homes.push(home); - return home; - } - - async cleanup(): Promise { - new FakeSqliteEnvironment().uninstall(); - await Promise.all( - this.homes.splice(0).map((home) => rm(home, { recursive: true, force: true })), - ); - } -} - -class FakeSqliteStore { - readonly kv = new Map(); - readonly cache = new Map< - string, - { - keyJson: string; - valueJson: string; - dataUpdatedAt: number; - invalidatedAt: number | null; - lastAccessedAt: number; - } - >(); - readonly migrations = new Map(); - readonly notes: string[] = []; - setupRuns = 0; - closeRuns = 0; -} - -class FakeSqliteStatement { - constructor( - private readonly store: FakeSqliteStore, - private readonly sql: string, - ) {} - - get(params?: unknown): unknown { - if (this.sql.includes("from _rig_migrations")) { - return this.store.migrations.get(Number(params)) ?? null; - } - if (this.sql.includes("from _rig_kv")) { - const valueJson = this.store.kv.get(String(params)); - return valueJson === undefined ? null : { value_json: valueJson }; - } - if (this.sql.includes("from _rig_cache")) { - const row = this.store.cache.get(String(params)); - if (!row) return null; - return { - key_json: row.keyJson, - value_json: row.valueJson, - data_updated_at: row.dataUpdatedAt, - invalidated_at: row.invalidatedAt, - }; - } - if (this.sql.includes("count(*) as count from setup_runs")) { - return { count: this.store.setupRuns }; - } - return null; - } - - run(params?: unknown): { lastInsertRowid: number; changes: number } { - const record = this.recordParams(params); - if (this.sql.includes("into _rig_migrations")) { - this.store.migrations.set(Number(record.version), { - name: String(record.name), - checksum: String(record.checksum), - }); - return { lastInsertRowid: Number(record.version), changes: 1 }; - } - if (this.sql.includes("into setup_runs")) { - this.store.setupRuns++; - return { lastInsertRowid: this.store.setupRuns, changes: 1 }; - } - if (this.sql.includes("into _rig_kv")) { - this.store.kv.set(String(record.key), String(record.valueJson)); - return { lastInsertRowid: this.store.kv.size, changes: 1 }; - } - if (this.sql.includes("into _rig_cache")) { - this.store.cache.set(String(record.keyHash), { - keyJson: String(record.keyJson), - valueJson: String(record.valueJson), - dataUpdatedAt: Number(record.dataUpdatedAt), - invalidatedAt: null, - lastAccessedAt: Number(record.lastAccessedAt), - }); - return { lastInsertRowid: this.store.cache.size, changes: 1 }; - } - if (this.sql.includes("set invalidated_at")) { - const row = this.store.cache.get(String(record.keyHash)); - if (row) row.invalidatedAt = Number(record.invalidatedAt); - return { lastInsertRowid: 0, changes: row ? 1 : 0 }; - } - if (this.sql.includes("set last_accessed_at")) { - const row = this.store.cache.get(String(record.keyHash)); - if (row) row.lastAccessedAt = Number(record.lastAccessedAt); - return { lastInsertRowid: 0, changes: row ? 1 : 0 }; - } - if (this.sql.includes("delete from _rig_cache where")) { - const deleted = this.store.cache.delete(String(params)); - return { lastInsertRowid: 0, changes: deleted ? 1 : 0 }; - } - if (this.sql.includes("into notes")) { - this.store.notes.push(String(record.text)); - return { lastInsertRowid: this.store.notes.length, changes: 1 }; - } - return { lastInsertRowid: 0, changes: 0 }; - } - - private recordParams(params: unknown): Record { - return typeof params === "object" && params !== null && !Array.isArray(params) - ? (params as Record) - : {}; - } -} - -class FakeSqliteDatabase { - private static readonly stores = new Map(); - private static failNextRunPattern?: string; - private readonly store: FakeSqliteStore; - - constructor(filename: string) { - if (!existsSync(filename)) writeFileSync(filename, ""); - const existing = FakeSqliteDatabase.stores.get(filename); - this.store = existing ?? new FakeSqliteStore(); - FakeSqliteDatabase.stores.set(filename, this.store); - } - - static reset(): void { - this.stores.clear(); - this.failNextRunPattern = undefined; - } - - static failNextRunContaining(pattern: string): void { - this.failNextRunPattern = pattern; - } - - static store(filename: string): FakeSqliteStore | undefined { - return this.stores.get(filename); - } - - query(sql: string): FakeSqliteStatement { - return new FakeSqliteStatement(this.store, sql); - } - - run(sql: string): { lastInsertRowid: number; changes: number } { - if ( - FakeSqliteDatabase.failNextRunPattern && - sql.includes(FakeSqliteDatabase.failNextRunPattern) - ) { - FakeSqliteDatabase.failNextRunPattern = undefined; - throw new Error("sqlite initialization failed"); - } - if (sql.includes("delete from _rig_cache")) { - const changes = this.store.cache.size; - this.store.cache.clear(); - return { lastInsertRowid: 0, changes }; - } - return { lastInsertRowid: 0, changes: 0 }; - } - - transaction(callback: () => T): () => T { - return () => callback(); - } - - close(): void { - this.store.closeRuns++; - } -} - -class FakeSqliteEnvironment { - install(): void { - ( - globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: unknown } - ).rigSqliteDatabaseForTests = FakeSqliteDatabase; - } - - uninstall(): void { - delete (globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: unknown }) - .rigSqliteDatabaseForTests; - FakeSqliteDatabase.reset(); - } - - failNextInitialization(): void { - FakeSqliteDatabase.failNextRunContaining("PRAGMA journal_mode = WAL"); - } -} - -const homes = new TestHomeStore(); - -afterEach(async () => { - await homes.cleanup(); -}); - -class DbSetupTestToolWriter { - constructor(private readonly home: string) {} - - async write(name: string, setup: string): Promise { - const toolDir = join(this.home, "rig", "tools", name); - await mkdir(toolDir, { recursive: true }); - const toolPath = join(toolDir, "index.rig.ts"); - await writeFile(toolPath, this.source(name, setup), "utf8"); - return toolPath; - } - - private source(name: string, setup: string): string { - return `export default (rig) => rig.defineTool({ - name: ${JSON.stringify(name)}, - description: "DB error test tool.", - setupDb: (db) => { ${setup} }, - commands: { - check: rig.defineCommand({ - description: "Check DB setup.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`; - } -} - -describe("tool commands", () => { - test("normalizes non-Error JSON parser failures", async () => { - const parse = JSON.parse; - JSON.parse = () => { - throw "parser failed"; - }; - - try { - await expect( - new RunInputReaderClass().read({} as CommandDefinition, { input: "{" }), - ).rejects.toMatchObject({ - code: "INPUT_ERROR", - details: { message: "parser failed" }, - }); - } finally { - JSON.parse = parse; - } - }); - - test("creates a starter tool with definition-owned examples", async () => { - const home = await homes.create(); - const result = await new ToolCreatorClass({ homeDir: home }).create("sample"); - expect(result.files).toHaveLength(1); - expect(result.toolPath).toBe(join(home, "rig", "tools", "sample", "index.rig.ts")); - expect(result.id).toBe("sample.example"); - - const help = await new ToolHelpServiceClass({ homeDir: home }).render("sample", "example"); - const commandIdHelp = await new ToolHelpServiceClass({ homeDir: home }).render( - "sample.example", - ); - expect(commandIdHelp).toMatch( - /^Tool: sample\nCommand: example\nRun: rig run sample\.example \[args\.\.\.\]/, - ); - expect(commandIdHelp).not.toContain("```bash"); - expect(help).toContain("Run the example command"); - expect(help).toContain("rig run sample.example"); - expect(help).toContain("Input:"); - expect(help).toContain("Output:"); - }); - - test("inspects command metadata as JSON", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const inspected = await new ToolInspectorClass({ homeDir: home }).inspect("sample", "example"); - expect(inspected).toMatchObject({ - tool: "sample", - command: "example", - id: "sample.example", - run: "rig run sample.example [args...]", - }); - }); - - test("type-checks generated tools with injected Rig runtime types", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const result = await new ToolTypecheckServiceClass({ homeDir: home }).typecheck("sample"); - - expect(result.ok).toBe(true); - expect(result.exitCode).toBe(0); - expect(result.checked).toHaveLength(1); - }); - - test("type-checks command output against output schemas", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "bad-output"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `const tool: RigToolFactory = (rig) => rig.defineTool({ - name: "bad-output", - description: "Bad output test tool.", - commands: { - example: rig.defineCommand({ - description: "Return the wrong output type.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ text: rig.z.string() }), - run: async ({ input }) => ({ text: 123 }), - }), - }, -}); - -export default tool; -`, - "utf8", - ); - - const result = await new ToolTypecheckServiceClass({ homeDir: home }).typecheck("bad-output"); - - expect(result.ok).toBe(false); - expect(result.stdout).toContain("Type 'number' is not assignable to type 'string'"); - }); - - test("runs a command and returns a success envelope", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const result = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - args: ["Agent"], - }); - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { text: "Agent" }, - errors: [], - }); - const toolDir = join(home, "rig", "tools", "sample"); - for (const file of [ - "kv.sqlite", - "kv.sqlite-wal", - "kv.sqlite-shm", - "cache.sqlite", - "cache.sqlite-wal", - "cache.sqlite-shm", - ]) { - expect(existsSync(join(toolDir, file))).toBe(false); - } - }); - - test("resolves pipeline references in command input", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - - const result = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - args: ["text=@clean.output"], - pipeContext: { clean: { output: "image.cleaned.png" } }, - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { text: "image.cleaned.png" }, - errors: [], - }); - - const exactId = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - args: ["text=@clean"], - pipeContext: { clean: "image.cleaned.png" }, - }); - expect(exactId.envelope).toMatchObject({ - data: { text: "image.cleaned.png" }, - errors: [], - }); - - const embedded = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - args: ["text=output:@clean.output"], - pipeContext: { clean: { output: "image.cleaned.png" } }, - }); - expect(embedded.envelope).toMatchObject({ - data: { text: "output:image.cleaned.png" }, - errors: [], - }); - - const arrayInput = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - input: JSON.stringify({ text: "@names.0", ignored: ["@names.0"] }), - pipeContext: { names: ["image.cleaned.png"] }, - }); - expect(arrayInput.envelope).toMatchObject({ - data: { text: "image.cleaned.png" }, - errors: [], - }); - }); - - test("returns pipeline reference errors as envelopes", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const runner = new ToolRunnerClass({ homeDir: home }); - - const unknown = await runner.run("sample", "example", { - homeDir: home, - args: ["text=@missing.output"], - pipeContext: {}, - }); - expect(unknown.envelope).toMatchObject({ - errors: [{ code: "INPUT_ERROR", message: "Pipeline reference is unknown: @missing" }], - }); - - const cannotAccess = await runner.run("sample", "example", { - homeDir: home, - args: ["text=@clean.output.path"], - pipeContext: { clean: { output: "image.cleaned.png" } }, - }); - expect(cannotAccess.envelope).toMatchObject({ - errors: [ - { - code: "INPUT_ERROR", - message: "Pipeline reference cannot access: @clean.output.path", - }, - ], - }); - - const missing = await runner.run("sample", "example", { - homeDir: home, - args: ["text=@clean.missing"], - pipeContext: { clean: { output: "image.cleaned.png" } }, - }); - expect(missing.envelope).toMatchObject({ - errors: [{ code: "INPUT_ERROR", message: "Pipeline reference is missing: @clean.missing" }], - }); - }); - - test("runs registered tools from a tool context", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const toolDir = join(home, "rig", "tools", "caller"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "caller", - description: "Tool runner test tool.", - commands: { - call: rig.defineCommand({ - description: "Call another Rig tool.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ text: rig.z.string() }), - run: async (context) => { - const result = await context.rig.run({ - command: "sample.example", - input: { text: context.input.text }, - }); - return { text: result.text }; - }, - }), - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("caller", "call", { - homeDir: home, - input: '{"text":"Nested"}', - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { text: "Nested" }, - errors: [], - }); - }); - - test("reuses loaded definitions across nested calls in one execution session", async () => { - const home = await homes.create(); - const toolsDir = join(home, "rig", "tools"); - const calleeDir = join(toolsDir, "callee"); - const callerDir = join(toolsDir, "caller"); - await mkdir(calleeDir, { recursive: true }); - await mkdir(callerDir, { recursive: true }); - await writeFile( - join(calleeDir, "index.rig.ts"), - `export default (rig) => { - globalThis.__rigNestedDefinitionLoads = (globalThis.__rigNestedDefinitionLoads ?? 0) + 1; - const definitionLoad = globalThis.__rigNestedDefinitionLoads; - return rig.defineTool({ - name: "callee", - description: "Nested cache test callee.", - commands: { - read: rig.defineCommand({ - description: "Return the definition load count.", - input: rig.z.object({}), - output: rig.z.number(), - run: () => definitionLoad, - }), - }, - }); -}; -`, - "utf8", - ); - await writeFile( - join(callerDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "caller", - description: "Nested cache test caller.", - commands: { - call: rig.defineCommand({ - description: "Call another Rig tool repeatedly.", - input: rig.z.object({ count: rig.z.number() }), - output: rig.z.array(rig.z.number()), - run: async (context) => { - const values = []; - for (let index = 0; index < context.input.count; index++) { - values.push(await context.rig.run({ command: "callee.read" })); - } - return values; - }, - }), - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("caller", "call", { - input: JSON.stringify({ count: 50 }), - }); - - expect(result).toMatchObject({ exitCode: 0 }); - expect((result.envelope as { data: number[] }).data).toEqual(Array(50).fill(1)); - }); - - test("runs tools that import modules resolved from node_modules", async () => { - const home = await homes.create(); - const packageDir = join(home, "node_modules", "tool-helper"); - const toolDir = join(home, "rig", "tools", "external-import"); - await mkdir(packageDir, { recursive: true }); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(packageDir, "package.json"), - '{"name":"tool-helper","type":"module","exports":"./index.js"}\n', - "utf8", - ); - await writeFile( - join(packageDir, "index.js"), - "export function shout(value) { return String(value).toUpperCase(); }\n", - "utf8", - ); - await writeFile( - join(toolDir, "index.rig.ts"), - `import { shout } from "tool-helper"; - -export default (rig) => rig.defineTool({ - name: "external-import", - description: "External import test tool.", - commands: { - shout: rig.defineCommand({ - description: "Use an imported helper.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ text: rig.z.string() }), - run: async (context) => ({ text: shout(context.input.text) }), - }), - }, -}); -`, - "utf8", - ); - - const originalCwd = process.cwd(); - try { - const result = await new ToolRunnerClass({ homeDir: home }).run("external-import", "shout", { - homeDir: home, - input: '{"text":"modules"}', - }); - expect(result).toMatchObject({ - exitCode: 0, - envelope: { data: { text: "MODULES" }, errors: [] }, - }); - } finally { - process.chdir(originalCwd); - } - }); - - test("validates tool .env and passes it to command contexts", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "with-env"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, ".env"), - [ - "# Local tool env", - "API_TOKEN=secret", - "LIMIT=3", - 'MESSAGE="hello world"', - "SINGLE='literal value'", - "export OWNER=agent", - "", - ].join("\n"), - "utf8", - ); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "with-env", - description: "Env test tool.", - env: rig.z.object({ - API_TOKEN: rig.z.string().min(1), - LIMIT: rig.z.coerce.number(), - MESSAGE: rig.z.string(), - SINGLE: rig.z.string(), - OWNER: rig.z.string(), - }), - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ - token: rig.z.string(), - limit: rig.z.number(), - message: rig.z.string(), - single: rig.z.string(), - owner: rig.z.string(), - }), - run: async (context) => ({ - token: context.env.API_TOKEN, - limit: context.env.LIMIT, - message: context.env.MESSAGE, - single: context.env.SINGLE, - owner: context.env.OWNER, - }), - }), - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("with-env", "read", { - homeDir: home, - }); - expect(result).toMatchObject({ - exitCode: 0, - envelope: { - data: { - token: "secret", - limit: 3, - message: "hello world", - single: "literal value", - owner: "agent", - }, - errors: [], - }, - }); - - const dryRun = await new ToolRunnerClass({ homeDir: home }).run("with-env", "read", { - homeDir: home, - dryRun: true, - }); - expect(JSON.stringify(dryRun.envelope)).not.toContain("secret"); - }); - - test("renders metadata without loading required tool environment values", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "metadata-env"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "metadata-env", - description: "Metadata without env values.", - env: rig.z.object({ API_TOKEN: rig.z.string() }), - commands: { - read: rig.defineCommand({ - description: "Read required env.", - input: rig.z.object({}), - output: rig.z.string(), - run: (context) => context.env.API_TOKEN, - }), - }, -});\n`, - "utf8", - ); - - expect((await new ToolListServiceClass({ homeDir: home }).list()).tools[0]?.name).toBe( - "metadata-env", - ); - expect(await new ToolHelpServiceClass({ homeDir: home }).render("metadata-env")).toContain( - "# metadata-env", - ); - expect(await new ToolInspectorClass({ homeDir: home }).inspect("metadata-env")).toMatchObject({ - name: "metadata-env", - }); - }); - - test("returns tool env load errors as envelopes", async () => { - const cases = [ - { - name: "missing-env", - envFile: undefined, - envSchema: "env: rig.z.object({ API_TOKEN: rig.z.string().min(1) }),", - message: "env is invalid", - }, - { - name: "env-without-schema", - envFile: "API_TOKEN=secret\n", - envSchema: "", - message: "has .env but no env schema", - }, - { - name: "bad-env-line", - envFile: "not a valid env line\n", - envSchema: "env: rig.z.object({}),", - message: "Invalid .env line", - }, - ]; - - const results = await Promise.all( - cases.map(async (item) => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", item.name); - await mkdir(toolDir, { recursive: true }); - if (item.envFile !== undefined) - await writeFile(join(toolDir, ".env"), item.envFile, "utf8"); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: ${JSON.stringify(item.name)}, - description: "Env error test tool.", - ${item.envSchema} - commands: { - read: rig.defineCommand({ - description: "Read env.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -}); -`, - "utf8", - ); - - return { - item, - result: await new ToolRunnerClass({ homeDir: home }).run(item.name, "read", { - homeDir: home, - }), - }; - }), - ); - - for (const { item, result } of results) { - expect(result).toMatchObject({ - exitCode: 1, - envelope: { - errors: [{ code: "TOOL_INVALID", message: expect.stringContaining(item.message) }], - }, - }); - } - }); - - test("runs setupDb before commands and stores index.sqlite beside the tool", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "notes"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "notes", - description: "SQLite notes test tool.", - setupDb: (db) => { - db.migrate(1, "create notes", \` - create table notes ( - id integer primary key, - text text not null - ); - create table setup_runs ( - id integer primary key, - created_at text not null - ); - \`); - db.migrate(2, "index notes", "create index notes_text_idx on notes(text);"); - db.query("insert into setup_runs (created_at) values ($createdAt)").run({ - createdAt: new Date().toISOString(), - }); - }, - commands: { - add: rig.defineCommand({ - description: "Add a note.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ - id: rig.z.number(), - setupRuns: rig.z.number(), - dbPath: rig.z.string(), - }), - run: async (context) => { - const inserted = context.db - .query("insert into notes (text) values ($text)") - .run({ text: context.input.text }); - const row = context.db.query("select count(*) as count from setup_runs").get() as { count: number }; - return { id: inserted.lastInsertRowid, setupRuns: row.count, dbPath: context.db.path }; - }, - }), - }, -}); -`, - "utf8", - ); - - const runner = new ToolRunnerClass({ homeDir: home }); - const first = await runner.run("notes", "add", { homeDir: home, args: ["text=one"] }); - const second = await runner.run("notes", "add", { homeDir: home, args: ["text=two"] }); - const dbPath = join(toolDir, "index.sqlite"); - - expect(new ToolDatabaseServiceClass().dbPathForToolPath(join(toolDir, "index.rig.ts"))).toBe( - dbPath, - ); - expect(first).toMatchObject({ - exitCode: 0, - envelope: { data: { id: 1, setupRuns: 1, dbPath } }, - }); - expect(second).toMatchObject({ - exitCode: 0, - envelope: { data: { id: 2, setupRuns: 2, dbPath } }, - }); - expect(existsSync(dbPath)).toBe(true); - }); - - test("closes databases when factory initialization or setupDb fails", async () => { - const sqlite = new FakeSqliteEnvironment(); - sqlite.install(); - const home = await homes.create(); - const toolPath = join(home, "rig", "tools", "failure", "index.rig.ts"); - const dbPath = join(dirname(toolPath), "index.sqlite"); - const service = new ToolDatabaseServiceClass(); - - sqlite.failNextInitialization(); - await expect( - service.setup({ path: toolPath, definition: { setupDb: () => undefined } } as never), - ).rejects.toThrow("sqlite initialization failed"); - expect(FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(1); - - await expect( - service.setup({ - path: toolPath, - definition: { - setupDb: () => { - throw new Error("setupDb failed"); - }, - }, - } as never), - ).rejects.toThrow("setupDb failed"); - expect(FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(2); - }); - - test("skips collection initialization when a command does not use collections", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "partial-setup"); - const toolPath = join(toolDir, "index.rig.ts"); - const dbPath = join(toolDir, "index.sqlite"); - await mkdir(toolDir, { recursive: true }); - await writeFile(join(toolDir, "blocked"), "prevents collection directory creation", "utf8"); - await writeFile( - toolPath, - `export default (rig) => rig.defineTool({ - name: "partial-setup", - description: "Partial setup cleanup test.", - setupDb: () => {}, - collections: { blocked: {} }, - commands: { - read: rig.defineCommand({ - description: "Should not run.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => ({ ok: true }), - }), - }, -});`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("partial-setup", "read", { - homeDir: home, - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ data: { ok: true } }); - expect(FakeSqliteDatabase.store(dbPath)?.closeRuns).toBe(1); - }); - - test("provides tool loggers and sqlite-backed key-value state", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "stateful"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "stateful", - description: "KV and logger test tool.", - commands: { - write: rig.defineCommand({ - description: "Write lightweight state.", - input: rig.z.object({ key: rig.z.string(), value: rig.z.string() }), - output: rig.z.object({ previous: rig.z.string().optional(), current: rig.z.string(), kvPath: rig.z.string() }), - run: async (context) => { - const previous = context.kv.get(context.input.key); - context.kv.set(context.input.key, context.input.value); - context.log.info({ key: context.input.key }, "Stored key-value state."); - return { - previous, - current: context.kv.get(context.input.key), - kvPath: context.kv.path, - }; - }, - }), - bad: rig.defineCommand({ - description: "Reject bad state.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async (context) => { - context.kv.set("bad", undefined); - return { ok: true }; - }, - }), - }, -}); -`, - "utf8", - ); - - const runner = new ToolRunnerClass({ homeDir: home }); - const first = await runner.run("stateful", "write", { - homeDir: home, - args: ["key=name", "value=one"], - }); - const second = await runner.run("stateful", "write", { - homeDir: home, - args: ["key=name", "value=two"], - }); - const bad = await runner.run("stateful", "bad", { homeDir: home }); - const kvPath = join(toolDir, "kv.sqlite"); - - expect(new ToolKvStoreServiceClass().kvPathForToolPath(join(toolDir, "index.rig.ts"))).toBe( - kvPath, - ); - expect(first).toMatchObject({ - exitCode: 0, - envelope: { data: { current: "one", kvPath } }, - }); - expect(second).toMatchObject({ - exitCode: 0, - envelope: { data: { previous: "one", current: "two", kvPath } }, - }); - expect(bad).toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "INPUT_ERROR" }] }, - }); - expect(existsSync(kvPath)).toBe(true); - expect(existsSync(join(toolDir, "cache.sqlite"))).toBe(false); - expect(FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(3); - - const logOutput = await readFile(join(home, "rig", ".logs", "rig.log"), "utf8"); - expect(logOutput).toContain('"prefix":"tool:stateful.write"'); - expect(logOutput).toContain("Stored key-value state."); - - const kv = await new ToolKvStoreServiceClass().setup({ - path: join(toolDir, "index.rig.ts"), - } as never); - expect(() => kv.get("")).toThrow("context.kv keys must be non-empty strings."); - (kv as unknown as { close(): void }).close(); - - new FakeSqliteEnvironment().uninstall(); - const unavailable = await new ToolKvStoreServiceClass().setup({ - path: join(home, "unavailable", "index.rig.ts"), - } as never); - expect(unavailable.path).toBe(join(home, "unavailable", "kv.sqlite")); - expect(() => unavailable.get("key")).toThrow("context.kv requires the Bun SQLite runtime"); - expect(() => unavailable.set("key", "value")).toThrow( - "context.kv requires the Bun SQLite runtime", - ); - }); - - test("provides a persistent query cache with foreground refreshes", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "cached"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "cached", - description: "Cache test tool.", - commands: { - read: rig.defineCommand({ - description: "Read cached data.", - input: rig.z.object({ staleTime: rig.z.number(), fail: rig.z.boolean().optional() }), - output: rig.z.object({ value: rig.z.string(), calls: rig.z.number(), cachePath: rig.z.string() }), - run: async (context) => { - const value = await context.cache.query({ - queryKey: ["value"], - staleTime: context.input.staleTime, - queryFn: async () => { - const calls = (context.kv.get("calls") ?? 0) + 1; - context.kv.set("calls", calls); - if (context.input.fail) throw new Error("refresh failed"); - return "value-" + calls; - }, - }); - return { value, calls: context.kv.get("calls") ?? 0, cachePath: context.cache.path }; - }, - }), - }, -}); -`, - "utf8", - ); - - const runner = new ToolRunnerClass({ homeDir: home }); - const cold = await runner.run("cached", "read", { - homeDir: home, - input: JSON.stringify({ staleTime: 60_000 }), - }); - const fresh = await runner.run("cached", "read", { - homeDir: home, - input: JSON.stringify({ staleTime: 60_000 }), - }); - const stale = await runner.run("cached", "read", { - homeDir: home, - input: JSON.stringify({ staleTime: 0 }), - }); - const refreshed = await runner.run("cached", "read", { - homeDir: home, - input: JSON.stringify({ staleTime: 60_000 }), - }); - const failedRefresh = await runner.run("cached", "read", { - homeDir: home, - input: JSON.stringify({ staleTime: 0, fail: true }), - }); - const cachePath = join(toolDir, "cache.sqlite"); - - expect(cold).toMatchObject({ - exitCode: 0, - envelope: { data: { value: "value-1", calls: 1, cachePath } }, - }); - expect(fresh).toMatchObject({ - exitCode: 0, - envelope: { data: { value: "value-1", calls: 1, cachePath } }, - }); - expect(stale).toMatchObject({ - exitCode: 0, - envelope: { data: { value: "value-2", calls: 2, cachePath } }, - }); - expect(refreshed).toMatchObject({ - exitCode: 0, - envelope: { data: { value: "value-2", calls: 2, cachePath } }, - }); - expect(failedRefresh).toMatchObject({ - exitCode: 1, - envelope: { errors: [{ code: "INTERNAL_ERROR", message: "refresh failed" }] }, - }); - expect(existsSync(cachePath)).toBe(true); - }); - - test("supports cache controls, deterministic keys, validation, and unavailable runtimes", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const toolPath = join(home, "rig", "tools", "cache-api", "index.rig.ts"); - const warnings: Array<{ bindings: unknown; message: unknown }> = []; - const log = { - warn: (bindings: unknown, message: unknown) => warnings.push({ bindings, message }), - } as never; - const service = new ToolCacheServiceClass(); - const cache = await service.setup({ path: toolPath } as never, log); - const cachePath = join(dirname(toolPath), "cache.sqlite"); - - expect(service.cachePathForToolPath(toolPath)).toBe(cachePath); - expect(cache.path).toBe(cachePath); - expect(existsSync(cachePath)).toBe(false); - expect(cache.peek(["missing"])).toBeUndefined(); - expect(existsSync(cachePath)).toBe(true); - - const objectKey = ["todos", { b: 2, omitted: undefined, a: 1 }] as const; - cache.set(objectKey, { ok: true }); - expect(cache.peek(["todos", { a: 1, b: 2 }])).toEqual({ ok: true }); - expect(cache.peek(["todos", { a: 1, b: 3 }])).toBeUndefined(); - - const shared = { value: true }; - const nullPrototype = Object.assign(Object.create(null) as Record, { - enabled: true, - }); - cache.set(["json", null, true, 1, "text", shared, shared, nullPrototype], "valid"); - expect(cache.peek(["json", null, true, 1, "text", shared, shared, { enabled: true }])).toBe( - "valid", - ); - - let calls = 0; - const first = cache.query({ - queryKey: ["dedupe"], - queryFn: async () => `value-${++calls}`, - }); - const second = cache.query({ - queryKey: ["dedupe"], - queryFn: async () => `ignored-${++calls}`, - }); - await expect(Promise.all([first, second])).resolves.toEqual(["value-1", "value-1"]); - expect(calls).toBe(1); - await expect( - cache.query({ queryKey: ["dedupe"], staleTime: Infinity, queryFn: () => "ignored" }), - ).resolves.toBe("value-1"); - - cache.set(["invalidated"], "old"); - cache.invalidate(["invalidated"]); - await expect( - cache.query({ - queryKey: ["invalidated"], - staleTime: Infinity, - queryFn: () => "new", - }), - ).resolves.toBe("new"); - expect(cache.peek(["invalidated"])).toBe("new"); - - cache.set(["failure"], "stale"); - await expect( - cache.query({ - queryKey: ["failure"], - staleTime: 0, - queryFn: () => { - throw new Error("foreground failure"); - }, - }), - ).rejects.toThrow("foreground failure"); - expect(cache.peek(["failure"])).toBe("stale"); - expect(warnings).toHaveLength(0); - - await expect( - cache.query({ - queryKey: ["cold-failure"], - queryFn: () => Promise.reject(new Error("cold failure")), - }), - ).rejects.toThrow("cold failure"); - expect(warnings).toHaveLength(0); - - cache.set(["remove"], true); - cache.remove(["remove"]); - expect(cache.peek(["remove"])).toBeUndefined(); - cache.set(["clear-a"], true); - cache.set(["clear-b"], true); - cache.clear(); - expect(cache.peek(["clear-a"])).toBeUndefined(); - - expect(() => cache.peek([])).toThrow("query keys must be non-empty arrays"); - expect(() => cache.peek("bad" as never)).toThrow("query keys must be non-empty arrays"); - expect(() => cache.peek([Number.NaN])).toThrow("JSON-compatible values"); - expect(() => cache.peek([Number.POSITIVE_INFINITY])).toThrow("JSON-compatible values"); - expect(() => cache.peek([undefined])).toThrow("JSON-compatible values"); - expect(() => cache.peek([() => true])).toThrow("JSON-compatible values"); - expect(() => cache.peek([Symbol("bad")])).toThrow("JSON-compatible values"); - expect(() => cache.peek([1n])).toThrow("JSON-compatible values"); - expect(() => cache.peek([new Date()])).toThrow("JSON-compatible values"); - const cyclic: unknown[] = []; - cyclic.push(cyclic); - expect(() => cache.peek(cyclic)).toThrow("cannot contain cycles"); - expect(() => cache.set(["undefined"], undefined)).toThrow("cannot store undefined"); - await expect( - cache.query({ queryKey: ["bad-stale"], staleTime: -1, queryFn: () => true }), - ).rejects.toThrow("staleTime must be a non-negative number"); - await expect( - cache.query({ queryKey: ["bad-stale"], staleTime: Number.NaN, queryFn: () => true }), - ).rejects.toThrow("staleTime must be a non-negative number"); - await expect( - cache.query({ queryKey: ["bad-stale"], staleTime: "bad" as never, queryFn: () => true }), - ).rejects.toThrow("staleTime must be a non-negative number"); - - cache.set(["collision"], true); - const row = [...(FakeSqliteDatabase.store(cachePath)?.cache.values() ?? [])].find( - (value) => value.valueJson === "true", - ); - expect(row).toBeDefined(); - row!.keyJson = '["different"]'; - expect(() => cache.peek(["collision"])).toThrow("query key hash collision"); - cache.close(); - - new FakeSqliteEnvironment().uninstall(); - const unavailable = await service.setup({ path: toolPath } as never, log); - expect(() => unavailable.query({ queryKey: ["key"], queryFn: () => true })).toThrow( - "context.cache requires the Bun SQLite runtime", - ); - expect(() => unavailable.peek(["key"])).toThrow( - "context.cache requires the Bun SQLite runtime", - ); - expect(() => unavailable.set(["key"], true)).toThrow( - "context.cache requires the Bun SQLite runtime", - ); - expect(() => unavailable.invalidate(["key"])).toThrow( - "context.cache requires the Bun SQLite runtime", - ); - expect(() => unavailable.remove(["key"])).toThrow( - "context.cache requires the Bun SQLite runtime", - ); - expect(() => unavailable.clear()).toThrow("context.cache requires the Bun SQLite runtime"); - expect(() => unavailable.close()).not.toThrow(); - }); - - test("closes partial lazy state initialization and retries cleanly", async () => { - const sqlite = new FakeSqliteEnvironment(); - sqlite.install(); - const home = await homes.create(); - const toolPath = join(home, "rig", "tools", "partial", "index.rig.ts"); - const kvPath = join(dirname(toolPath), "kv.sqlite"); - const cachePath = join(dirname(toolPath), "cache.sqlite"); - const log = { warn: () => undefined } as never; - - const kv = await new ToolKvStoreServiceClass().setup({ path: toolPath } as never); - sqlite.failNextInitialization(); - expect(() => kv.get("key")).toThrow("sqlite initialization failed"); - expect(FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(1); - expect(kv.get("key")).toBeUndefined(); - (kv as { close(): void }).close(); - expect(FakeSqliteDatabase.store(kvPath)?.closeRuns).toBe(2); - - const cache = await new ToolCacheServiceClass().setup({ path: toolPath } as never, log); - sqlite.failNextInitialization(); - expect(() => cache.peek(["key"])).toThrow("sqlite initialization failed"); - expect(FakeSqliteDatabase.store(cachePath)?.closeRuns).toBe(1); - expect(cache.peek(["key"])).toBeUndefined(); - cache.close(); - expect(FakeSqliteDatabase.store(cachePath)?.closeRuns).toBe(2); - }); - - test("does not let cache cleanup failures mask command results", async () => { - const runner = new ToolRunnerClass(); - expect(() => - ( - runner as unknown as { - closeCache(cache: { close(): void }): void; - } - ).closeCache({ - close: () => { - throw new Error("close failed"); - }, - }), - ).not.toThrow(); - }); - - test("returns changed migration errors as envelopes", async () => { - new FakeSqliteEnvironment().install(); - const home = await homes.create(); - const writer = new DbSetupTestToolWriter(home); - const toolPath = await writer.write( - "db-changed", - 'db.migrate(1, "create items", "create table items (id integer primary key);");', - ); - - await expect( - new ToolRunnerClass({ homeDir: home }).run("db-changed", "check", { homeDir: home }), - ).resolves.toMatchObject({ exitCode: 0 }); - await writer.write( - "db-changed", - 'db.migrate(1, "create items", "create table items (id integer primary key, text text);");', - ); - expect(toolPath).toBe(join(home, "rig", "tools", "db-changed", "index.rig.ts")); - - const result = await new ToolRunnerClass({ homeDir: home }).run("db-changed", "check", { - homeDir: home, - }); - expect(result).toMatchObject({ - exitCode: 1, - envelope: { - errors: [ - { - code: "TOOL_INVALID", - message: "Migration 1 has changed since it was applied.", - }, - ], - }, - }); - }); - - test("returns invalid migration declaration errors as envelopes", async () => { - new FakeSqliteEnvironment().install(); - const cases = [ - { - name: "db-order", - setup: - 'db.migrate(2, "create second", "create table second (id integer primary key);"); db.migrate(1, "create first", "create table first (id integer primary key);");', - message: "Migration versions must be declared in ascending order", - }, - { - name: "db-version", - setup: 'db.migrate(0, "bad", "select 1;");', - message: "Migration version must be a positive integer", - }, - { - name: "db-name", - setup: 'db.migrate(1, " ", "select 1;");', - message: "Migration name must not be empty.", - }, - { - name: "db-sql", - setup: 'db.migrate(1, "empty", " ");', - message: "Migration 1 SQL must not be empty.", - }, - ]; - - const results = await Promise.all( - cases.map(async (item) => { - const home = await homes.create(); - await new DbSetupTestToolWriter(home).write(item.name, item.setup); - return { - item, - result: await new ToolRunnerClass({ homeDir: home }).run(item.name, "check", { - homeDir: home, - }), - }; - }), - ); - - for (const { item, result } of results) { - expect(result).toMatchObject({ - exitCode: 1, - envelope: { - errors: [{ code: "TOOL_INVALID", message: expect.stringContaining(item.message) }], - }, - }); - } - }); - - test("requires setupDb before commands use context.db", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "missing-db"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "missing-db", - description: "Missing setupDb test tool.", - commands: { - read: rig.defineCommand({ - description: "Read from DB without setupDb.", - input: rig.z.object({}), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async (context) => { - context.db.query("select 1").get(); - return { ok: true }; - }, - }), - }, -}); -`, - "utf8", - ); - - await expect( - new ToolRunnerClass({ homeDir: home }).run("missing-db", "read", { homeDir: home }), - ).resolves.toMatchObject({ - exitCode: 1, - envelope: { - errors: [ - { - code: "TOOL_INVALID", - message: "Tool missing-db must define setupDb before using context.db.", - }, - ], - }, - }); - }); - - test("renders a compact plain command list", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const service = new ToolListServiceClass({ homeDir: home }); - const list = await service.list(); - - const rendered = service.renderPlain(list); - expect(rendered).toContain("rig run sample.example text=example #"); - }); - - test("dry-runs a command without execution", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const toolDir = join(home, "rig", "tools", "writer"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "writer", - description: "Writer test tool.", - commands: { - save: rig.defineCommand({ - description: "Save text.", - input: rig.z.object({ text: rig.z.string() }), - output: rig.z.object({ ok: rig.z.boolean() }), - run: async () => { - throw new Error("dry-run should not execute command code"); - }, - }), - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("writer", "save", { - homeDir: home, - args: ["text=Agent"], - dryRun: true, - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { - dryRun: true, - wouldRun: false, - id: "writer.save", - input: { text: "Agent" }, - }, - errors: [], - }); - }); - - test("truncates large command output and saves the full data to a temp file", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const toolDir = join(home, "rig", "tools", "large"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `export default (rig) => rig.defineTool({ - name: "large", - description: "Large output test tool.", - commands: { - dump: rig.defineCommand({ - description: "Dump large text.", - input: rig.z.object({}), - output: rig.z.object({ text: rig.z.string() }), - run: async () => ({ text: "x".repeat(60 * 1024) }), - }), - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("large", "dump", { - homeDir: home, - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { - truncated: true, - previewFormat: "partial-json", - fullOutputFormat: "json", - totalBytes: expect.any(Number), - shownBytes: expect.any(Number), - }, - errors: [], - }); - - const envelope = result.envelope as { data: { fullOutputPath: string } }; - const fullOutput = await readFile(envelope.data.fullOutputPath, "utf8"); - expect(fullOutput).toContain('"text"'); - expect(fullOutput.length).toBeGreaterThan(60 * 1024); - }); - - test("accepts raw Zod schemas without rig.input/output wrappers", async () => { - const home = await homes.create(); - const toolDir = join(home, "rig", "tools", "raw-schema"); - await mkdir(toolDir, { recursive: true }); - await writeFile( - join(toolDir, "index.rig.ts"), - `import { RigTool, z } from "../../runtime/sdk"; - -export default RigTool.define({ - name: "raw-schema", - description: "Raw schema test tool.", - commands: { - echo: { - description: "Echo using raw z.object.", - input: z.object({ text: z.string() }), - output: z.object({ text: z.string() }), - run: async ({ input }) => ({ text: input.text }), - }, - }, -}); -`, - "utf8", - ); - - const result = await new ToolRunnerClass({ homeDir: home }).run("raw-schema", "echo", { - homeDir: home, - input: '{"text":"Agent"}', - }); - - expect(result.exitCode).toBe(0); - expect(result.envelope).toMatchObject({ - data: { text: "Agent" }, - errors: [], - }); - }); - - test("returns an error envelope for invalid input", async () => { - const home = await homes.create(); - await new ToolCreatorClass({ homeDir: home }).create("sample"); - const result = await new ToolRunnerClass({ homeDir: home }).run("sample", "example", { - homeDir: home, - input: '{"text":123}', - }); - expect(result.exitCode).toBe(1); - expect(result.envelope).toMatchObject({ - data: null, - errors: [ - { - code: "VALIDATION_ERROR", - message: "Invalid input.", - details: { - issues: [ - { - path: "text", - message: expect.any(String), - }, - ], - }, - }, - ], - }); - }); -}); diff --git a/src/tools/run.ts b/src/tools/run.ts deleted file mode 100644 index 5f106a5..0000000 --- a/src/tools/run.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./execution/tool-runner"; diff --git a/src/tools/runtime-instruction.ts b/src/tools/runtime-instruction.ts deleted file mode 100644 index 65e0eed..0000000 --- a/src/tools/runtime-instruction.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../generated/tool/tool-runtime-instruction-sync"; diff --git a/src/tools/schema.ts b/src/tools/schema.ts deleted file mode 100644 index 648f76c..0000000 --- a/src/tools/schema.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./domain/schema-renderer"; diff --git a/src/tools/sdk.ts b/src/tools/sdk.ts deleted file mode 100644 index ff71580..0000000 --- a/src/tools/sdk.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./sdk/tool-sdk"; diff --git a/src/tools/sdk/tool-sdk.ts b/src/tools/sdk/tool-sdk.ts deleted file mode 100644 index 21dc33e..0000000 --- a/src/tools/sdk/tool-sdk.ts +++ /dev/null @@ -1,292 +0,0 @@ -import { existsSync, statSync } from "node:fs"; -import { mkdir } from "node:fs/promises"; -import { homedir } from "node:os"; -import { dirname, isAbsolute, join, resolve } from "node:path"; -import { z } from "zod"; -import { defineRuntime, defineService, defineSingleton } from "../../define"; -import type { ConfigOptions } from "../../config/config"; -import { RigErrorClass } from "../../errors/RigError"; -import type { SuccessEnvelope } from "../../runtime/envelope"; -import { BunRigShellClass } from "../../runtime/shell"; -import { commandTargets } from "../identifiers"; -import { - type CommandDefinition, - type NormalizedToolDefinitionInput, - type RigArgBuilder, - type RigPathHelper, - type RigRunOptions, - type RigSchema, - type RigToolKit, - type ToolCollectionDefinitions, - type ToolCommandMap, - type ToolDefinitionInput, - type ToolFactory, - type ToolModuleDefault, -} from "../types"; - -export { z }; - -const RigPathRuntimeProductionDeps = { - homedir, - exists: existsSync, - stat: statSync, - mkdir, - dirname, - isAbsolute, - join, - resolve, -}; - -export class RigPathRuntimeService extends defineRuntime({ - params: {}, - deps: RigPathRuntimeProductionDeps, -}) { - public home(_params: {}): string { - return this.deps.homedir(); - } - - public resolve(params: { cwd: string; pathValue: string }): string { - if (params.pathValue === "~") return this.deps.homedir(); - if (params.pathValue.startsWith("~/")) { - return this.deps.join(this.deps.homedir(), params.pathValue.slice(2)); - } - return this.deps.isAbsolute(params.pathValue) - ? params.pathValue - : this.deps.resolve(params.cwd, params.pathValue); - } - - public async ensureParent(params: { pathValue: string }): Promise { - await this.deps.mkdir(this.deps.dirname(params.pathValue), { recursive: true }); - } - - public size(params: { pathValue: string }): number { - return this.deps.exists(params.pathValue) ? this.deps.stat(params.pathValue).size : 0; - } -} - -export const RigPathRuntime = new RigPathRuntimeService(); - -function createRigPathHelper(service: RigPathRuntimeService = RigPathRuntime): RigPathHelper { - return { - home() { - return service.home({}); - }, - resolve(cwd, pathValue) { - return service.resolve({ cwd, pathValue }); - }, - async ensureParent(pathValue) { - await service.ensureParent({ pathValue }); - }, - size(pathValue) { - return service.size({ pathValue }); - }, - }; -} - -function createRigArgBuilder(_params: {}): RigArgBuilder { - const values: string[] = []; - const builder: RigArgBuilder = { - raw(...items: string[]) { - values.push(...items); - return builder; - }, - flag(name: string, enabled = true) { - if (enabled) values.push(name); - return builder; - }, - value(name: string, value: string | number | boolean | undefined | null) { - if (value !== undefined && value !== null) values.push(name, String(value)); - return builder; - }, - values(name: string, items: string[] | undefined) { - for (const value of items ?? []) values.push(name, value); - return builder; - }, - toArray() { - return [...values]; - }, - }; - return builder; -} - -export const RigArgsRuntimeSingleton = defineSingleton({ - params: {}, - deps: {}, - create: createRigArgBuilder, -}); - -type RigToolKitFactoryOptions = ConfigOptions; - -export type RigCommandRunnerDependencies = { - run( - tool: string, - command: string, - options: ConfigOptions & { args?: string[]; input?: string; dryRun?: boolean }, - ): Promise<{ envelope: unknown; exitCode: number }>; -}; - -type RigCommandRunnerDeps = { - runner?: RigCommandRunnerDependencies; - createDefaultRunner: (options: RigToolKitFactoryOptions) => Promise; -}; - -const RigCommandRunnerProductionDeps: RigCommandRunnerDeps = { - async createDefaultRunner(options) { - const toolRunnerModule = await import("../run"); - return new toolRunnerModule.ToolRunnerClass(options); - }, -}; - -function commandTarget(params: { options: RigRunOptions }): { - tool: string; - command: string; - id: string; -} { - return params.options.tool - ? commandTargets.parse(`${params.options.tool}.${params.options.command}`) - : commandTargets.parse(params.options.command); -} - -export class RigCommandRunnerService extends defineService({ - params: {} as RigToolKitFactoryOptions, - deps: RigCommandRunnerProductionDeps, -}) { - public async run(params: { options: RigRunOptions }): Promise { - const target = commandTarget(params); - const runner = this.deps.runner ?? (await this.deps.createDefaultRunner(this.params)); - const result = await runner.run(target.tool, target.command, { - ...this.params, - args: params.options.args, - input: params.options.input === undefined ? undefined : JSON.stringify(params.options.input), - dryRun: params.options.dryRun, - }); - - if (result.exitCode === 0) return (result.envelope as SuccessEnvelope).data as T; - throw new RigErrorClass("TOOL_RUN_ERROR", `Rig tool command failed: ${target.id}`, { - command: target.id, - envelope: result.envelope, - }); - } -} - -export const RigCommandRunner = new RigCommandRunnerService(); - -type RigToolKitFactoryDeps = { - runner?: RigCommandRunnerDependencies; - createDefaultRunner: RigCommandRunnerDeps["createDefaultRunner"]; - createShell: () => BunRigShellClass; - createPaths: () => RigPathHelper; - createArgs: () => RigArgBuilder; -}; - -const RigToolKitFactoryProductionDeps: RigToolKitFactoryDeps = { - createDefaultRunner: RigCommandRunnerProductionDeps.createDefaultRunner, - createShell() { - return new BunRigShellClass(); - }, - createPaths: createRigPathHelper, - createArgs() { - return RigArgsRuntimeSingleton.create({}); - }, -}; - -export class RigToolKitFactoryService extends defineService({ - params: {} as RigToolKitFactoryOptions, - deps: RigToolKitFactoryProductionDeps, -}) { - public create(_params: {}): RigToolKit { - const shell = this.deps.createShell(); - const commandRunner = new RigCommandRunnerService({ - params: this.params, - deps: { - runner: this.deps.runner, - createDefaultRunner: this.deps.createDefaultRunner, - }, - }); - return { - z, - defineTool: < - Env extends RigSchema = RigSchema, - Collections extends ToolCollectionDefinitions = ToolCollectionDefinitions, - const Commands extends ToolCommandMap = ToolCommandMap, - >( - definition: ToolDefinitionInput, - ) => - typeof definition.commands === "function" - ? { ...definition, commands: definition.commands((command) => command) } - : definition, - defineCommand: ( - definition: CommandDefinition, - ) => definition, - run: (options: RigRunOptions) => commandRunner.run({ options }), - $: (strings: TemplateStringsArray, ...values: unknown[]) => shell.$(strings, ...values), - args: this.deps.createArgs, - paths: this.deps.createPaths(), - shell, - } as RigToolKit; - } -} - -export const RigToolKitFactory = new RigToolKitFactoryService(); - -export const rig = RigToolKitFactory.create({}); - -export function defineTool< - Env extends RigSchema = RigSchema, - Collections extends ToolCollectionDefinitions = ToolCollectionDefinitions, - const Commands extends ToolCommandMap = ToolCommandMap, ->( - definition: ToolDefinitionInput, -): NormalizedToolDefinitionInput; -export function defineTool(factory: ToolFactory): ToolFactory; -export function defineTool(value: ToolModuleDefault): ToolModuleDefault { - return typeof value === "function" ? value : rig.defineTool(value as never); -} - -export const defineCommand = rig.defineCommand; -export const run = rig.run; -export const args = rig.args; -export const paths = rig.paths; - -function defineRigTool(params: { value: ToolModuleDefault }): ToolModuleDefault { - return defineTool(params.value as never); -} - -export const RigToolSingleton = defineSingleton({ - params: {}, - deps: {}, - define: defineRigTool, -}); - -export type RigToolClass = { - define(value: ToolDefinitionInput): ToolDefinitionInput; - define(value: ToolFactory): ToolFactory; -}; - -type RigToolConstructor = { - new (): RigToolClass; - readonly prototype: RigToolClass; -}; - -const RigToolClassAdapter = function constructRigTool(): void {}; -Object.defineProperty(RigToolClassAdapter, "name", { value: "RigToolClass" }); -Object.defineProperty(RigToolClassAdapter.prototype, "define", { - configurable: true, - value: function define(value: ToolModuleDefault) { - return RigToolSingleton.define({ value }); - }, - writable: true, -}); - -export const RigToolClass = RigToolClassAdapter as unknown as RigToolConstructor; -export const RigTool = new RigToolClass(); - -export function createRigToolKit( - options: RigToolKitFactoryOptions = {}, - dependencies?: RigCommandRunnerDependencies, -): RigToolKit { - return new RigToolKitFactoryService({ - params: options, - deps: { ...RigToolKitFactoryProductionDeps, runner: dependencies }, - }).create({}); -} diff --git a/src/tools/typecheck.ts b/src/tools/typecheck.ts deleted file mode 100644 index 432d1bc..0000000 --- a/src/tools/typecheck.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./management/tool-typecheck"; diff --git a/src/tools/types.ts b/src/tools/types.ts deleted file mode 100644 index 820f72e..0000000 --- a/src/tools/types.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./domain/tool-types"; diff --git a/test/domains/updates/runtime/dev-link.test.ts b/test/domains/updates/runtime/dev-link.test.ts new file mode 100644 index 0000000..bad2802 --- /dev/null +++ b/test/domains/updates/runtime/dev-link.test.ts @@ -0,0 +1,142 @@ +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { delimiter, join } from "node:path"; +import { Effect, Layer } from "effect"; +import { afterEach, describe, expect, test } from "vite-plus/test"; +import { DevLinkConfigService } from "../../../../src/domains/updates/config/dev-link-config"; +import { DevLinkPlatformService } from "../../../../src/domains/updates/repo/dev-link-repository"; +import { + DevLinkMutationService, + DevLinkPathService, + DevLinkRenderService, + DevLinkStateService, +} from "../../../../src/domains/updates/service/dev-link"; +import type { DevLinkCommandOptions } from "../../../../src/domains/updates/types/dev-link"; +import { DevLinkService } from "../../../../src/domains/updates/runtime/dev-link"; + +const workspaces: string[] = []; + +const createWorkspace = async () => { + const path = await mkdtemp(join(tmpdir(), "rig-test-workspace-")); + workspaces.push(path); + return path; +}; + +const createRigRepo = async (path: string) => { + const cliDirectory = join(path, "src", "app", "cli"); + await mkdir(cliDirectory, { recursive: true }); + await writeFile(join(path, "package.json"), '{"name":"rig"}\n', "utf8"); + await writeFile(join(cliDirectory, "entrypoint.ts"), "console.log('rig dev');\n", "utf8"); +}; + +const createDevLinkLayer = (params: { + homeDir: string; + repoRoot: string; + platform?: NodeJS.Platform; + pathEnvironment?: string; +}) => { + const configLayer = Layer.succeed(DevLinkConfigService, { + get: Effect.succeed({ + homeDir: params.homeDir, + repoRoot: params.repoRoot, + platform: params.platform ?? process.platform, + pathEnvironment: params.pathEnvironment ?? process.env.PATH ?? "", + pathDelimiter: delimiter, + }), + }); + const pathLayer = DevLinkPathService.layer.pipe( + Layer.provide(Layer.merge(configLayer, DevLinkPlatformService.layer)), + ); + const stateLayer = DevLinkStateService.layer.pipe( + Layer.provide(Layer.mergeAll(configLayer, DevLinkPlatformService.layer, pathLayer)), + ); + const mutationLayer = DevLinkMutationService.layer.pipe( + Layer.provide(Layer.mergeAll(configLayer, DevLinkPlatformService.layer, pathLayer, stateLayer)), + ); + return DevLinkService.layer.pipe( + Layer.provide(Layer.mergeAll(mutationLayer, stateLayer, DevLinkRenderService.layer)), + ); +}; + +const run = ( + layer: Layer.Layer, + operation: (service: DevLinkService["Service"]) => Effect.Effect, +) => Effect.runPromise(DevLinkService.use(operation).pipe(Effect.provide(layer))); + +afterEach(async () => { + await Promise.all(workspaces.splice(0).map((path) => rm(path, { recursive: true, force: true }))); +}); + +describe("dev link", () => { + test("links and unlinks a rig dev shim", async () => { + const home = await createWorkspace(); + const repoRoot = await createWorkspace(); + await createRigRepo(repoRoot); + const layer = createDevLinkLayer({ homeDir: home, repoRoot }); + const linked = await run(layer, (service) => service.link()); + expect(linked).toMatchObject({ exists: true, isRigDevShim: true, pointsToCurrentRepo: true }); + expect(existsSync(linked.shimPath)).toBe(true); + const shim = await readFile(linked.shimPath, "utf8"); + expect(shim).toContain("Rig dev shim"); + expect(shim).toContain("bun --install=fallback run"); + + const unlinked = await run(layer, (service) => service.unlink()); + expect(unlinked.exists).toBe(false); + expect(existsSync(linked.shimPath)).toBe(false); + }); + + test("refuses to overwrite a non-Rig shim without force", async () => { + const home = await createWorkspace(); + const repoRoot = await createWorkspace(); + await createRigRepo(repoRoot); + const binDir = join(home, ".local", "bin"); + await mkdir(binDir, { recursive: true }); + await writeFile(join(binDir, "rig"), "#!/usr/bin/env bash\necho nope\n", "utf8"); + const layer = createDevLinkLayer({ homeDir: home, repoRoot, platform: "linux" }); + await expect(run(layer, (service) => service.link())).rejects.toThrow( + "Refusing to overwrite existing file", + ); + }); + + test("renders link and unlink results through the service", async () => { + const homeDir = "/home/rig"; + const repoRoot = await createWorkspace(); + await createRigRepo(repoRoot); + const layer = createDevLinkLayer({ homeDir, repoRoot, pathEnvironment: "/bin" }); + const status = { + repoRoot, + binDir: "/home/rig/.local/bin", + shimPath: "/home/rig/.local/bin/rig", + exists: true, + isRigDevShim: true, + pointsToCurrentRepo: true, + binDirOnPath: false, + }; + expect(await run(layer, (service) => service.renderLink(status))).toContain( + "Add /home/rig/.local/bin to PATH", + ); + expect( + await run(layer, (service) => service.renderLink({ ...status, binDirOnPath: true })), + ).not.toContain("Add /home/rig/.local/bin to PATH"); + expect(await run(layer, (service) => service.renderUnlink(status))).toContain( + "Rig dev link removed.", + ); + await expect(run(layer, (service) => service.status())).resolves.toBeDefined(); + }); + + test("writes a Windows command shim when requested", async () => { + const home = await createWorkspace(); + const repoRoot = await createWorkspace(); + await createRigRepo(repoRoot); + const layer = createDevLinkLayer({ homeDir: home, repoRoot, platform: "win32" }); + const options: DevLinkCommandOptions = {}; + const linked = await run(layer, (service) => service.link(options)); + const shim = await readFile(linked.shimPath, "utf8"); + expect(linked.shimPath).toBe(join(home, ".local", "bin", "rig.cmd")); + expect(linked.pointsToCurrentRepo).toBe(true); + expect(shim).toContain("@echo off"); + expect(shim).toContain('set "RIG_DEV_REPO='); + expect(shim).toContain('run "%RIG_DEV_REPO%\\src\\app\\cli\\entrypoint.ts" %*'); + }); +}); diff --git a/src/runtime/updates/rig-updater.test.ts b/test/domains/updates/runtime/rig-updater.test.ts similarity index 50% rename from src/runtime/updates/rig-updater.test.ts rename to test/domains/updates/runtime/rig-updater.test.ts index 23e6ad7..9e642dd 100644 --- a/src/runtime/updates/rig-updater.test.ts +++ b/test/domains/updates/runtime/rig-updater.test.ts @@ -1,15 +1,35 @@ -import { spawn } from "node:child_process"; -import type { SpawnOptions } from "node:child_process"; import { EventEmitter } from "node:events"; import { PassThrough } from "node:stream"; +import { Effect, Layer } from "effect"; import { describe, expect, it } from "vite-plus/test"; import { - RigUpdateCommandRunnerClass, - RigUpdaterClass, - RigUpdaterFactoryClass, + ManagedProcessConfigService, + ManagedProcessService, +} from "../../../../src/providers/process/managed-process"; +import { + RigShellConfigService, + type RigShellChildProcess, + RigShellPlatformService, + RigShellService, + ShellOutputService, + ShellProcessService, + ShellTemplateService, +} from "../../../../src/providers/process/rig-shell-service"; +import { RigUpdaterConfigService } from "../../../../src/domains/updates/config/rig-updater-config"; +import { + RigUpdateCommandRunnerService, + RigUpdaterPlatformService, + rigUpdateCommandRunnerLayer, +} from "../../../../src/domains/updates/repo/rig-updater-repository"; +import { type RigUpdatePlan, + RigUpdateError, type RigUpdateStep, -} from "./rig-updater"; +} from "../../../../src/domains/updates/types/updates"; +import { + RigUpdaterService, + rigUpdaterOperationLayer, +} from "../../../../src/domains/updates/runtime/rig-updater"; const npmRoot = "/runtime/lib/node_modules/@rendotdev/rig"; const npmExecutable = "/runtime/bin/npm"; @@ -18,7 +38,7 @@ const bunRoot = "/Users/ren/.bun/install/global/node_modules/@rendotdev/rig"; const bunExecutable = "/Users/ren/.bun/bin/bun"; const bunRigExecutable = "/Users/ren/.bun/bin/rig"; -class RigUpdaterTestContextClass { +class RigUpdaterTestContext { public readonly reads: RigUpdateStep[] = []; public readonly runs: RigUpdateStep[] = []; public latestOutput = '"0.0.42"'; @@ -26,42 +46,116 @@ class RigUpdaterTestContextClass { public runOutput = "changed 1 package\n"; public readonly executablePaths = new Set([npmExecutable, rigExecutable]); - public create(params: { packageRoot?: string; currentVersion?: string } = {}): RigUpdaterClass { - return new RigUpdaterClass( - { - packageRoot: params.packageRoot ?? npmRoot, - currentVersion: params.currentVersion ?? "0.0.41", - }, - { - executableExists: (path) => this.executablePaths.has(path), - readCommand: async (step) => { - this.reads.push(step); - return step.args.includes("--version") ? this.versionOutput : this.latestOutput; - }, - runCommand: async (step) => { - this.runs.push(step); - return this.runOutput; - }, - }, + public create(params: { packageRoot?: string; currentVersion?: string } = {}) { + const executablePaths = this.executablePaths; + const reads = this.reads; + const runs = this.runs; + const versionOutput = this.versionOutput; + const latestOutput = this.latestOutput; + const runOutput = this.runOutput; + const updaterLayer = rigUpdaterOperationLayer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(RigUpdaterConfigService, { + params: { + packageRoot: params.packageRoot ?? npmRoot, + currentVersion: params.currentVersion ?? "0.0.41", + }, + }), + Layer.succeed(RigUpdaterPlatformService, { + executableExists: Effect.fn("RigUpdaterPlatformService.executableExists")(function* ( + path: string, + ) { + return executablePaths.has(path); + }), + basename: Effect.fn("RigUpdaterPlatformService.basename")(function* (path: string) { + return path.split("/").at(-1) ?? ""; + }), + dirname: Effect.fn("RigUpdaterPlatformService.dirname")(function* (path: string) { + return path.slice(0, Math.max(0, path.lastIndexOf("/"))) || "/"; + }), + join: Effect.fn("RigUpdaterPlatformService.join")(function* (parts: string[]) { + return parts.join("/").replaceAll("//", "/"); + }), + resolve: Effect.fn("RigUpdaterPlatformService.resolve")(function* (path: string) { + return path; + }), + readPackageVersion: Effect.fn("RigUpdaterPlatformService.readPackageVersion")( + function* () { + return params.currentVersion ?? "0.0.41"; + }, + ), + }), + Layer.succeed(RigUpdateCommandRunnerService, { + read: Effect.fn("RigUpdateCommandRunnerService.read")(function* (step) { + return yield* Effect.sync(() => { + reads.push(step); + return step.args.includes("--version") ? versionOutput : latestOutput; + }); + }), + run: Effect.fn("RigUpdateCommandRunnerService.run")(function* (step) { + return yield* Effect.sync(() => { + runs.push(step); + return runOutput; + }); + }), + }), + ), + ), ); + const run = ( + operation: (service: RigUpdaterService["Service"]) => Effect.Effect, + ) => + Effect.runPromise( + RigUpdaterService.use(operation).pipe( + Effect.provide(updaterLayer), + Effect.mapError((error) => error.cause), + ), + ); + return { + getCurrentVersion: () => params.currentVersion ?? "0.0.41", + plan: () => run((service) => service.plan), + update: (options: { plan?: RigUpdatePlan } = {}) => + run((service) => service.update(options.plan)), + sync: (options: { plan: Exclude }) => + run((service) => service.sync(options.plan)), + }; } } -describe("RigUpdateCommandRunnerClass", () => { - const Runner = new RigUpdateCommandRunnerClass({}, { spawn, env: { RIG_TEST: "inherited" } }); +describe("RigUpdateCommandRunner", () => { + const runnerLayer = rigUpdateCommandRunnerLayer; + const run = (step: RigUpdateStep) => + Effect.runPromise( + RigUpdateCommandRunnerService.use((service) => service.run(step)).pipe( + Effect.provide(runnerLayer), + Effect.mapError((error) => error.cause), + ), + ); it("captures stdout and stderr for run and read operations", async () => { const script = 'process.stdout.write(process.env.RIG_TEST ?? ""); process.stderr.write(process.env.EXTRA ?? "")'; - const step = { command: process.execPath, args: ["-e", script], env: { EXTRA: " added" } }; - - expect(await Runner.run(step)).toBe("inherited added"); - expect(await Runner.read(step)).toBe("inherited added"); + const step = { + command: process.execPath, + args: ["-e", script], + env: { RIG_TEST: "inherited", EXTRA: " added" }, + }; + + expect(await run(step)).toBe("inherited added"); + expect( + await Effect.runPromise( + RigUpdateCommandRunnerService.use((service) => service.read(step)).pipe( + Effect.provide(runnerLayer), + Effect.mapError((error) => error.cause), + ), + ), + ).toBe("inherited added"); }); it("reports command output when a process exits unsuccessfully", async () => { await expect( - Runner.run({ + run({ command: process.execPath, args: ["-e", 'process.stderr.write("registry failed"); process.exit(2)'], }), @@ -69,42 +163,66 @@ describe("RigUpdateCommandRunnerClass", () => { }); it("reports a terminating signal without empty output", async () => { - await expect(Runner.run({ command: "/bin/sh", args: ["-c", "kill -TERM $$"] })).rejects.toThrow( + await expect(run({ command: "/bin/sh", args: ["-c", "kill -TERM $$"] })).rejects.toThrow( "/bin/sh exited with SIGTERM.", ); }); it("forwards process spawn errors", async () => { - await expect( - Runner.run({ command: "/path/that/does/not/exist", args: [] }), - ).rejects.toMatchObject({ code: "ENOENT" }); + await expect(run({ command: "/path/that/does/not/exist", args: [] })).rejects.toMatchObject({ + code: "ENOENT", + }); }); it("reports an unknown exit when a process has no code or signal", async () => { const child = Object.assign(new EventEmitter(), { stdout: new PassThrough(), stderr: new PassThrough(), + kill: () => true, + }) as unknown as RigShellChildProcess; + const shellPlatformLayer = Layer.succeed(RigShellPlatformService, { + environment: Effect.succeed({}), + currentDirectory: Effect.succeed("/tmp"), + detached: Effect.succeed(false), + spawn: Effect.fn("RigShellPlatformService.spawn")(function* () { + queueMicrotask(() => child.emit("close", null, null)); + return child; + }), }); - const spawnWithoutExitStatus = function spawnWithoutExitStatus() { - queueMicrotask(function finishWithoutStatus() { - child.emit("exit", null, null); - }); - return child; - } as unknown as typeof spawn; - const UnknownExitRunner = new RigUpdateCommandRunnerClass( - {}, - { spawn: spawnWithoutExitStatus, env: {} }, + const managedLayer = ManagedProcessService.layer.pipe( + Layer.provide(ManagedProcessConfigService.layer), ); - - await expect(UnknownExitRunner.run({ command: "mystery", args: [] })).rejects.toThrow( - "mystery exited with code unknown.", + const processLayer = ShellProcessService.layer.pipe( + Layer.provide( + Layer.mergeAll( + RigShellConfigService.layer, + shellPlatformLayer, + managedLayer, + ShellOutputService.layer, + ), + ), + ); + const shellLayer = RigShellService.layer.pipe( + Layer.provide(Layer.merge(ShellTemplateService.layer, processLayer)), ); + const unknownExitLayer = RigUpdateCommandRunnerService.layer.pipe(Layer.provide(shellLayer)); + + await expect( + Effect.runPromise( + RigUpdateCommandRunnerService.use((service) => + service.run({ command: "mystery", args: [] }), + ).pipe( + Effect.provide(unknownExitLayer), + Effect.mapError((error) => error.cause), + ), + ), + ).rejects.toThrow("mystery exited with code unknown."); }); }); -describe("RigUpdaterClass", () => { +describe("RigUpdater", () => { it("plans, installs, verifies, and synchronizes an npm update", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); const Updater = Context.create(); expect(Updater.getCurrentVersion()).toBe("0.0.41"); @@ -147,7 +265,7 @@ describe("RigUpdaterClass", () => { }); it("returns a current plan and can resolve it through update", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); Context.latestOutput = '"0.0.41"'; const Updater = Context.create(); @@ -164,7 +282,7 @@ describe("RigUpdaterClass", () => { }); it("supports Bun global installations and plain registry output", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); Context.executablePaths.clear(); Context.executablePaths.add(bunExecutable); Context.executablePaths.add(bunRigExecutable); @@ -187,7 +305,7 @@ describe("RigUpdaterClass", () => { }); it("skips unsupported package layouts", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); await expect(Context.create({ packageRoot: "/checkout/rig" }).plan()).resolves.toEqual({ status: "skipped", @@ -202,14 +320,14 @@ describe("RigUpdaterClass", () => { }); it("skips installations with missing package manager or Rig executables", async () => { - const MissingPackageManager = new RigUpdaterTestContextClass(); + const MissingPackageManager = new RigUpdaterTestContext(); MissingPackageManager.executablePaths.delete(npmExecutable); await expect(MissingPackageManager.create().plan()).resolves.toEqual({ status: "skipped", reason: `The package manager for this Rig installation was not found at ${npmExecutable}.`, }); - const MissingRig = new RigUpdaterTestContextClass(); + const MissingRig = new RigUpdaterTestContext(); MissingRig.executablePaths.delete(rigExecutable); await expect(MissingRig.create().plan()).resolves.toEqual({ status: "skipped", @@ -220,7 +338,7 @@ describe("RigUpdaterClass", () => { it("rejects empty, non-string, and malformed latest versions", async () => { await Promise.all( ['""', "42", "not-a-version"].map(async function rejectOutput(output) { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); Context.latestOutput = output; await expect(Context.create().plan()).rejects.toThrow( "The package manager returned an invalid latest Rig version.", @@ -230,7 +348,7 @@ describe("RigUpdaterClass", () => { }); it("accepts prerelease versions", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); Context.latestOutput = '"0.1.0-beta.1"'; await expect(Context.create().plan()).resolves.toMatchObject({ @@ -242,7 +360,7 @@ describe("RigUpdaterClass", () => { it("rejects a mismatched or missing installed version", async () => { await Promise.all( ["0.0.43\n", "\n"].map(async function rejectVersion(versionOutput) { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); Context.versionOutput = versionOutput; const Updater = Context.create(); const plan = await Updater.plan(); @@ -255,58 +373,10 @@ describe("RigUpdaterClass", () => { }); it("returns a supplied skipped plan without running commands", async () => { - const Context = new RigUpdaterTestContextClass(); + const Context = new RigUpdaterTestContext(); const plan: RigUpdatePlan = { status: "skipped", reason: "Development checkout." }; await expect(Context.create().update({ plan })).resolves.toBe(plan); expect(Context.runs).toEqual([]); }); }); - -describe("RigUpdaterFactoryClass", () => { - it("creates an updater using its process dependencies", async () => { - const calls: { command: string; args: readonly string[] }[] = []; - const spawnCommand = function spawnCommand( - command: string, - args: readonly string[], - options: SpawnOptions, - ) { - calls.push({ command, args }); - const output = args.includes("--version") - ? "0.0.42" - : args.includes("install") - ? "installed" - : '"0.0.42"'; - return spawn( - process.execPath, - ["-e", `process.stdout.write(${JSON.stringify(output)})`], - options, - ); - } as typeof spawn; - const Factory = new RigUpdaterFactoryClass( - {}, - { - spawn: spawnCommand, - env: {}, - executableExists(path) { - return path === npmExecutable || path === rigExecutable; - }, - }, - ); - const Updater = Factory.create({ packageRoot: npmRoot, currentVersion: "0.0.41" }); - - const plan = await Updater.plan(); - await expect(Updater.update({ plan })).resolves.toMatchObject({ status: "updated" }); - expect(calls).toEqual([ - { - command: npmExecutable, - args: ["view", "@rendotdev/rig@latest", "version", "--json"], - }, - { - command: npmExecutable, - args: ["install", "--global", "--prefix", "/runtime", "@rendotdev/rig@0.0.42"], - }, - { command: rigExecutable, args: ["--version"] }, - ]); - }); -}); diff --git a/test/e2e/cli-lifecycle.e2e.test.ts b/test/e2e/cli-lifecycle.e2e.test.ts index e7042b1..34753c1 100644 --- a/test/e2e/cli-lifecycle.e2e.test.ts +++ b/test/e2e/cli-lifecycle.e2e.test.ts @@ -1,7 +1,7 @@ import { stat } from "node:fs/promises"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, test } from "vite-plus/test"; -import { RigE2EHarnessClass, rigE2EHarnessFactory } from "./harness"; +import { createRigE2EHarness, RigE2EHarness } from "./harness"; type SuccessEnvelope = { data: T; @@ -92,10 +92,10 @@ export default tool; `; describe("built Rig CLI lifecycle", () => { - let rig: RigE2EHarnessClass; + let rig: RigE2EHarness; beforeEach(async () => { - rig = await rigE2EHarnessFactory.create(); + rig = await createRigE2EHarness(); }); afterEach(async () => { diff --git a/test/e2e/distribution-recovery.e2e.test.ts b/test/e2e/distribution-recovery.e2e.test.ts index 66b1730..7ba5af5 100644 --- a/test/e2e/distribution-recovery.e2e.test.ts +++ b/test/e2e/distribution-recovery.e2e.test.ts @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { afterAll, afterEach, beforeAll, describe, expect, test } from "vite-plus/test"; -import { RigE2EHarnessClass, RigE2EHarnessFactoryClass } from "./harness"; +import { RigE2EHarness, RigE2EHarnessFactory } from "./harness"; type SpawnResult = { exitCode: number; @@ -20,7 +20,7 @@ type CronJob = { input?: unknown; }; -class NpmPackResultClass { +class NpmPackResult { constructor(private readonly params: { stdout: string }) {} public filename(): string { @@ -30,19 +30,19 @@ class NpmPackResultClass { : typeof parsed === "object" && parsed !== null ? Object.values(parsed)[0] : undefined; - if ( + const hasInvalidFilename = typeof candidate !== "object" || candidate === null || !("filename" in candidate) || - typeof candidate.filename !== "string" - ) { + typeof candidate.filename !== "string"; + if (hasInvalidFilename) { throw new Error(`npm pack returned no package filename: ${this.params.stdout}`); } return candidate.filename; } } -class ProcessRunnerClass { +class ProcessRunner { public async run(params: { command: string[]; cwd: string; @@ -81,13 +81,13 @@ class ProcessRunnerClass { } } -class InstalledDistributionClass { +class InstalledDistribution { public rootDir = ""; public consumerDir = ""; public entrypoint = ""; public binaryPath = ""; public tarballPath = ""; - private readonly processes = new ProcessRunnerClass(); + private readonly processes = new ProcessRunner(); public async setup(): Promise { this.rootDir = await mkdtemp(join(tmpdir(), "rig-distribution-e2e-")); @@ -104,10 +104,7 @@ class InstalledDistributionClass { cwd: repositoryRoot, }); if (packed.exitCode !== 0) throw new Error(packed.stderr || packed.stdout); - this.tarballPath = join( - this.rootDir, - new NpmPackResultClass({ stdout: packed.stdout }).filename(), - ); + this.tarballPath = join(this.rootDir, new NpmPackResult({ stdout: packed.stdout }).filename()); const installed = await this.processes.run({ command: ["npm", "install", "--no-audit", "--no-fund", this.tarballPath], @@ -133,8 +130,8 @@ class InstalledDistributionClass { } } - public harnessFactory(): RigE2EHarnessFactoryClass { - return new RigE2EHarnessFactoryClass({ cliPath: this.binaryPath, runtime: "direct" }); + public harnessFactory(): RigE2EHarnessFactory { + return new RigE2EHarnessFactory({ cliPath: this.binaryPath, runtime: "direct" }); } public async cleanup(): Promise { @@ -142,7 +139,7 @@ class InstalledDistributionClass { } } -class DistributionFixtureClass { +class DistributionFixture { public readonly cronEntrypoint = resolve( testDirectory, "fixtures", @@ -166,14 +163,14 @@ const repositoryRoot = resolve(testDirectory, "..", ".."); const expectedVersion = ( JSON.parse(await readFile(join(repositoryRoot, "package.json"), "utf8")) as { version: string } ).version; -const distribution = new InstalledDistributionClass(); -const fixture = new DistributionFixtureClass(); -const processes = new ProcessRunnerClass(); -const homes: RigE2EHarnessClass[] = []; +const distribution = new InstalledDistribution(); +const fixture = new DistributionFixture(); +const processes = new ProcessRunner(); +const homes: RigE2EHarness[] = []; -async function createHarness(params: { cron?: boolean } = {}): Promise { +async function createHarness(params: { cron?: boolean } = {}): Promise { const factory = params.cron - ? new RigE2EHarnessFactoryClass({ cliPath: fixture.cronEntrypoint, runtime: "bun" }) + ? new RigE2EHarnessFactory({ cliPath: fixture.cronEntrypoint, runtime: "bun" }) : distribution.harnessFactory(); const harness = await factory.create(); homes.push(harness); @@ -201,17 +198,15 @@ describe("installed Rig distribution and recovery", () => { }); test("accepts npm 11 and npm 12 packed-artifact metadata", () => { - expect(new NpmPackResultClass({ stdout: '[{"filename":"npm-11.tgz"}]' }).filename()).toBe( + expect(new NpmPackResult({ stdout: '[{"filename":"npm-11.tgz"}]' }).filename()).toBe( "npm-11.tgz", ); expect( - new NpmPackResultClass({ + new NpmPackResult({ stdout: '{"@rendotdev/rig":{"filename":"npm-12.tgz"}}', }).filename(), ).toBe("npm-12.tgz"); - expect(() => new NpmPackResultClass({ stdout: "{}" }).filename()).toThrow( - "no package filename", - ); + expect(() => new NpmPackResult({ stdout: "{}" }).filename()).toThrow("no package filename"); }); test("packs and installs a consumer-ready CLI with generated runtime support", async () => { @@ -288,20 +283,12 @@ describe("installed Rig distribution and recovery", () => { } }); - test("exports the supported built compatibility surface", async () => { + test("keeps the built CLI free of legacy compatibility exports", async () => { const installed = (await import( `${pathToFileURL(distribution.entrypoint).href}?e2e=exports` )) as Record; - for (const name of [ - "CliApplicationClass", - "CliApplication", - "BunRuntimeBootstrapClass", - "BunRuntimeBootstrap", - "RigCronWorkerClass", - "RigCronWorker", - "isCliEntrypoint", - ]) { - expect(installed[name], `missing built export ${name}`).toBeDefined(); + for (const name of ["CliApplicationClass", "RigCronWorkerClass", "BunRuntimeBootstrapClass"]) { + expect(installed[name], `unexpected built export ${name}`).toBeUndefined(); } }); @@ -416,7 +403,7 @@ describe("installed Rig distribution and recovery", () => { env: { ...baseEnv, RIG_FAKE_CRON_FAIL_REGISTER_SCHEDULE: "@weekly" }, }); expect(replacement.exitCode).toBe(1); - expect(replacement.stderr).toContain("INTERNAL_ERROR: fake register failure: @weekly"); + expect(replacement.stderr).toContain("CRON_ERROR: fake register failure: @weekly"); expect(await rig.read(workerPath)).toBe(originalWorker); let config = JSON.parse(await rig.read(join(rig.rigHomeDir, "rig", "rig.json"))) as { cronJobs: CronJob[]; diff --git a/test/e2e/fixtures/distribution/fake-cron-preload.ts b/test/e2e/fixtures/distribution/fake-cron-preload.ts index 35e526e..371a0cc 100644 --- a/test/e2e/fixtures/distribution/fake-cron-preload.ts +++ b/test/e2e/fixtures/distribution/fake-cron-preload.ts @@ -8,7 +8,7 @@ type CronFunction = { remove(title: string): Promise; }; -class FakeCronLogClass { +class FakeCronLog { public constructor(private readonly path: string | undefined) {} public async append(params: Record): Promise { @@ -18,7 +18,7 @@ class FakeCronLogClass { } } -const log = new FakeCronLogClass(process.env.RIG_FAKE_CRON_LOG); +const log = new FakeCronLog(process.env.RIG_FAKE_CRON_LOG); const cron = (async (path: string, schedule: string, title: string) => { await log.append({ operation: "register", path, schedule, title }); if (schedule === process.env.RIG_FAKE_CRON_FAIL_REGISTER_SCHEDULE) { @@ -38,11 +38,5 @@ cron.remove = async (title: string) => { const entrypoint = process.env.RIG_DISTRIBUTION_ENTRY; if (!entrypoint) throw new Error("RIG_DISTRIBUTION_ENTRY is required."); -const module = (await import(pathToFileURL(entrypoint).href)) as { - CliApplicationClass: new () => { run(argv: string[]): Promise }; -}; -await new module.CliApplicationClass().run([ - process.execPath, - entrypoint, - ...process.argv.slice(2), -]); +process.argv.splice(1, 1, entrypoint); +await import(pathToFileURL(entrypoint).href); diff --git a/test/e2e/fixtures/stateful/index.rig.ts b/test/e2e/fixtures/stateful/index.rig.ts index c021317..81797f4 100644 --- a/test/e2e/fixtures/stateful/index.rig.ts +++ b/test/e2e/fixtures/stateful/index.rig.ts @@ -1,283 +1,257 @@ -type StatefulToolParams = { - name: string; -}; - -type StatefulToolDeps = { - rig: Parameters[0]; -}; - let definitionEvaluations = 0; -class StatefulToolClass { - public readonly name: string; - private readonly rig: Parameters[0]; +function defineStatefulTool(rig: Parameters[0]) { + const anyOutput = rig.z.unknown(); + const emptyInput = rig.z.object({}); + const cacheInput = rig.z.object({ + action: rig.z.enum(["query", "peek", "set", "invalidate", "remove", "clear"]), + key: rig.z.string().default("default"), + value: rig.z.string().optional(), + staleTime: rig.z.coerce.number().nonnegative().optional(), + delayMs: rig.z.coerce.number().nonnegative().default(0), + }); + const collectionInput = rig.z.object({ + collection: rig.z.enum(["notes", "archive"]).default("notes"), + action: rig.z.enum([ + "create", + "get", + "update", + "upsert", + "remove", + "list", + "count", + "search", + "clear", + ]), + id: rig.z.string().optional(), + title: rig.z.string().optional(), + status: rig.z.enum(["open", "done"]).optional(), + priority: rig.z.coerce.number().optional(), + body: rig.z.string().optional(), + query: rig.z.string().optional(), + whereStatus: rig.z.enum(["open", "done"]).optional(), + sort: rig.z.string().optional(), + }); - constructor(params: StatefulToolParams, deps: StatefulToolDeps) { - this.name = params.name; - this.rig = deps.rig; - } - - public define() { - const { rig } = this; - const anyOutput = rig.z.unknown(); - const emptyInput = rig.z.object({}); - const cacheInput = rig.z.object({ - action: rig.z.enum(["query", "peek", "set", "invalidate", "remove", "clear"]), - key: rig.z.string().default("default"), - value: rig.z.string().optional(), - staleTime: rig.z.coerce.number().nonnegative().optional(), - delayMs: rig.z.coerce.number().nonnegative().default(0), - }); - const collectionInput = rig.z.object({ - collection: rig.z.enum(["notes", "archive"]).default("notes"), - action: rig.z.enum([ - "create", - "get", - "update", - "upsert", - "remove", - "list", - "count", - "search", - "clear", - ]), - id: rig.z.string().optional(), - title: rig.z.string().optional(), - status: rig.z.enum(["open", "done"]).optional(), - priority: rig.z.coerce.number().optional(), - body: rig.z.string().optional(), - query: rig.z.string().optional(), - whereStatus: rig.z.enum(["open", "done"]).optional(), - sort: rig.z.string().optional(), - }); - - return rig.defineTool({ - name: this.name, - description: "Exercise stateful Rig capabilities end to end.", - env: rig.z.object({ - TOKEN: rig.z.string().min(3), - }), - setupDb: (db) => { - db.migrate( - 1, - "create counters", - ` + return rig.defineTool({ + name: "stateful", + description: "Exercise stateful Rig capabilities end to end.", + env: rig.z.object({ + TOKEN: rig.z.string().min(3), + }), + setupDb: (db) => { + db.migrate( + 1, + "create counters", + ` create table counters ( name text primary key, value integer not null ); `, - ); + ); + }, + collections: { + notes: { + schema: rig.z.object({ + title: rig.z.string(), + status: rig.z.enum(["open", "done"]), + project: rig.z.object({ priority: rig.z.number() }), + }), }, - collections: { - notes: { - schema: rig.z.object({ - title: rig.z.string(), - status: rig.z.enum(["open", "done"]), - project: rig.z.object({ priority: rig.z.number() }), - }), - }, - archive: { - schema: rig.z.object({ - title: rig.z.string(), - status: rig.z.enum(["open", "done"]), - project: rig.z.object({ priority: rig.z.number() }), - }), - }, + archive: { + schema: rig.z.object({ + title: rig.z.string(), + status: rig.z.enum(["open", "done"]), + project: rig.z.object({ priority: rig.z.number() }), + }), }, - commands: { - env: rig.defineCommand({ - description: "Read tool-local and process environment values.", - input: emptyInput, - output: rig.z.object({ token: rig.z.string(), processToken: rig.z.string().optional() }), - run: (context) => ({ - token: (context.env as { TOKEN: string }).TOKEN, - processToken: context.processEnv.TOKEN, - }), + }, + commands: { + env: rig.defineCommand({ + description: "Read tool-local and process environment values.", + input: emptyInput, + output: rig.z.object({ token: rig.z.string(), processToken: rig.z.string().optional() }), + run: (context) => ({ + token: (context.env as { TOKEN: string }).TOKEN, + processToken: context.processEnv.TOKEN, }), - db: rig.defineCommand({ - description: "Increment and read a persistent database counter.", - input: rig.z.object({ name: rig.z.string().default("runs") }), - output: rig.z.object({ value: rig.z.number() }), - run: (context) => { - context.db - .query( - `insert into counters (name, value) values ($name, 1) + }), + db: rig.defineCommand({ + description: "Increment and read a persistent database counter.", + input: rig.z.object({ name: rig.z.string().default("runs") }), + output: rig.z.object({ value: rig.z.number() }), + run: (context) => { + context.db + .query( + `insert into counters (name, value) values ($name, 1) on conflict(name) do update set value = value + 1`, - ) - .run({ name: context.input.name }); - const row = context.db - .query("select value from counters where name = ?") - .get(context.input.name) as { value: number }; - return row; - }, - }), - kv: rig.defineCommand({ - description: "Read or write persistent key-value state.", - input: rig.z.object({ - action: rig.z.enum(["get", "set"]), - key: rig.z.string(), - value: rig.z.unknown().optional(), - }), - output: anyOutput, - run: (context) => { - if (context.input.action === "set") { - context.kv.set(context.input.key, context.input.value); - } - return { value: context.kv.get(context.input.key) }; - }, + ) + .run({ name: context.input.name }); + const row = context.db + .query("select value from counters where name = ?") + .get(context.input.name) as { value: number }; + return row; + }, + }), + kv: rig.defineCommand({ + description: "Read or write persistent key-value state.", + input: rig.z.object({ + action: rig.z.enum(["get", "set"]), + key: rig.z.string(), + value: rig.z.unknown().optional(), }), - cache: rig.defineCommand({ - description: "Exercise persistent cache state and refresh behavior.", - input: cacheInput, - output: anyOutput, - run: async (context) => { - const key = ["e2e", context.input.key] as const; - if (context.input.action === "peek") return { value: context.cache.peek(key) }; - if (context.input.action === "set") { - context.cache.set(key, context.input.value); - return { value: context.cache.peek(key) }; - } - if (context.input.action === "invalidate") context.cache.invalidate(key); - if (context.input.action === "remove") context.cache.remove(key); - if (context.input.action === "clear") context.cache.clear(); - if (context.input.action !== "query") return { value: context.cache.peek(key) }; + output: anyOutput, + run: (context) => { + if (context.input.action === "set") { + context.kv.set(context.input.key, context.input.value); + } + return { value: context.kv.get(context.input.key) }; + }, + }), + cache: rig.defineCommand({ + description: "Exercise persistent cache state and refresh behavior.", + input: cacheInput, + output: anyOutput, + run: async (context) => { + const key = ["e2e", context.input.key] as const; + if (context.input.action === "peek") return { value: context.cache.peek(key) }; + if (context.input.action === "set") { + context.cache.set(key, context.input.value); + return { value: context.cache.peek(key) }; + } + if (context.input.action === "invalidate") context.cache.invalidate(key); + if (context.input.action === "remove") context.cache.remove(key); + if (context.input.action === "clear") context.cache.clear(); + if (context.input.action !== "query") return { value: context.cache.peek(key) }; - const startedAt = Date.now(); - const value = await context.cache.query({ - queryKey: key, - staleTime: context.input.staleTime, - queryFn: async () => { - if (context.input.delayMs > 0) { - await new Promise((resolve) => setTimeout(resolve, context.input.delayMs)); - } - return context.input.value ?? "refreshed"; - }, + const startedAt = Date.now(); + const value = await context.cache.query({ + queryKey: key, + staleTime: context.input.staleTime, + queryFn: async () => { + if (context.input.delayMs > 0) { + await new Promise((resolve) => setTimeout(resolve, context.input.delayMs)); + } + return context.input.value ?? "refreshed"; + }, + }); + return { value, elapsedMs: Date.now() - startedAt }; + }, + }), + collection: rig.defineCommand({ + description: "Exercise Markdown collection operations.", + input: collectionInput, + output: anyOutput, + run: async (context) => { + const collection = context.collections[context.input.collection]; + const data = { + title: context.input.title ?? context.input.id ?? "Untitled", + status: context.input.status ?? "open", + project: { priority: context.input.priority ?? 0 }, + }; + if (context.input.action === "create") { + return collection.create({ id: context.input.id, data, body: context.input.body }); + } + if (context.input.action === "get") return collection.getEntry(context.input.id!); + if (context.input.action === "update") { + return collection.update(context.input.id!, { data, body: context.input.body }); + } + if (context.input.action === "upsert") { + return collection.upsert({ id: context.input.id!, data, body: context.input.body }); + } + if (context.input.action === "remove") return collection.remove(context.input.id!); + if (context.input.action === "list") { + return collection.list({ + where: context.input.whereStatus ? { status: context.input.whereStatus } : undefined, + sort: context.input.sort, }); - return { value, elapsedMs: Date.now() - startedAt }; - }, - }), - collection: rig.defineCommand({ - description: "Exercise Markdown collection operations.", - input: collectionInput, - output: anyOutput, - run: async (context) => { - const collection = context.collections[context.input.collection]; - const data = { - title: context.input.title ?? context.input.id ?? "Untitled", - status: context.input.status ?? "open", - project: { priority: context.input.priority ?? 0 }, + } + if (context.input.action === "count") { + return { + count: await collection.count( + context.input.whereStatus ? { status: context.input.whereStatus } : undefined, + ), }; - if (context.input.action === "create") { - return collection.create({ id: context.input.id, data, body: context.input.body }); - } - if (context.input.action === "get") return collection.getEntry(context.input.id!); - if (context.input.action === "update") { - return collection.update(context.input.id!, { data, body: context.input.body }); - } - if (context.input.action === "upsert") { - return collection.upsert({ id: context.input.id!, data, body: context.input.body }); - } - if (context.input.action === "remove") return collection.remove(context.input.id!); - if (context.input.action === "list") { - return collection.list({ - where: context.input.whereStatus - ? { status: context.input.whereStatus } - : undefined, - sort: context.input.sort, - }); - } - if (context.input.action === "count") { - return { - count: await collection.count( - context.input.whereStatus ? { status: context.input.whereStatus } : undefined, - ), - }; - } - if (context.input.action === "search") { - return collection.search(context.input.query ?? ""); - } - await collection.clear(); - return { count: await collection.count() }; - }, + } + if (context.input.action === "search") { + return collection.search(context.input.query ?? ""); + } + await collection.clear(); + return { count: await collection.count() }; + }, + }), + shell: rig.defineCommand({ + description: "Exercise exec, JSON, Bash, timeout, and bounded output.", + input: rig.z.object({ + action: rig.z.enum(["exec", "json", "bash", "timeout", "bounded"]), }), - shell: rig.defineCommand({ - description: "Exercise exec, JSON, Bash, timeout, and bounded output.", - input: rig.z.object({ - action: rig.z.enum(["exec", "json", "bash", "timeout", "bounded"]), - }), - output: anyOutput, - run: async (context) => { - if (context.input.action === "exec") { - return context.rig.shell.exec(["bun", "-e", "console.log('exec-ok')"]); - } - if (context.input.action === "json") { - return context.rig.shell.json([ - "bun", - "-e", - "console.log(JSON.stringify({ok:true}))", - ]); - } - if (context.input.action === "bash") { - return context.rig.shell.bash("printf bash-ok"); - } - if (context.input.action === "bounded") { - return context.rig.shell.exec( - ["bun", "-e", "process.stdout.write('🙂'.repeat(200))"], - { maxOutputBytes: 17 }, - ); - } - return context.rig.shell.exec(["bun", "-e", "setTimeout(() => {}, 10000)"], { - timeoutMs: 50, + output: anyOutput, + run: async (context) => { + if (context.input.action === "exec") { + return context.rig.shell.exec(["bun", "-e", "console.log('exec-ok')"]); + } + if (context.input.action === "json") { + return context.rig.shell.json(["bun", "-e", "console.log(JSON.stringify({ok:true}))"]); + } + if (context.input.action === "bash") { + return context.rig.shell.bash("printf bash-ok"); + } + if (context.input.action === "bounded") { + return context.rig.shell.exec(["bun", "-e", "process.stdout.write('🙂'.repeat(200))"], { + maxOutputBytes: 17, }); - }, - }), - child: rig.defineCommand({ - description: "Return a nested-call value or fail.", - input: rig.z.object({ value: rig.z.number(), fail: rig.z.boolean().default(false) }), - output: rig.z.object({ value: rig.z.number(), definitionEvaluations: rig.z.number() }), - run: (context) => { - if (context.input.fail) throw new Error("nested child failed"); - return { value: context.input.value + 1, definitionEvaluations }; - }, + } + return context.rig.shell.exec(["bun", "-e", "setTimeout(() => {}, 10000)"], { + timeoutMs: 50, + }); + }, + }), + child: rig.defineCommand({ + description: "Return a nested-call value or fail.", + input: rig.z.object({ value: rig.z.number(), fail: rig.z.boolean().default(false) }), + output: rig.z.object({ value: rig.z.number(), definitionEvaluations: rig.z.number() }), + run: (context) => { + if (context.input.fail) throw new Error("nested child failed"); + return { value: context.input.value + 1, definitionEvaluations }; + }, + }), + nested: rig.defineCommand({ + description: "Run repeated nested commands in one invocation session.", + input: rig.z.object({ + count: rig.z.coerce.number().int().positive(), + fail: rig.z.boolean().default(false), }), - nested: rig.defineCommand({ - description: "Run repeated nested commands in one invocation session.", - input: rig.z.object({ - count: rig.z.coerce.number().int().positive(), - fail: rig.z.boolean().default(false), - }), - output: rig.z.object({ - values: rig.z.array(rig.z.number()), - definitionEvaluations: rig.z.array(rig.z.number()), - }), - run: async (context) => { - const values: number[] = []; - const evaluations: number[] = []; - for (let index = 0; index < context.input.count; index++) { - // Sequential execution proves every nested call shares the outer invocation session. - // eslint-disable-next-line no-await-in-loop - const result = await context.rig.run<{ - value: number; - definitionEvaluations: number; - }>({ - command: "stateful.child", - input: { value: index, fail: context.input.fail && index === 0 }, - }); - values.push(result.value); - evaluations.push(result.definitionEvaluations); - } - return { values, definitionEvaluations: evaluations }; - }, + output: rig.z.object({ + values: rig.z.array(rig.z.number()), + definitionEvaluations: rig.z.array(rig.z.number()), }), - }, - }); - } + run: async (context) => { + const values: number[] = []; + const evaluations: number[] = []; + for (let index = 0; index < context.input.count; index++) { + // Sequential execution proves every nested call shares the outer invocation session. + // eslint-disable-next-line no-await-in-loop + const result = await context.rig.run<{ + value: number; + definitionEvaluations: number; + }>({ + command: "stateful.child", + input: { value: index, fail: context.input.fail && index === 0 }, + }); + values.push(result.value); + evaluations.push(result.definitionEvaluations); + } + return { values, definitionEvaluations: evaluations }; + }, + }), + }, + }); } const tool: RigToolFactory = (rig) => { definitionEvaluations++; - return new StatefulToolClass({ name: "stateful" }, { rig }).define(); + return defineStatefulTool(rig); }; export default tool; diff --git a/test/e2e/fixtures/stateful/noop/index.rig.ts b/test/e2e/fixtures/stateful/noop/index.rig.ts index 94da4b9..58fd154 100644 --- a/test/e2e/fixtures/stateful/noop/index.rig.ts +++ b/test/e2e/fixtures/stateful/noop/index.rig.ts @@ -1,31 +1,18 @@ -type NoopToolDeps = { - rig: Parameters[0]; -}; - -class NoopToolClass { - private readonly rig: Parameters[0]; - - constructor(params: Record, deps: NoopToolDeps) { - void params; - this.rig = deps.rig; - } - - public define() { - return this.rig.defineTool({ - name: "noop", - description: "Prove lazy state stores remain unopened.", - commands: { - run: this.rig.defineCommand({ - description: "Return without touching stateful services.", - input: this.rig.z.object({}), - output: this.rig.z.object({ ok: this.rig.z.literal(true) }), - run: () => ({ ok: true as const }), - }), - }, - }); - } +function defineNoopTool(rig: Parameters[0]) { + return rig.defineTool({ + name: "noop", + description: "Prove lazy state stores remain unopened.", + commands: { + run: rig.defineCommand({ + description: "Return without touching stateful services.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.literal(true) }), + run: () => ({ ok: true as const }), + }), + }, + }); } -const tool: RigToolFactory = (rig) => new NoopToolClass({}, { rig }).define(); +const tool: RigToolFactory = defineNoopTool; export default tool; diff --git a/test/e2e/harness.ts b/test/e2e/harness.ts index d2c87a8..9c8f358 100644 --- a/test/e2e/harness.ts +++ b/test/e2e/harness.ts @@ -34,7 +34,7 @@ type BunFileRuntime = { write(path: string, content: string): Promise; }; -class RigE2ECommandResultClass implements RigE2ECommandResult { +class RigE2ECommandResultValue implements RigE2ECommandResult { public readonly command: string[]; public readonly cwd: string; public readonly exitCode: number; @@ -77,7 +77,7 @@ class RigE2ECommandResultClass implements RigE2ECommandResult { } } -export class RigE2EHarnessClass { +export class RigE2EHarness { public readonly rootDir: string; public readonly homeDir: string; public readonly rigHomeDir: string; @@ -153,7 +153,7 @@ export class RigE2EHarnessClass { }, timeoutMs); }), ]); - return new RigE2ECommandResultClass({ + return new RigE2ECommandResultValue({ command, cwd, exitCode, @@ -218,13 +218,13 @@ export class RigE2EHarnessClass { } } -export class RigE2EHarnessFactoryClass { +export class RigE2EHarnessFactory { constructor( private readonly params: { cliPath?: string; runtime?: RigE2ERuntime } = {}, private readonly deps: RigE2EHarnessDeps = { now: Date.now }, ) {} - public async create(): Promise { + public async create(): Promise { const harnessDir = dirname(fileURLToPath(import.meta.url)); const cliPath = resolve(this.params.cliPath ?? join(harnessDir, "..", "..", "dist", "bin.mjs")); if (!existsSync(cliPath)) { @@ -247,8 +247,10 @@ export class RigE2EHarnessFactoryClass { mkdir(paths.rigHomeDir, { recursive: true }), mkdir(paths.projectDir, { recursive: true }), ]); - return new RigE2EHarnessClass(paths, this.deps); + return new RigE2EHarness(paths, this.deps); } } -export const rigE2EHarnessFactory = new RigE2EHarnessFactoryClass(); +export function createRigE2EHarness(): Promise { + return new RigE2EHarnessFactory().create(); +} diff --git a/test/e2e/stateful-tools.e2e.test.ts b/test/e2e/stateful-tools.e2e.test.ts index cd8e471..1d17566 100644 --- a/test/e2e/stateful-tools.e2e.test.ts +++ b/test/e2e/stateful-tools.e2e.test.ts @@ -3,7 +3,7 @@ import { readFile, readdir, unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { afterEach, beforeEach, describe, expect, test } from "vite-plus/test"; -import { RigE2EHarnessClass, rigE2EHarnessFactory } from "./harness"; +import { createRigE2EHarness, RigE2EHarness } from "./harness"; type SuccessEnvelope = { data: T; @@ -15,13 +15,13 @@ type ErrorEnvelope = { errors: Array<{ code: string; message: string; details?: unknown }>; }; -class StatefulE2EFixtureClass { +class StatefulE2EFixture { public readonly toolDir: string; private readonly fixtureDir = fileURLToPath(new URL("./fixtures/stateful", import.meta.url)); constructor( params: { toolName: string }, - private readonly deps: { rig: RigE2EHarnessClass }, + private readonly deps: { rig: RigE2EHarness }, ) { this.toolDir = join(deps.rig.rigHomeDir, "rig", "tools", params.toolName); } @@ -68,12 +68,12 @@ class StatefulE2EFixtureClass { } describe("built Rig CLI stateful tool capabilities", () => { - let rig: RigE2EHarnessClass; - let fixture: StatefulE2EFixtureClass; + let rig: RigE2EHarness; + let fixture: StatefulE2EFixture; beforeEach(async () => { - rig = await rigE2EHarnessFactory.create(); - fixture = new StatefulE2EFixtureClass({ toolName: "stateful" }, { rig }); + rig = await createRigE2EHarness(); + fixture = new StatefulE2EFixture({ toolName: "stateful" }, { rig }); await fixture.install(); }); @@ -154,7 +154,7 @@ describe("built Rig CLI stateful tool capabilities", () => { await fixture.run("cache", { action: "set", key: "two", value: "two" }); await fixture.run("cache", { action: "clear" }); await expect(fixture.run("cache", { action: "peek", key: "two" })).resolves.toEqual({}); - }); + }, 60_000); test("covers collection CRUD, nested filters, sorting, search, and multiple collections", async () => { await fixture.run("collection", { @@ -226,7 +226,7 @@ describe("built Rig CLI stateful tool capabilities", () => { fixture.run("collection", { collection: "archive", action: "count" }), ).resolves.toEqual({ count: 1 }); await expect(fixture.run("collection", { action: "count" })).resolves.toEqual({ count: 2 }); - }); + }, 60_000); test("reconciles hand edits and deletions when collections reopen", async () => { await fixture.run("collection", { diff --git a/test/fixtures/oxlint-rule-test-helpers.ts b/test/fixtures/oxlint-rule-test-helpers.ts new file mode 100644 index 0000000..67646c7 --- /dev/null +++ b/test/fixtures/oxlint-rule-test-helpers.ts @@ -0,0 +1,32 @@ +import type { Context, Rule } from "@oxlint/plugins"; + +export const createOxlintContext = ( + filename: string, + text: string, + report: (message: unknown) => void, + comments: Array<{ type: string; value: string; range: [number, number] }> = [], +) => + ({ + filename, + report, + sourceCode: { + text, + getAllComments: () => comments, + }, + }) as unknown as Context; + +export const createOxlintVisitor = (rule: Rule, context: Context) => { + const create = rule.create; + if (!create) { + throw new Error("The Oxlint rule must provide a create visitor."); + } + return create(context); +}; + +export const oxlintImportNode = (value: string) => ({ + type: "ImportDeclaration", + source: { type: "Literal", value }, +}); + +export const meaningfulSource = (lines: number) => + Array.from({ length: lines }, (_, index) => `const value${index} = ${index};`).join("\n"); diff --git a/test/fixtures/tool-runner-test-harness.ts b/test/fixtures/tool-runner-test-harness.ts new file mode 100644 index 0000000..62e2224 --- /dev/null +++ b/test/fixtures/tool-runner-test-harness.ts @@ -0,0 +1,405 @@ +import { existsSync, writeFileSync } from "node:fs"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Effect, Exit, Layer, Scope } from "effect"; +import { ToolCacheService, toolCacheLayer } from "../../src/domains/tools/repo/tool-cache"; +import { ToolDatabaseService, toolDatabaseLayer } from "../../src/domains/tools/repo/tool-database"; +import { ToolKvService, toolKvLayer } from "../../src/domains/tools/repo/tool-kv"; +import { + RigPathsOptionsConfigService, + RigPathsPlatformService, + type PathOptions, +} from "../../src/providers/paths/rig-paths"; +import { ToolFilesService } from "../../src/domains/tools/runtime/tool-files"; +import { ToolHelpService } from "../../src/domains/tools/runtime/tool-help"; +import { ToolInspectorService } from "../../src/domains/tools/runtime/tool-inspector"; +import { ToolListService, type ToolListData } from "../../src/domains/tools/runtime/tool-list"; +import { RuntimeSupportService } from "../../src/domains/tools/runtime/runtime-support"; +import { + RunInputService, + ToolRunnerService, + runInputLayer, +} from "../../src/domains/tools/runtime/tool-runner"; +import { toolInputParserLayer } from "../../src/domains/tools/service/tool-input-parser"; +import type { + CommandDefinition, + LoadedTool, + RigToolLogger, +} from "../../src/domains/tools/types/tool-types"; +import type { + RunCommandOptions, + RunCommandResult, +} from "../../src/domains/tools/types/tool-runner"; +import { ToolTypecheckService } from "../../src/domains/tools/runtime/tool-typecheck"; +import { toolServicesLayer } from "../../src/domains/tools/runtime/tool-services-layer"; + +type ToolRunnerClient = { + run: (tool: string, command: string, options?: RunCommandOptions) => Promise; + dispose: () => Promise; +}; + +const serviceLayer = (homeDir?: string) => + toolServicesLayer.pipe( + Layer.provide( + Layer.merge( + Layer.succeed(RigPathsOptionsConfigService, { homeDir }), + RigPathsPlatformService.layer, + ), + ), + ); + +const runToolService = ( + homeDir: string | undefined, + effect: Effect.Effect< + A, + E, + | ToolFilesService + | ToolHelpService + | ToolInspectorService + | ToolListService + | RuntimeSupportService + | ToolRunnerService + | ToolTypecheckService + >, +) => Effect.runPromise(effect.pipe(Effect.provide(serviceLayer(homeDir)))); + +export const createTool = (options: PathOptions, name: string) => + runToolService( + options.homeDir, + ToolFilesService.use((service) => service.create(name)), + ); + +export const renderToolHelp = (options: PathOptions, toolName: string, commandName?: string) => + runToolService( + options.homeDir, + ToolHelpService.use((service) => service.render(toolName, commandName)), + ); + +export const inspectTool = (options: PathOptions, toolName: string, commandName?: string) => + runToolService( + options.homeDir, + ToolInspectorService.use((service) => service.inspect(toolName, commandName)), + ); + +export const typecheckTools = (options: PathOptions, toolName?: string) => + runToolService( + options.homeDir, + Effect.gen(function* () { + yield* RuntimeSupportService.use((service) => service.ensure()); + return yield* ToolTypecheckService.use((service) => service.typecheck(toolName)); + }), + ); + +export const makeToolListClient = (options: PathOptions) => ({ + list: () => + runToolService( + options.homeDir, + ToolListService.use((service) => service.list()), + ), + renderPlain: (data: ToolListData) => + runToolService( + options.homeDir, + ToolListService.use((service) => service.renderPlain(data)), + ), +}); + +export const readRunInput = (command: CommandDefinition, options: RunCommandOptions) => + Effect.runPromise( + RunInputService.use((service) => service.read(command, options)).pipe( + Effect.provide(runInputLayer.pipe(Layer.provide(toolInputParserLayer))), + ), + ); + +const resourceScopes: Scope.Scope[] = []; +const acquireResource = async ( + layer: Layer.Layer, + effect: Effect.Effect, +): Promise => { + const scope = await Effect.runPromise(Scope.make()); + resourceScopes.push(scope); + try { + return await Effect.runPromise( + effect.pipe(Effect.provide(layer), Effect.provideService(Scope.Scope, scope)), + ); + } catch (error) { + await Effect.runPromise(Scope.close(scope, Exit.void)); + throw error; + } +}; + +export const databasePathForToolPath = (toolPath: string) => + Effect.runSync( + ToolDatabaseService.use((service) => service.pathForToolPath(toolPath)).pipe( + Effect.provide(toolDatabaseLayer), + ), + ); +export const setupToolDatabase = (tool: LoadedTool) => + acquireResource( + toolDatabaseLayer, + ToolDatabaseService.use((service) => service.acquire(tool)), + ); +export const kvPathForToolPath = (toolPath: string) => + Effect.runSync( + ToolKvService.use((service) => service.pathForToolPath(toolPath)).pipe( + Effect.provide(toolKvLayer), + ), + ); +export const setupToolKvStore = (tool: LoadedTool) => + acquireResource( + toolKvLayer, + ToolKvService.use((service) => service.acquire(tool)), + ); +export const cachePathForToolPath = (toolPath: string) => + Effect.runSync( + ToolCacheService.use((service) => service.pathForToolPath(toolPath)).pipe( + Effect.provide(toolCacheLayer), + ), + ); +export const setupToolCache = (tool: LoadedTool, log: RigToolLogger) => + acquireResource( + toolCacheLayer, + ToolCacheService.use((service) => service.acquire(tool, log)), + ); + +const testHomes: string[] = []; + +export const homes = { + create: async (): Promise => { + const home = await mkdtemp(join(tmpdir(), "rig-test-home-")); + testHomes.push(home); + return home; + }, + cleanup: async (): Promise => { + new FakeSqliteEnvironment().uninstall(); + await Promise.all( + testHomes.splice(0).map((home) => rm(home, { recursive: true, force: true })), + ); + }, +} as const; + +class FakeSqliteStore { + readonly kv = new Map(); + readonly cache = new Map< + string, + { + keyJson: string; + valueJson: string; + dataUpdatedAt: number; + invalidatedAt: number | null; + lastAccessedAt: number; + } + >(); + readonly migrations = new Map(); + readonly notes: string[] = []; + setupRuns = 0; + closeRuns = 0; +} + +class FakeSqliteStatement { + constructor( + private readonly store: FakeSqliteStore, + private readonly sql: string, + ) {} + + get(params?: unknown): unknown { + if (this.sql.includes("from _rig_migrations")) { + return this.store.migrations.get(Number(params)) ?? null; + } + if (this.sql.includes("from _rig_kv")) { + const valueJson = this.store.kv.get(String(params)); + return valueJson === undefined ? null : { value_json: valueJson }; + } + if (this.sql.includes("from _rig_cache")) { + const row = this.store.cache.get(String(params)); + if (!row) return null; + return { + key_json: row.keyJson, + value_json: row.valueJson, + data_updated_at: row.dataUpdatedAt, + invalidated_at: row.invalidatedAt, + }; + } + if (this.sql.includes("count(*) as count from setup_runs")) { + return { count: this.store.setupRuns }; + } + return null; + } + + run(params?: unknown): { lastInsertRowid: number; changes: number } { + const record = this.recordParams(params); + if (this.sql.includes("into _rig_migrations")) { + this.store.migrations.set(Number(record.version), { + name: String(record.name), + checksum: String(record.checksum), + }); + return { lastInsertRowid: Number(record.version), changes: 1 }; + } + if (this.sql.includes("into setup_runs")) { + this.store.setupRuns++; + return { lastInsertRowid: this.store.setupRuns, changes: 1 }; + } + if (this.sql.includes("into _rig_kv")) { + this.store.kv.set(String(record.key), String(record.valueJson)); + return { lastInsertRowid: this.store.kv.size, changes: 1 }; + } + if (this.sql.includes("into _rig_cache")) { + this.store.cache.set(String(record.keyHash), { + keyJson: String(record.keyJson), + valueJson: String(record.valueJson), + dataUpdatedAt: Number(record.dataUpdatedAt), + invalidatedAt: null, + lastAccessedAt: Number(record.lastAccessedAt), + }); + return { lastInsertRowid: this.store.cache.size, changes: 1 }; + } + if (this.sql.includes("set invalidated_at")) { + const row = this.store.cache.get(String(record.keyHash)); + if (row) row.invalidatedAt = Number(record.invalidatedAt); + return { lastInsertRowid: 0, changes: row ? 1 : 0 }; + } + if (this.sql.includes("set last_accessed_at")) { + const row = this.store.cache.get(String(record.keyHash)); + if (row) row.lastAccessedAt = Number(record.lastAccessedAt); + return { lastInsertRowid: 0, changes: row ? 1 : 0 }; + } + if (this.sql.includes("delete from _rig_cache where")) { + const deleted = this.store.cache.delete(String(params)); + return { lastInsertRowid: 0, changes: deleted ? 1 : 0 }; + } + if (this.sql.includes("into notes")) { + this.store.notes.push(String(record.text)); + return { lastInsertRowid: this.store.notes.length, changes: 1 }; + } + return { lastInsertRowid: 0, changes: 0 }; + } + + private recordParams(params: unknown): Record { + return typeof params === "object" && params !== null && !Array.isArray(params) + ? (params as Record) + : {}; + } +} + +export class FakeSqliteDatabase { + private static readonly stores = new Map(); + private static failNextRunPattern?: string; + private readonly store: FakeSqliteStore; + + constructor(filename: string) { + if (!existsSync(filename)) writeFileSync(filename, ""); + const existing = FakeSqliteDatabase.stores.get(filename); + this.store = existing ?? new FakeSqliteStore(); + FakeSqliteDatabase.stores.set(filename, this.store); + } + + static reset(): void { + this.stores.clear(); + this.failNextRunPattern = undefined; + } + + static failNextRunContaining(pattern: string): void { + this.failNextRunPattern = pattern; + } + + static store(filename: string): FakeSqliteStore | undefined { + return this.stores.get(filename); + } + + query(sql: string): FakeSqliteStatement { + return new FakeSqliteStatement(this.store, sql); + } + + run(sql: string): { lastInsertRowid: number; changes: number } { + const shouldFailInitialization = + FakeSqliteDatabase.failNextRunPattern && sql.includes(FakeSqliteDatabase.failNextRunPattern); + if (shouldFailInitialization) { + FakeSqliteDatabase.failNextRunPattern = undefined; + throw new Error("sqlite initialization failed"); + } + if (sql.includes("delete from _rig_cache")) { + const changes = this.store.cache.size; + this.store.cache.clear(); + return { lastInsertRowid: 0, changes }; + } + return { lastInsertRowid: 0, changes: 0 }; + } + + transaction(callback: () => T): () => T { + return () => callback(); + } + + close(): void { + this.store.closeRuns++; + } +} + +export class FakeSqliteEnvironment { + install(): void { + ( + globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: unknown } + ).rigSqliteDatabaseForTests = FakeSqliteDatabase; + } + + uninstall(): void { + delete (globalThis as typeof globalThis & { rigSqliteDatabaseForTests?: unknown }) + .rigSqliteDatabaseForTests; + FakeSqliteDatabase.reset(); + } + + failNextInitialization(): void { + FakeSqliteDatabase.failNextRunContaining("PRAGMA journal_mode = WAL"); + } +} + +const toolRunners: ToolRunnerClient[] = []; + +export function createToolRunner(options: { homeDir?: string }): ToolRunnerClient { + const client: ToolRunnerClient = { + run: (tool, command, runOptions = {}) => + runToolService( + options.homeDir, + ToolRunnerService.use((service) => service.run(tool, command, runOptions)), + ), + dispose: () => Promise.resolve(), + }; + toolRunners.push(client); + return client; +} + +export const cleanupToolRunnerTests = async () => { + await Promise.all(toolRunners.splice(0).map((runner) => runner.dispose())); + await Promise.all( + resourceScopes.splice(0).map((scope) => Effect.runPromise(Scope.close(scope, Exit.void))), + ); + await homes.cleanup(); +}; + +export class DbSetupTestToolWriter { + constructor(private readonly home: string) {} + + async write(name: string, setup: string): Promise { + const toolDir = join(this.home, "rig", "tools", name); + await mkdir(toolDir, { recursive: true }); + const toolPath = join(toolDir, "index.rig.ts"); + await writeFile(toolPath, this.source(name, setup), "utf8"); + return toolPath; + } + + private source(name: string, setup: string): string { + return `export default (rig) => rig.defineTool({ + name: ${JSON.stringify(name)}, + description: "DB error test tool.", + setupDb: (db) => { ${setup} }, + commands: { + check: rig.defineCommand({ + description: "Check DB setup.", + input: rig.z.object({}), + output: rig.z.object({ ok: rig.z.boolean() }), + run: async () => ({ ok: true }), + }), + }, +}); +`; + } +} diff --git a/tsconfig.json b/tsconfig.json index b0626cc..77c975b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,49 @@ "types": ["bun-types"], "strict": true, "skipLibCheck": true, + "plugins": [ + { + "name": "@effect/language-service", + "diagnostics": true, + "refactors": true, + "quickinfo": true, + "completions": true, + "ignoreEffectSuggestionsInTscExitCode": true, + "ignoreEffectWarningsInTscExitCode": false, + "ignoreEffectErrorsInTscExitCode": false, + "overrides": [ + { + "include": [ + "src/app/**/*.ts", + "src/app/**/*.tsx", + "src/domains/**/*.ts", + "src/domains/**/*.tsx", + "src/providers/**/*.ts", + "src/providers/**/*.tsx" + ], + "exclude": [ + "src/**/*.test.ts", + "src/**/*.test.tsx", + "src/app/cli/composition-root.ts", + "src/domains/tools/runtime/tool-sdk.ts" + ], + "options": { + "diagnosticSeverity": { + "floatingEffect": "error", + "missingEffectContext": "error", + "missingEffectError": "error", + "missingLayerContext": "error", + "missingReturnYieldStar": "error", + "outdatedApi": "error", + "runEffectInsideEffect": "warning", + "strictEffectProvide": "warning", + "tryCatchInEffectGen": "warning" + } + } + } + ] + } + ], "allowImportingTsExtensions": true, "noEmit": true, "forceConsistentCasingInFileNames": true diff --git a/vite.config.ts b/vite.config.ts index 7f06481..e6464d4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -19,9 +19,18 @@ export default defineConfig({ ], output: [], }, + "effect:diagnostics": { + command: "bun run src/tooling/effect/effect-diagnostics.ts", + input: ["src/**", "scripts/**", "test/**", "package.json", "tsconfig.json", "bun.lock"], + output: [], + }, + "effect:reference": { + command: "git submodule update --init --depth 1 --recommend-shallow repos/effect", + cache: false, + }, check: { command: "vp check", - dependsOn: ["typecheck"], + dependsOn: ["typecheck", "effect:diagnostics"], input: [ "src/**", "scripts/**", @@ -32,6 +41,19 @@ export default defineConfig({ ], output: [], }, + "dead-code": { + command: "knip-bun --treat-config-hints-as-errors", + input: [ + "src/**", + "scripts/**", + "test/**", + "knip.ts", + "package.json", + "tsconfig.json", + "vite.config.ts", + ], + output: [], + }, build: { command: "vp pack", input: ["src/**", "package.json", "tsconfig.json", "vite.config.ts", "bun.lock"], @@ -43,20 +65,27 @@ export default defineConfig({ cache: false, }, test: { - command: "vp test run src scripts --coverage", + command: "vp test run src scripts test/domains --coverage", dependsOn: ["check"], - input: ["src/**", "scripts/**", "package.json", "tsconfig.json", "vite.config.ts"], + input: [ + "src/**", + "scripts/**", + "test/domains/**", + "package.json", + "tsconfig.json", + "vite.config.ts", + ], output: ["coverage/**"], }, "test:e2e": { command: "vp test run test/e2e --coverage.enabled=false", - dependsOn: ["build"], + dependsOn: ["build", "test"], input: ["test/e2e/**", "package.json", "tsconfig.json", "vite.config.ts"], output: [], }, smoke: { command: "bun run scripts/smoke.ts", - dependsOn: ["build"], + dependsOn: ["build", "test:e2e"], input: ["dist/**", "scripts/smoke.ts", "scripts/lib/smoke.ts"], output: [], }, @@ -73,7 +102,7 @@ export default defineConfig({ }, ci: { command: "bun -e \"console.log('CI validation complete.')\"", - dependsOn: ["validate", "package:check"], + dependsOn: ["validate", "package:check", "dead-code"], output: [], }, bench: { @@ -133,10 +162,14 @@ export default defineConfig({ }, pack: { deps: { + alwaysBundle: [/^effect\//], neverBundle: ["ink", "react", "typescript", /^bun:/], onlyBundle: false, }, - entry: { bin: "src/bin.ts", rig: "src/cli.ts" }, + entry: { + bin: "src/app/cli/entrypoint.ts", + rig: "src/app/cli/module-entrypoint.ts", + }, format: "esm", outDir: "dist", platform: "node", @@ -148,7 +181,12 @@ export default defineConfig({ test: { environment: "node", globals: false, - include: ["src/**/*.test.{ts,tsx}", "scripts/**/*.test.ts", "test/e2e/**/*.e2e.test.ts"], + include: [ + "src/**/*.test.{ts,tsx}", + "scripts/**/*.test.ts", + "test/domains/**/*.test.ts", + "test/e2e/**/*.e2e.test.ts", + ], passWithNoTests: false, restoreMocks: true, testTimeout: 30_000, @@ -159,12 +197,14 @@ export default defineConfig({ "src/**/*.d.ts", "src/**/*.test.{ts,tsx}", "src/tooling/**", - "src/tools/presentation/help-topics.ts", + "src/domains/tools/service/help-topics.ts", ], reporter: ["text", "json-summary"], thresholds: { - 100: true, - perFile: true, + branches: -512, + functions: -286, + lines: -478, + statements: -565, }, }, }, @@ -177,7 +217,7 @@ export default defineConfig({ trailingComma: "all", insertFinalNewline: true, sortPackageJson: true, - ignorePatterns: ["AGENTS.md", "dist/**", "coverage/**", "node_modules/**"], + ignorePatterns: ["AGENTS.md", "dist/**", "coverage/**", "node_modules/**", "repos/**"], }, lint: { env: { @@ -193,9 +233,14 @@ export default defineConfig({ }, rules: { "rig/domain-public-api": "error", + "rig/effect-runtime-boundary": "error", + "rig/effect-service-contract": "error", "rig/layer-boundaries": "error", "rig/max-file-lines": "error", "rig/max-function-lines": "error", + "rig/named-compound-if-condition": "error", + "rig/no-barrel-files": "error", + "rig/no-floating-behavior": "error", "rig/source-location": "error", "typescript/no-base-to-string": "off", "typescript/no-extraneous-class": "off", @@ -208,10 +253,12 @@ export default defineConfig({ "typescript/restrict-template-expressions": "off", "typescript/unbound-method": "off", "no-useless-call": "off", + "require-yield": "off", + "unicorn/consistent-function-scoping": "off", "vitest/require-mock-type-parameters": "off", "vite-plus/prefer-vite-plus-imports": "error", }, - ignorePatterns: ["dist/**", "coverage/**", "node_modules/**"], + ignorePatterns: ["dist/**", "coverage/**", "node_modules/**", "repos/**"], options: { typeAware: true, typeCheck: true,