palaryn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +716 -0
- package/dist/sdk/typescript/src/client.d.ts +71 -0
- package/dist/sdk/typescript/src/client.d.ts.map +1 -0
- package/dist/sdk/typescript/src/client.js +176 -0
- package/dist/sdk/typescript/src/client.js.map +1 -0
- package/dist/sdk/typescript/src/errors.d.ts +50 -0
- package/dist/sdk/typescript/src/errors.d.ts.map +1 -0
- package/dist/sdk/typescript/src/errors.js +103 -0
- package/dist/sdk/typescript/src/errors.js.map +1 -0
- package/dist/sdk/typescript/src/index.d.ts +4 -0
- package/dist/sdk/typescript/src/index.d.ts.map +1 -0
- package/dist/sdk/typescript/src/index.js +15 -0
- package/dist/sdk/typescript/src/index.js.map +1 -0
- package/dist/sdk/typescript/src/types.d.ts +101 -0
- package/dist/sdk/typescript/src/types.d.ts.map +1 -0
- package/dist/sdk/typescript/src/types.js +6 -0
- package/dist/sdk/typescript/src/types.js.map +1 -0
- package/dist/src/admin/index.d.ts +2 -0
- package/dist/src/admin/index.d.ts.map +1 -0
- package/dist/src/admin/index.js +6 -0
- package/dist/src/admin/index.js.map +1 -0
- package/dist/src/admin/routes.d.ts +5 -0
- package/dist/src/admin/routes.d.ts.map +1 -0
- package/dist/src/admin/routes.js +471 -0
- package/dist/src/admin/routes.js.map +1 -0
- package/dist/src/admin/templates.d.ts +51 -0
- package/dist/src/admin/templates.d.ts.map +1 -0
- package/dist/src/admin/templates.js +500 -0
- package/dist/src/admin/templates.js.map +1 -0
- package/dist/src/anomaly/detector.d.ts +141 -0
- package/dist/src/anomaly/detector.d.ts.map +1 -0
- package/dist/src/anomaly/detector.js +554 -0
- package/dist/src/anomaly/detector.js.map +1 -0
- package/dist/src/anomaly/index.d.ts +2 -0
- package/dist/src/anomaly/index.d.ts.map +1 -0
- package/dist/src/anomaly/index.js +7 -0
- package/dist/src/anomaly/index.js.map +1 -0
- package/dist/src/approval/manager.d.ts +147 -0
- package/dist/src/approval/manager.d.ts.map +1 -0
- package/dist/src/approval/manager.js +511 -0
- package/dist/src/approval/manager.js.map +1 -0
- package/dist/src/approval/webhook.d.ts +36 -0
- package/dist/src/approval/webhook.d.ts.map +1 -0
- package/dist/src/approval/webhook.js +135 -0
- package/dist/src/approval/webhook.js.map +1 -0
- package/dist/src/audit/logger.d.ts +70 -0
- package/dist/src/audit/logger.d.ts.map +1 -0
- package/dist/src/audit/logger.js +440 -0
- package/dist/src/audit/logger.js.map +1 -0
- package/dist/src/auth/index.d.ts +6 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/index.js +22 -0
- package/dist/src/auth/index.js.map +1 -0
- package/dist/src/auth/password.d.ts +3 -0
- package/dist/src/auth/password.d.ts.map +1 -0
- package/dist/src/auth/password.js +25 -0
- package/dist/src/auth/password.js.map +1 -0
- package/dist/src/auth/pkce.d.ts +13 -0
- package/dist/src/auth/pkce.d.ts.map +1 -0
- package/dist/src/auth/pkce.js +58 -0
- package/dist/src/auth/pkce.js.map +1 -0
- package/dist/src/auth/providers.d.ts +28 -0
- package/dist/src/auth/providers.d.ts.map +1 -0
- package/dist/src/auth/providers.js +198 -0
- package/dist/src/auth/providers.js.map +1 -0
- package/dist/src/auth/routes.d.ts +14 -0
- package/dist/src/auth/routes.d.ts.map +1 -0
- package/dist/src/auth/routes.js +431 -0
- package/dist/src/auth/routes.js.map +1 -0
- package/dist/src/auth/session.d.ts +24 -0
- package/dist/src/auth/session.d.ts.map +1 -0
- package/dist/src/auth/session.js +105 -0
- package/dist/src/auth/session.js.map +1 -0
- package/dist/src/billing/index.d.ts +7 -0
- package/dist/src/billing/index.d.ts.map +1 -0
- package/dist/src/billing/index.js +14 -0
- package/dist/src/billing/index.js.map +1 -0
- package/dist/src/billing/plan-enforcer.d.ts +44 -0
- package/dist/src/billing/plan-enforcer.d.ts.map +1 -0
- package/dist/src/billing/plan-enforcer.js +110 -0
- package/dist/src/billing/plan-enforcer.js.map +1 -0
- package/dist/src/billing/routes.d.ts +15 -0
- package/dist/src/billing/routes.d.ts.map +1 -0
- package/dist/src/billing/routes.js +193 -0
- package/dist/src/billing/routes.js.map +1 -0
- package/dist/src/billing/stripe-client.d.ts +14 -0
- package/dist/src/billing/stripe-client.d.ts.map +1 -0
- package/dist/src/billing/stripe-client.js +51 -0
- package/dist/src/billing/stripe-client.js.map +1 -0
- package/dist/src/billing/webhook-handler.d.ts +19 -0
- package/dist/src/billing/webhook-handler.d.ts.map +1 -0
- package/dist/src/billing/webhook-handler.js +169 -0
- package/dist/src/billing/webhook-handler.js.map +1 -0
- package/dist/src/billing/webhook-routes.d.ts +5 -0
- package/dist/src/billing/webhook-routes.d.ts.map +1 -0
- package/dist/src/billing/webhook-routes.js +30 -0
- package/dist/src/billing/webhook-routes.js.map +1 -0
- package/dist/src/budget/manager.d.ts +95 -0
- package/dist/src/budget/manager.d.ts.map +1 -0
- package/dist/src/budget/manager.js +547 -0
- package/dist/src/budget/manager.js.map +1 -0
- package/dist/src/budget/usage-extractor.d.ts +38 -0
- package/dist/src/budget/usage-extractor.d.ts.map +1 -0
- package/dist/src/budget/usage-extractor.js +165 -0
- package/dist/src/budget/usage-extractor.js.map +1 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +115 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config/defaults.d.ts +3 -0
- package/dist/src/config/defaults.d.ts.map +1 -0
- package/dist/src/config/defaults.js +243 -0
- package/dist/src/config/defaults.js.map +1 -0
- package/dist/src/config/validate.d.ts +15 -0
- package/dist/src/config/validate.d.ts.map +1 -0
- package/dist/src/config/validate.js +105 -0
- package/dist/src/config/validate.js.map +1 -0
- package/dist/src/dlp/composite-scanner.d.ts +47 -0
- package/dist/src/dlp/composite-scanner.d.ts.map +1 -0
- package/dist/src/dlp/composite-scanner.js +186 -0
- package/dist/src/dlp/composite-scanner.js.map +1 -0
- package/dist/src/dlp/index.d.ts +10 -0
- package/dist/src/dlp/index.d.ts.map +1 -0
- package/dist/src/dlp/index.js +26 -0
- package/dist/src/dlp/index.js.map +1 -0
- package/dist/src/dlp/interfaces.d.ts +33 -0
- package/dist/src/dlp/interfaces.d.ts.map +1 -0
- package/dist/src/dlp/interfaces.js +3 -0
- package/dist/src/dlp/interfaces.js.map +1 -0
- package/dist/src/dlp/patterns.d.ts +9 -0
- package/dist/src/dlp/patterns.d.ts.map +1 -0
- package/dist/src/dlp/patterns.js +25 -0
- package/dist/src/dlp/patterns.js.map +1 -0
- package/dist/src/dlp/prompt-injection-backend.d.ts +68 -0
- package/dist/src/dlp/prompt-injection-backend.d.ts.map +1 -0
- package/dist/src/dlp/prompt-injection-backend.js +148 -0
- package/dist/src/dlp/prompt-injection-backend.js.map +1 -0
- package/dist/src/dlp/prompt-injection-patterns.d.ts +32 -0
- package/dist/src/dlp/prompt-injection-patterns.d.ts.map +1 -0
- package/dist/src/dlp/prompt-injection-patterns.js +290 -0
- package/dist/src/dlp/prompt-injection-patterns.js.map +1 -0
- package/dist/src/dlp/regex-backend.d.ts +32 -0
- package/dist/src/dlp/regex-backend.d.ts.map +1 -0
- package/dist/src/dlp/regex-backend.js +153 -0
- package/dist/src/dlp/regex-backend.js.map +1 -0
- package/dist/src/dlp/scanner.d.ts +122 -0
- package/dist/src/dlp/scanner.d.ts.map +1 -0
- package/dist/src/dlp/scanner.js +444 -0
- package/dist/src/dlp/scanner.js.map +1 -0
- package/dist/src/dlp/text-normalizer.d.ts +41 -0
- package/dist/src/dlp/text-normalizer.d.ts.map +1 -0
- package/dist/src/dlp/text-normalizer.js +203 -0
- package/dist/src/dlp/text-normalizer.js.map +1 -0
- package/dist/src/dlp/trufflehog-backend.d.ts +64 -0
- package/dist/src/dlp/trufflehog-backend.d.ts.map +1 -0
- package/dist/src/dlp/trufflehog-backend.js +151 -0
- package/dist/src/dlp/trufflehog-backend.js.map +1 -0
- package/dist/src/executor/http-executor.d.ts +25 -0
- package/dist/src/executor/http-executor.d.ts.map +1 -0
- package/dist/src/executor/http-executor.js +333 -0
- package/dist/src/executor/http-executor.js.map +1 -0
- package/dist/src/executor/index.d.ts +6 -0
- package/dist/src/executor/index.d.ts.map +1 -0
- package/dist/src/executor/index.js +12 -0
- package/dist/src/executor/index.js.map +1 -0
- package/dist/src/executor/interfaces.d.ts +11 -0
- package/dist/src/executor/interfaces.d.ts.map +1 -0
- package/dist/src/executor/interfaces.js +3 -0
- package/dist/src/executor/interfaces.js.map +1 -0
- package/dist/src/executor/noop-executor.d.ts +13 -0
- package/dist/src/executor/noop-executor.d.ts.map +1 -0
- package/dist/src/executor/noop-executor.js +21 -0
- package/dist/src/executor/noop-executor.js.map +1 -0
- package/dist/src/executor/registry.d.ts +30 -0
- package/dist/src/executor/registry.d.ts.map +1 -0
- package/dist/src/executor/registry.js +62 -0
- package/dist/src/executor/registry.js.map +1 -0
- package/dist/src/executor/slack-executor.d.ts +24 -0
- package/dist/src/executor/slack-executor.d.ts.map +1 -0
- package/dist/src/executor/slack-executor.js +147 -0
- package/dist/src/executor/slack-executor.js.map +1 -0
- package/dist/src/index.d.ts +25 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +74 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mcp/auth-verifier.d.ts +23 -0
- package/dist/src/mcp/auth-verifier.d.ts.map +1 -0
- package/dist/src/mcp/auth-verifier.js +162 -0
- package/dist/src/mcp/auth-verifier.js.map +1 -0
- package/dist/src/mcp/bridge.d.ts +132 -0
- package/dist/src/mcp/bridge.d.ts.map +1 -0
- package/dist/src/mcp/bridge.js +734 -0
- package/dist/src/mcp/bridge.js.map +1 -0
- package/dist/src/mcp/http-transport.d.ts +32 -0
- package/dist/src/mcp/http-transport.d.ts.map +1 -0
- package/dist/src/mcp/http-transport.js +538 -0
- package/dist/src/mcp/http-transport.js.map +1 -0
- package/dist/src/mcp/index.d.ts +10 -0
- package/dist/src/mcp/index.d.ts.map +1 -0
- package/dist/src/mcp/index.js +17 -0
- package/dist/src/mcp/index.js.map +1 -0
- package/dist/src/mcp/oauth-pages.d.ts +23 -0
- package/dist/src/mcp/oauth-pages.d.ts.map +1 -0
- package/dist/src/mcp/oauth-pages.js +121 -0
- package/dist/src/mcp/oauth-pages.js.map +1 -0
- package/dist/src/mcp/oauth-postgres-stores.d.ts +55 -0
- package/dist/src/mcp/oauth-postgres-stores.d.ts.map +1 -0
- package/dist/src/mcp/oauth-postgres-stores.js +226 -0
- package/dist/src/mcp/oauth-postgres-stores.js.map +1 -0
- package/dist/src/mcp/oauth-provider.d.ts +95 -0
- package/dist/src/mcp/oauth-provider.d.ts.map +1 -0
- package/dist/src/mcp/oauth-provider.js +360 -0
- package/dist/src/mcp/oauth-provider.js.map +1 -0
- package/dist/src/mcp/oauth-stores.d.ts +62 -0
- package/dist/src/mcp/oauth-stores.d.ts.map +1 -0
- package/dist/src/mcp/oauth-stores.js +154 -0
- package/dist/src/mcp/oauth-stores.js.map +1 -0
- package/dist/src/mcp/server.d.ts +18 -0
- package/dist/src/mcp/server.d.ts.map +1 -0
- package/dist/src/mcp/server.js +51 -0
- package/dist/src/mcp/server.js.map +1 -0
- package/dist/src/metrics/collector.d.ts +106 -0
- package/dist/src/metrics/collector.d.ts.map +1 -0
- package/dist/src/metrics/collector.js +311 -0
- package/dist/src/metrics/collector.js.map +1 -0
- package/dist/src/metrics/index.d.ts +2 -0
- package/dist/src/metrics/index.d.ts.map +1 -0
- package/dist/src/metrics/index.js +6 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/middleware/auth.d.ts +77 -0
- package/dist/src/middleware/auth.d.ts.map +1 -0
- package/dist/src/middleware/auth.js +720 -0
- package/dist/src/middleware/auth.js.map +1 -0
- package/dist/src/middleware/session.d.ts +18 -0
- package/dist/src/middleware/session.d.ts.map +1 -0
- package/dist/src/middleware/session.js +67 -0
- package/dist/src/middleware/session.js.map +1 -0
- package/dist/src/middleware/validate.d.ts +3 -0
- package/dist/src/middleware/validate.d.ts.map +1 -0
- package/dist/src/middleware/validate.js +85 -0
- package/dist/src/middleware/validate.js.map +1 -0
- package/dist/src/policy/engine.d.ts +107 -0
- package/dist/src/policy/engine.d.ts.map +1 -0
- package/dist/src/policy/engine.js +646 -0
- package/dist/src/policy/engine.js.map +1 -0
- package/dist/src/policy/index.d.ts +3 -0
- package/dist/src/policy/index.d.ts.map +1 -0
- package/dist/src/policy/index.js +8 -0
- package/dist/src/policy/index.js.map +1 -0
- package/dist/src/policy/opa-engine.d.ts +176 -0
- package/dist/src/policy/opa-engine.d.ts.map +1 -0
- package/dist/src/policy/opa-engine.js +790 -0
- package/dist/src/policy/opa-engine.js.map +1 -0
- package/dist/src/proxy/forward-proxy.d.ts +30 -0
- package/dist/src/proxy/forward-proxy.d.ts.map +1 -0
- package/dist/src/proxy/forward-proxy.js +580 -0
- package/dist/src/proxy/forward-proxy.js.map +1 -0
- package/dist/src/proxy/index.d.ts +2 -0
- package/dist/src/proxy/index.d.ts.map +1 -0
- package/dist/src/proxy/index.js +8 -0
- package/dist/src/proxy/index.js.map +1 -0
- package/dist/src/ratelimit/limiter.d.ts +45 -0
- package/dist/src/ratelimit/limiter.d.ts.map +1 -0
- package/dist/src/ratelimit/limiter.js +158 -0
- package/dist/src/ratelimit/limiter.js.map +1 -0
- package/dist/src/replay/engine.d.ts +40 -0
- package/dist/src/replay/engine.d.ts.map +1 -0
- package/dist/src/replay/engine.js +106 -0
- package/dist/src/replay/engine.js.map +1 -0
- package/dist/src/replay/index.d.ts +2 -0
- package/dist/src/replay/index.d.ts.map +1 -0
- package/dist/src/replay/index.js +6 -0
- package/dist/src/replay/index.js.map +1 -0
- package/dist/src/saas/index.d.ts +2 -0
- package/dist/src/saas/index.d.ts.map +1 -0
- package/dist/src/saas/index.js +18 -0
- package/dist/src/saas/index.js.map +1 -0
- package/dist/src/saas/routes.d.ts +18 -0
- package/dist/src/saas/routes.d.ts.map +1 -0
- package/dist/src/saas/routes.js +1566 -0
- package/dist/src/saas/routes.js.map +1 -0
- package/dist/src/server/app.d.ts +44 -0
- package/dist/src/server/app.d.ts.map +1 -0
- package/dist/src/server/app.js +854 -0
- package/dist/src/server/app.js.map +1 -0
- package/dist/src/server/errors.d.ts +32 -0
- package/dist/src/server/errors.d.ts.map +1 -0
- package/dist/src/server/errors.js +39 -0
- package/dist/src/server/errors.js.map +1 -0
- package/dist/src/server/gateway.d.ts +165 -0
- package/dist/src/server/gateway.d.ts.map +1 -0
- package/dist/src/server/gateway.js +964 -0
- package/dist/src/server/gateway.js.map +1 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +295 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/server/logger.d.ts +33 -0
- package/dist/src/server/logger.d.ts.map +1 -0
- package/dist/src/server/logger.js +230 -0
- package/dist/src/server/logger.js.map +1 -0
- package/dist/src/server/stream-proxy.d.ts +32 -0
- package/dist/src/server/stream-proxy.d.ts.map +1 -0
- package/dist/src/server/stream-proxy.js +184 -0
- package/dist/src/server/stream-proxy.js.map +1 -0
- package/dist/src/storage/file-persistence.d.ts +48 -0
- package/dist/src/storage/file-persistence.d.ts.map +1 -0
- package/dist/src/storage/file-persistence.js +280 -0
- package/dist/src/storage/file-persistence.js.map +1 -0
- package/dist/src/storage/index.d.ts +5 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +21 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/src/storage/interfaces.d.ts +237 -0
- package/dist/src/storage/interfaces.d.ts.map +1 -0
- package/dist/src/storage/interfaces.js +3 -0
- package/dist/src/storage/interfaces.js.map +1 -0
- package/dist/src/storage/memory.d.ts +162 -0
- package/dist/src/storage/memory.d.ts.map +1 -0
- package/dist/src/storage/memory.js +603 -0
- package/dist/src/storage/memory.js.map +1 -0
- package/dist/src/storage/postgres.d.ts +267 -0
- package/dist/src/storage/postgres.d.ts.map +1 -0
- package/dist/src/storage/postgres.js +1555 -0
- package/dist/src/storage/postgres.js.map +1 -0
- package/dist/src/storage/redis.d.ts +202 -0
- package/dist/src/storage/redis.d.ts.map +1 -0
- package/dist/src/storage/redis.js +629 -0
- package/dist/src/storage/redis.js.map +1 -0
- package/dist/src/tracing/index.d.ts +2 -0
- package/dist/src/tracing/index.d.ts.map +1 -0
- package/dist/src/tracing/index.js +6 -0
- package/dist/src/tracing/index.js.map +1 -0
- package/dist/src/tracing/provider.d.ts +43 -0
- package/dist/src/tracing/provider.d.ts.map +1 -0
- package/dist/src/tracing/provider.js +74 -0
- package/dist/src/tracing/provider.js.map +1 -0
- package/dist/src/trust/calculator.d.ts +54 -0
- package/dist/src/trust/calculator.d.ts.map +1 -0
- package/dist/src/trust/calculator.js +102 -0
- package/dist/src/trust/calculator.js.map +1 -0
- package/dist/src/trust/index.d.ts +2 -0
- package/dist/src/trust/index.d.ts.map +1 -0
- package/dist/src/trust/index.js +7 -0
- package/dist/src/trust/index.js.map +1 -0
- package/dist/src/types/budget.d.ts +30 -0
- package/dist/src/types/budget.d.ts.map +1 -0
- package/dist/src/types/budget.js +3 -0
- package/dist/src/types/budget.js.map +1 -0
- package/dist/src/types/config.d.ts +176 -0
- package/dist/src/types/config.d.ts.map +1 -0
- package/dist/src/types/config.js +3 -0
- package/dist/src/types/config.js.map +1 -0
- package/dist/src/types/events.d.ts +24 -0
- package/dist/src/types/events.d.ts.map +1 -0
- package/dist/src/types/events.js +3 -0
- package/dist/src/types/events.js.map +1 -0
- package/dist/src/types/index.d.ts +8 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +24 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/policy.d.ts +60 -0
- package/dist/src/types/policy.d.ts.map +1 -0
- package/dist/src/types/policy.js +3 -0
- package/dist/src/types/policy.js.map +1 -0
- package/dist/src/types/stripe-config.d.ts +12 -0
- package/dist/src/types/stripe-config.d.ts.map +1 -0
- package/dist/src/types/stripe-config.js +3 -0
- package/dist/src/types/stripe-config.js.map +1 -0
- package/dist/src/types/subscription.d.ts +24 -0
- package/dist/src/types/subscription.d.ts.map +1 -0
- package/dist/src/types/subscription.js +38 -0
- package/dist/src/types/subscription.js.map +1 -0
- package/dist/src/types/tool-call.d.ts +42 -0
- package/dist/src/types/tool-call.d.ts.map +1 -0
- package/dist/src/types/tool-call.js +3 -0
- package/dist/src/types/tool-call.js.map +1 -0
- package/dist/src/types/tool-result.d.ts +58 -0
- package/dist/src/types/tool-result.d.ts.map +1 -0
- package/dist/src/types/tool-result.js +3 -0
- package/dist/src/types/tool-result.js.map +1 -0
- package/dist/src/types/user.d.ts +101 -0
- package/dist/src/types/user.d.ts.map +1 -0
- package/dist/src/types/user.js +6 -0
- package/dist/src/types/user.js.map +1 -0
- package/dist/tests/integration/api.test.d.ts +2 -0
- package/dist/tests/integration/api.test.d.ts.map +1 -0
- package/dist/tests/integration/api.test.js +1199 -0
- package/dist/tests/integration/api.test.js.map +1 -0
- package/dist/tests/integration/proxy.test.d.ts +2 -0
- package/dist/tests/integration/proxy.test.d.ts.map +1 -0
- package/dist/tests/integration/proxy.test.js +251 -0
- package/dist/tests/integration/proxy.test.js.map +1 -0
- package/dist/tests/integration/storage.test.d.ts +16 -0
- package/dist/tests/integration/storage.test.d.ts.map +1 -0
- package/dist/tests/integration/storage.test.js +826 -0
- package/dist/tests/integration/storage.test.js.map +1 -0
- package/dist/tests/unit/admin.test.d.ts +2 -0
- package/dist/tests/unit/admin.test.d.ts.map +1 -0
- package/dist/tests/unit/admin.test.js +698 -0
- package/dist/tests/unit/admin.test.js.map +1 -0
- package/dist/tests/unit/anomaly-detector.test.d.ts +2 -0
- package/dist/tests/unit/anomaly-detector.test.d.ts.map +1 -0
- package/dist/tests/unit/anomaly-detector.test.js +903 -0
- package/dist/tests/unit/anomaly-detector.test.js.map +1 -0
- package/dist/tests/unit/approval-manager.test.d.ts +2 -0
- package/dist/tests/unit/approval-manager.test.d.ts.map +1 -0
- package/dist/tests/unit/approval-manager.test.js +528 -0
- package/dist/tests/unit/approval-manager.test.js.map +1 -0
- package/dist/tests/unit/approval-webhook.test.d.ts +2 -0
- package/dist/tests/unit/approval-webhook.test.d.ts.map +1 -0
- package/dist/tests/unit/approval-webhook.test.js +355 -0
- package/dist/tests/unit/approval-webhook.test.js.map +1 -0
- package/dist/tests/unit/audit-logger.test.d.ts +2 -0
- package/dist/tests/unit/audit-logger.test.d.ts.map +1 -0
- package/dist/tests/unit/audit-logger.test.js +635 -0
- package/dist/tests/unit/audit-logger.test.js.map +1 -0
- package/dist/tests/unit/auth-routes.test.d.ts +2 -0
- package/dist/tests/unit/auth-routes.test.d.ts.map +1 -0
- package/dist/tests/unit/auth-routes.test.js +281 -0
- package/dist/tests/unit/auth-routes.test.js.map +1 -0
- package/dist/tests/unit/auth.test.d.ts +2 -0
- package/dist/tests/unit/auth.test.d.ts.map +1 -0
- package/dist/tests/unit/auth.test.js +1382 -0
- package/dist/tests/unit/auth.test.js.map +1 -0
- package/dist/tests/unit/billing.test.d.ts +2 -0
- package/dist/tests/unit/billing.test.d.ts.map +1 -0
- package/dist/tests/unit/billing.test.js +579 -0
- package/dist/tests/unit/billing.test.js.map +1 -0
- package/dist/tests/unit/budget-manager.test.d.ts +2 -0
- package/dist/tests/unit/budget-manager.test.d.ts.map +1 -0
- package/dist/tests/unit/budget-manager.test.js +778 -0
- package/dist/tests/unit/budget-manager.test.js.map +1 -0
- package/dist/tests/unit/budget-race.test.d.ts +2 -0
- package/dist/tests/unit/budget-race.test.d.ts.map +1 -0
- package/dist/tests/unit/budget-race.test.js +58 -0
- package/dist/tests/unit/budget-race.test.js.map +1 -0
- package/dist/tests/unit/cli.test.d.ts +2 -0
- package/dist/tests/unit/cli.test.d.ts.map +1 -0
- package/dist/tests/unit/cli.test.js +93 -0
- package/dist/tests/unit/cli.test.js.map +1 -0
- package/dist/tests/unit/concurrency.test.d.ts +2 -0
- package/dist/tests/unit/concurrency.test.d.ts.map +1 -0
- package/dist/tests/unit/concurrency.test.js +1270 -0
- package/dist/tests/unit/concurrency.test.js.map +1 -0
- package/dist/tests/unit/config-validate.test.d.ts +2 -0
- package/dist/tests/unit/config-validate.test.d.ts.map +1 -0
- package/dist/tests/unit/config-validate.test.js +230 -0
- package/dist/tests/unit/config-validate.test.js.map +1 -0
- package/dist/tests/unit/defaults.test.d.ts +2 -0
- package/dist/tests/unit/defaults.test.d.ts.map +1 -0
- package/dist/tests/unit/defaults.test.js +364 -0
- package/dist/tests/unit/defaults.test.js.map +1 -0
- package/dist/tests/unit/dlp-backends.test.d.ts +2 -0
- package/dist/tests/unit/dlp-backends.test.d.ts.map +1 -0
- package/dist/tests/unit/dlp-backends.test.js +563 -0
- package/dist/tests/unit/dlp-backends.test.js.map +1 -0
- package/dist/tests/unit/dlp-scanner.test.d.ts +2 -0
- package/dist/tests/unit/dlp-scanner.test.d.ts.map +1 -0
- package/dist/tests/unit/dlp-scanner.test.js +739 -0
- package/dist/tests/unit/dlp-scanner.test.js.map +1 -0
- package/dist/tests/unit/error-responses.test.d.ts +2 -0
- package/dist/tests/unit/error-responses.test.d.ts.map +1 -0
- package/dist/tests/unit/error-responses.test.js +101 -0
- package/dist/tests/unit/error-responses.test.js.map +1 -0
- package/dist/tests/unit/executor-registry.test.d.ts +2 -0
- package/dist/tests/unit/executor-registry.test.d.ts.map +1 -0
- package/dist/tests/unit/executor-registry.test.js +390 -0
- package/dist/tests/unit/executor-registry.test.js.map +1 -0
- package/dist/tests/unit/forward-proxy.test.d.ts +2 -0
- package/dist/tests/unit/forward-proxy.test.d.ts.map +1 -0
- package/dist/tests/unit/forward-proxy.test.js +621 -0
- package/dist/tests/unit/forward-proxy.test.js.map +1 -0
- package/dist/tests/unit/gateway-features.test.d.ts +2 -0
- package/dist/tests/unit/gateway-features.test.d.ts.map +1 -0
- package/dist/tests/unit/gateway-features.test.js +753 -0
- package/dist/tests/unit/gateway-features.test.js.map +1 -0
- package/dist/tests/unit/http-executor.test.d.ts +2 -0
- package/dist/tests/unit/http-executor.test.d.ts.map +1 -0
- package/dist/tests/unit/http-executor.test.js +310 -0
- package/dist/tests/unit/http-executor.test.js.map +1 -0
- package/dist/tests/unit/mcp-bridge.test.d.ts +2 -0
- package/dist/tests/unit/mcp-bridge.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-bridge.test.js +1136 -0
- package/dist/tests/unit/mcp-bridge.test.js.map +1 -0
- package/dist/tests/unit/mcp-http-transport.test.d.ts +2 -0
- package/dist/tests/unit/mcp-http-transport.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-http-transport.test.js +899 -0
- package/dist/tests/unit/mcp-http-transport.test.js.map +1 -0
- package/dist/tests/unit/mcp-oauth.test.d.ts +2 -0
- package/dist/tests/unit/mcp-oauth.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-oauth.test.js +759 -0
- package/dist/tests/unit/mcp-oauth.test.js.map +1 -0
- package/dist/tests/unit/mcp-server.test.d.ts +15 -0
- package/dist/tests/unit/mcp-server.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-server.test.js +158 -0
- package/dist/tests/unit/mcp-server.test.js.map +1 -0
- package/dist/tests/unit/metrics.test.d.ts +2 -0
- package/dist/tests/unit/metrics.test.d.ts.map +1 -0
- package/dist/tests/unit/metrics.test.js +208 -0
- package/dist/tests/unit/metrics.test.js.map +1 -0
- package/dist/tests/unit/oauth.test.d.ts +2 -0
- package/dist/tests/unit/oauth.test.d.ts.map +1 -0
- package/dist/tests/unit/oauth.test.js +281 -0
- package/dist/tests/unit/oauth.test.js.map +1 -0
- package/dist/tests/unit/opa-circuit-breaker.test.d.ts +2 -0
- package/dist/tests/unit/opa-circuit-breaker.test.d.ts.map +1 -0
- package/dist/tests/unit/opa-circuit-breaker.test.js +297 -0
- package/dist/tests/unit/opa-circuit-breaker.test.js.map +1 -0
- package/dist/tests/unit/opa-engine.test.d.ts +2 -0
- package/dist/tests/unit/opa-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/opa-engine.test.js +1813 -0
- package/dist/tests/unit/opa-engine.test.js.map +1 -0
- package/dist/tests/unit/pipeline-timing.test.d.ts +2 -0
- package/dist/tests/unit/pipeline-timing.test.d.ts.map +1 -0
- package/dist/tests/unit/pipeline-timing.test.js +528 -0
- package/dist/tests/unit/pipeline-timing.test.js.map +1 -0
- package/dist/tests/unit/policy-engine.test.d.ts +2 -0
- package/dist/tests/unit/policy-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/policy-engine.test.js +1345 -0
- package/dist/tests/unit/policy-engine.test.js.map +1 -0
- package/dist/tests/unit/policy-store.test.d.ts +2 -0
- package/dist/tests/unit/policy-store.test.d.ts.map +1 -0
- package/dist/tests/unit/policy-store.test.js +60 -0
- package/dist/tests/unit/policy-store.test.js.map +1 -0
- package/dist/tests/unit/postgres-storage.test.d.ts +2 -0
- package/dist/tests/unit/postgres-storage.test.d.ts.map +1 -0
- package/dist/tests/unit/postgres-storage.test.js +614 -0
- package/dist/tests/unit/postgres-storage.test.js.map +1 -0
- package/dist/tests/unit/prompt-injection-backend.test.d.ts +2 -0
- package/dist/tests/unit/prompt-injection-backend.test.d.ts.map +1 -0
- package/dist/tests/unit/prompt-injection-backend.test.js +621 -0
- package/dist/tests/unit/prompt-injection-backend.test.js.map +1 -0
- package/dist/tests/unit/proxy-hardening.test.d.ts +2 -0
- package/dist/tests/unit/proxy-hardening.test.d.ts.map +1 -0
- package/dist/tests/unit/proxy-hardening.test.js +166 -0
- package/dist/tests/unit/proxy-hardening.test.js.map +1 -0
- package/dist/tests/unit/rate-limiter.test.d.ts +2 -0
- package/dist/tests/unit/rate-limiter.test.d.ts.map +1 -0
- package/dist/tests/unit/rate-limiter.test.js +443 -0
- package/dist/tests/unit/rate-limiter.test.js.map +1 -0
- package/dist/tests/unit/redis-storage.test.d.ts +2 -0
- package/dist/tests/unit/redis-storage.test.d.ts.map +1 -0
- package/dist/tests/unit/redis-storage.test.js +766 -0
- package/dist/tests/unit/redis-storage.test.js.map +1 -0
- package/dist/tests/unit/replay-engine.test.d.ts +2 -0
- package/dist/tests/unit/replay-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/replay-engine.test.js +371 -0
- package/dist/tests/unit/replay-engine.test.js.map +1 -0
- package/dist/tests/unit/saas-routes.test.d.ts +2 -0
- package/dist/tests/unit/saas-routes.test.d.ts.map +1 -0
- package/dist/tests/unit/saas-routes.test.js +1399 -0
- package/dist/tests/unit/saas-routes.test.js.map +1 -0
- package/dist/tests/unit/session.test.d.ts +2 -0
- package/dist/tests/unit/session.test.d.ts.map +1 -0
- package/dist/tests/unit/session.test.js +532 -0
- package/dist/tests/unit/session.test.js.map +1 -0
- package/dist/tests/unit/slack-executor.test.d.ts +2 -0
- package/dist/tests/unit/slack-executor.test.d.ts.map +1 -0
- package/dist/tests/unit/slack-executor.test.js +209 -0
- package/dist/tests/unit/slack-executor.test.js.map +1 -0
- package/dist/tests/unit/storage-hardening.test.d.ts +2 -0
- package/dist/tests/unit/storage-hardening.test.d.ts.map +1 -0
- package/dist/tests/unit/storage-hardening.test.js +165 -0
- package/dist/tests/unit/storage-hardening.test.js.map +1 -0
- package/dist/tests/unit/storage.test.d.ts +2 -0
- package/dist/tests/unit/storage.test.d.ts.map +1 -0
- package/dist/tests/unit/storage.test.js +698 -0
- package/dist/tests/unit/storage.test.js.map +1 -0
- package/dist/tests/unit/text-normalizer.test.d.ts +2 -0
- package/dist/tests/unit/text-normalizer.test.d.ts.map +1 -0
- package/dist/tests/unit/text-normalizer.test.js +229 -0
- package/dist/tests/unit/text-normalizer.test.js.map +1 -0
- package/dist/tests/unit/tracing.test.d.ts +2 -0
- package/dist/tests/unit/tracing.test.d.ts.map +1 -0
- package/dist/tests/unit/tracing.test.js +611 -0
- package/dist/tests/unit/tracing.test.js.map +1 -0
- package/dist/tests/unit/trust-calculator.test.d.ts +2 -0
- package/dist/tests/unit/trust-calculator.test.d.ts.map +1 -0
- package/dist/tests/unit/trust-calculator.test.js +497 -0
- package/dist/tests/unit/trust-calculator.test.js.map +1 -0
- package/dist/tests/unit/ts-sdk.test.d.ts +2 -0
- package/dist/tests/unit/ts-sdk.test.d.ts.map +1 -0
- package/dist/tests/unit/ts-sdk.test.js +421 -0
- package/dist/tests/unit/ts-sdk.test.js.map +1 -0
- package/dist/tests/unit/usage-extractor-llm.test.d.ts +2 -0
- package/dist/tests/unit/usage-extractor-llm.test.d.ts.map +1 -0
- package/dist/tests/unit/usage-extractor-llm.test.js +139 -0
- package/dist/tests/unit/usage-extractor-llm.test.js.map +1 -0
- package/dist/tests/unit/usage-extractor.test.d.ts +2 -0
- package/dist/tests/unit/usage-extractor.test.d.ts.map +1 -0
- package/dist/tests/unit/usage-extractor.test.js +271 -0
- package/dist/tests/unit/usage-extractor.test.js.map +1 -0
- package/dist/tests/unit/user-stores.test.d.ts +2 -0
- package/dist/tests/unit/user-stores.test.d.ts.map +1 -0
- package/dist/tests/unit/user-stores.test.js +687 -0
- package/dist/tests/unit/user-stores.test.js.map +1 -0
- package/dist/tests/unit/validate.test.d.ts +2 -0
- package/dist/tests/unit/validate.test.d.ts.map +1 -0
- package/dist/tests/unit/validate.test.js +545 -0
- package/dist/tests/unit/validate.test.js.map +1 -0
- package/package.json +86 -0
- package/policy-packs/README.md +42 -0
- package/policy-packs/default.yaml +46 -0
- package/policy-packs/dev_fast.yaml +54 -0
- package/policy-packs/prod_strict.yaml +83 -0
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const redis_1 = require("../../src/storage/redis");
|
|
4
|
+
function makeToolResult(overrides = {}) {
|
|
5
|
+
return {
|
|
6
|
+
tool_call_id: 'tc-001',
|
|
7
|
+
task_id: 'task-001',
|
|
8
|
+
status: 'ok',
|
|
9
|
+
policy: { decision: 'allow', reasons: [] },
|
|
10
|
+
dlp: { detected: [], redactions: [], severity: 'low' },
|
|
11
|
+
budget: { estimated_cost_usd: 0, spent_cost_usd_task: 0, remaining_cost_usd_task: 100 },
|
|
12
|
+
timing: { started_at: new Date().toISOString(), duration_ms: 10 },
|
|
13
|
+
...overrides,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function makeApprovalRecord(overrides = {}) {
|
|
17
|
+
return {
|
|
18
|
+
approval_id: 'appr-001',
|
|
19
|
+
tool_call_id: 'tc-001',
|
|
20
|
+
task_id: 'task-001',
|
|
21
|
+
workspace_id: 'ws-001',
|
|
22
|
+
actor_id: 'actor-001',
|
|
23
|
+
tool_name: 'http.get',
|
|
24
|
+
tool_capability: 'read',
|
|
25
|
+
args_summary: '{}',
|
|
26
|
+
scope: 'global',
|
|
27
|
+
reason: 'test',
|
|
28
|
+
token_hash: 'hash-001',
|
|
29
|
+
status: 'pending',
|
|
30
|
+
created_at: new Date().toISOString(),
|
|
31
|
+
expires_at: new Date(Date.now() + 3600000).toISOString(),
|
|
32
|
+
...overrides,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Mock Redis client
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
function createMockPipeline(zcardCount = 1) {
|
|
39
|
+
return {
|
|
40
|
+
zremrangebyscore: jest.fn().mockReturnThis(),
|
|
41
|
+
zadd: jest.fn().mockReturnThis(),
|
|
42
|
+
zcard: jest.fn().mockReturnThis(),
|
|
43
|
+
pexpire: jest.fn().mockReturnThis(),
|
|
44
|
+
hset: jest.fn().mockReturnThis(),
|
|
45
|
+
sadd: jest.fn().mockReturnThis(),
|
|
46
|
+
srem: jest.fn().mockReturnThis(),
|
|
47
|
+
exec: jest.fn().mockResolvedValue([
|
|
48
|
+
[null, 0], // zremrangebyscore result
|
|
49
|
+
[null, 1], // zadd result
|
|
50
|
+
[null, zcardCount], // zcard result
|
|
51
|
+
[null, 1], // pexpire result
|
|
52
|
+
]),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function createMockRedis(pipeline) {
|
|
56
|
+
const mp = pipeline || createMockPipeline();
|
|
57
|
+
return {
|
|
58
|
+
pipeline: jest.fn().mockReturnValue(mp),
|
|
59
|
+
get: jest.fn().mockResolvedValue(null),
|
|
60
|
+
set: jest.fn().mockResolvedValue('OK'),
|
|
61
|
+
exists: jest.fn().mockResolvedValue(0),
|
|
62
|
+
del: jest.fn().mockResolvedValue(1),
|
|
63
|
+
scan: jest.fn().mockResolvedValue(['0', []]),
|
|
64
|
+
pttl: jest.fn().mockResolvedValue(5000),
|
|
65
|
+
zremrangebyscore: jest.fn().mockResolvedValue(0),
|
|
66
|
+
zrangebyscore: jest.fn().mockResolvedValue([]),
|
|
67
|
+
// Hash operations (for Budget, Audit, Approval stores)
|
|
68
|
+
hset: jest.fn().mockResolvedValue(1),
|
|
69
|
+
hget: jest.fn().mockResolvedValue(null),
|
|
70
|
+
hgetall: jest.fn().mockResolvedValue({}),
|
|
71
|
+
hdel: jest.fn().mockResolvedValue(1),
|
|
72
|
+
// Sorted set operations (for Audit store indices)
|
|
73
|
+
zadd: jest.fn().mockResolvedValue(1),
|
|
74
|
+
// Set operations (for Approval store pending set)
|
|
75
|
+
sadd: jest.fn().mockResolvedValue(1),
|
|
76
|
+
srem: jest.fn().mockResolvedValue(1),
|
|
77
|
+
smembers: jest.fn().mockResolvedValue([]),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
// RedisRateLimitStore
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
describe('RedisRateLimitStore', () => {
|
|
84
|
+
let mockRedis;
|
|
85
|
+
let mockPipeline;
|
|
86
|
+
let store;
|
|
87
|
+
beforeEach(() => {
|
|
88
|
+
mockPipeline = createMockPipeline();
|
|
89
|
+
mockRedis = createMockRedis(mockPipeline);
|
|
90
|
+
store = new redis_1.RedisRateLimitStore(mockRedis);
|
|
91
|
+
});
|
|
92
|
+
it('records a hit and returns allowed=true when under the limit', () => {
|
|
93
|
+
const result = store.hit('actor:agent-001', 60000, 100);
|
|
94
|
+
expect(result.allowed).toBe(true);
|
|
95
|
+
expect(result.current).toBe(1);
|
|
96
|
+
expect(result.limit).toBe(100);
|
|
97
|
+
expect(typeof result.resetAt).toBe('number');
|
|
98
|
+
// Verify Redis pipeline was created and executed
|
|
99
|
+
expect(mockRedis.pipeline).toHaveBeenCalled();
|
|
100
|
+
expect(mockPipeline.zremrangebyscore).toHaveBeenCalled();
|
|
101
|
+
expect(mockPipeline.zadd).toHaveBeenCalled();
|
|
102
|
+
expect(mockPipeline.zcard).toHaveBeenCalled();
|
|
103
|
+
expect(mockPipeline.pexpire).toHaveBeenCalled();
|
|
104
|
+
expect(mockPipeline.exec).toHaveBeenCalled();
|
|
105
|
+
});
|
|
106
|
+
it('returns allowed=false when the current count exceeds maxRequests', () => {
|
|
107
|
+
const windowMs = 60000;
|
|
108
|
+
const maxRequests = 3;
|
|
109
|
+
// Fill up the window to the limit
|
|
110
|
+
store.hit('actor:agent-001', windowMs, maxRequests);
|
|
111
|
+
store.hit('actor:agent-001', windowMs, maxRequests);
|
|
112
|
+
store.hit('actor:agent-001', windowMs, maxRequests);
|
|
113
|
+
// Fourth hit should exceed the limit
|
|
114
|
+
const result = store.hit('actor:agent-001', windowMs, maxRequests);
|
|
115
|
+
expect(result.allowed).toBe(false);
|
|
116
|
+
expect(result.current).toBe(4);
|
|
117
|
+
expect(result.limit).toBe(maxRequests);
|
|
118
|
+
});
|
|
119
|
+
it('reset clears local state', () => {
|
|
120
|
+
store.hit('actor:agent-001', 60000, 100);
|
|
121
|
+
store.hit('actor:agent-002', 60000, 100);
|
|
122
|
+
store.reset();
|
|
123
|
+
// After reset, a hit on the same key should start fresh (current=1)
|
|
124
|
+
const result = store.hit('actor:agent-001', 60000, 100);
|
|
125
|
+
expect(result.current).toBe(1);
|
|
126
|
+
expect(result.allowed).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
it('uses the correct key prefix for Redis operations', () => {
|
|
129
|
+
const customPrefix = 'myrl:';
|
|
130
|
+
const customStore = new redis_1.RedisRateLimitStore(mockRedis, customPrefix);
|
|
131
|
+
customStore.hit('actor:agent-001', 60000, 100);
|
|
132
|
+
// The zadd call should use the prefixed key
|
|
133
|
+
const zaddArgs = mockPipeline.zadd.mock.calls[0];
|
|
134
|
+
expect(zaddArgs[0]).toBe('myrl:actor:agent-001');
|
|
135
|
+
// zremrangebyscore should also use the prefixed key
|
|
136
|
+
const zremArgs = mockPipeline.zremrangebyscore.mock.calls[0];
|
|
137
|
+
expect(zremArgs[0]).toBe('myrl:actor:agent-001');
|
|
138
|
+
});
|
|
139
|
+
it('uses the default key prefix "rl:" when none is provided', () => {
|
|
140
|
+
store.hit('workspace:ws-001', 60000, 500);
|
|
141
|
+
const zaddArgs = mockPipeline.zadd.mock.calls[0];
|
|
142
|
+
expect(zaddArgs[0]).toBe('rl:workspace:ws-001');
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
// RedisIdempotencyStore
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
describe('RedisIdempotencyStore', () => {
|
|
149
|
+
let mockRedis;
|
|
150
|
+
let store;
|
|
151
|
+
beforeEach(() => {
|
|
152
|
+
jest.useFakeTimers();
|
|
153
|
+
mockRedis = createMockRedis();
|
|
154
|
+
store = new redis_1.RedisIdempotencyStore(mockRedis);
|
|
155
|
+
});
|
|
156
|
+
afterEach(() => {
|
|
157
|
+
jest.useRealTimers();
|
|
158
|
+
});
|
|
159
|
+
it('set stores and get retrieves from local cache', async () => {
|
|
160
|
+
const result = makeToolResult({ tool_call_id: 'tc-001' });
|
|
161
|
+
const ttlMs = 300000; // 5 minutes
|
|
162
|
+
store.set('tc-001', result, ttlMs);
|
|
163
|
+
const retrieved = await store.get('tc-001');
|
|
164
|
+
expect(retrieved).toEqual(result);
|
|
165
|
+
});
|
|
166
|
+
it('get returns undefined for expired entries', async () => {
|
|
167
|
+
const result = makeToolResult({ tool_call_id: 'tc-002' });
|
|
168
|
+
const ttlMs = 5000; // 5 seconds
|
|
169
|
+
store.set('tc-002', result, ttlMs);
|
|
170
|
+
// Should be available before expiry
|
|
171
|
+
expect(await store.get('tc-002')).toEqual(result);
|
|
172
|
+
// Advance time past the TTL
|
|
173
|
+
jest.advanceTimersByTime(6000);
|
|
174
|
+
// Should return undefined after expiry
|
|
175
|
+
expect(await store.get('tc-002')).toBeUndefined();
|
|
176
|
+
});
|
|
177
|
+
it('has returns true for cached entries', async () => {
|
|
178
|
+
store.set('tc-003', makeToolResult({ tool_call_id: 'tc-003' }), 300000);
|
|
179
|
+
expect(await store.has('tc-003')).toBe(true);
|
|
180
|
+
});
|
|
181
|
+
it('has returns false for expired entries', async () => {
|
|
182
|
+
store.set('tc-004', makeToolResult({ tool_call_id: 'tc-004' }), 5000);
|
|
183
|
+
// Before expiry
|
|
184
|
+
expect(await store.has('tc-004')).toBe(true);
|
|
185
|
+
// Advance past TTL
|
|
186
|
+
jest.advanceTimersByTime(6000);
|
|
187
|
+
// After expiry
|
|
188
|
+
expect(await store.has('tc-004')).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
it('clear empties the local cache', async () => {
|
|
191
|
+
store.set('tc-005', makeToolResult({ tool_call_id: 'tc-005' }), 300000);
|
|
192
|
+
store.set('tc-006', makeToolResult({ tool_call_id: 'tc-006' }), 300000);
|
|
193
|
+
expect(await store.has('tc-005')).toBe(true);
|
|
194
|
+
expect(await store.has('tc-006')).toBe(true);
|
|
195
|
+
store.clear();
|
|
196
|
+
expect(await store.get('tc-005')).toBeUndefined();
|
|
197
|
+
expect(await store.get('tc-006')).toBeUndefined();
|
|
198
|
+
expect(await store.has('tc-005')).toBe(false);
|
|
199
|
+
expect(await store.has('tc-006')).toBe(false);
|
|
200
|
+
});
|
|
201
|
+
it('set fires an async Redis SET with PX expiry', () => {
|
|
202
|
+
const result = makeToolResult({ tool_call_id: 'tc-007' });
|
|
203
|
+
const ttlMs = 60000;
|
|
204
|
+
store.set('tc-007', result, ttlMs);
|
|
205
|
+
expect(mockRedis.set).toHaveBeenCalledWith('idem:tc-007', JSON.stringify(result), 'PX', ttlMs);
|
|
206
|
+
});
|
|
207
|
+
it('uses the correct key prefix for Redis operations', () => {
|
|
208
|
+
const customStore = new redis_1.RedisIdempotencyStore(mockRedis, 'myidem:');
|
|
209
|
+
const result = makeToolResult({ tool_call_id: 'tc-008' });
|
|
210
|
+
customStore.set('tc-008', result, 60000);
|
|
211
|
+
expect(mockRedis.set).toHaveBeenCalledWith('myidem:tc-008', JSON.stringify(result), 'PX', 60000);
|
|
212
|
+
});
|
|
213
|
+
it('uses the default key prefix "idem:" when none is provided', () => {
|
|
214
|
+
const result = makeToolResult({ tool_call_id: 'tc-009' });
|
|
215
|
+
store.set('tc-009', result, 30000);
|
|
216
|
+
expect(mockRedis.set).toHaveBeenCalledWith('idem:tc-009', JSON.stringify(result), 'PX', 30000);
|
|
217
|
+
});
|
|
218
|
+
it('get returns undefined for keys not in the cache', async () => {
|
|
219
|
+
expect(await store.get('nonexistent')).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
it('has returns false for keys not in the cache', async () => {
|
|
222
|
+
expect(await store.has('nonexistent')).toBe(false);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
// RedisBudgetStore
|
|
227
|
+
// ---------------------------------------------------------------------------
|
|
228
|
+
describe('RedisBudgetStore', () => {
|
|
229
|
+
let mockRedis;
|
|
230
|
+
let store;
|
|
231
|
+
beforeEach(() => {
|
|
232
|
+
mockRedis = createMockRedis();
|
|
233
|
+
store = new redis_1.RedisBudgetStore(mockRedis);
|
|
234
|
+
});
|
|
235
|
+
describe('getTaskState / setTaskState', () => {
|
|
236
|
+
it('returns undefined for unknown task', () => {
|
|
237
|
+
expect(store.getTaskState('task-unknown')).toBeUndefined();
|
|
238
|
+
});
|
|
239
|
+
it('stores and retrieves a task state from local cache', () => {
|
|
240
|
+
const state = {
|
|
241
|
+
task_id: 'task-001',
|
|
242
|
+
workspace_id: 'ws-001',
|
|
243
|
+
actor_id: 'actor-001',
|
|
244
|
+
spent_usd: 0.50,
|
|
245
|
+
steps: 3,
|
|
246
|
+
started_at: '2025-01-01T00:00:00Z',
|
|
247
|
+
};
|
|
248
|
+
store.setTaskState('task-001', state);
|
|
249
|
+
expect(store.getTaskState('task-001')).toEqual(state);
|
|
250
|
+
});
|
|
251
|
+
it('fires an async HSET to Redis on setTaskState', () => {
|
|
252
|
+
const state = {
|
|
253
|
+
task_id: 'task-002',
|
|
254
|
+
workspace_id: 'ws-002',
|
|
255
|
+
actor_id: 'actor-002',
|
|
256
|
+
spent_usd: 1.25,
|
|
257
|
+
steps: 5,
|
|
258
|
+
started_at: '2025-06-15T12:00:00Z',
|
|
259
|
+
};
|
|
260
|
+
store.setTaskState('task-002', state);
|
|
261
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:task:task-002', 'task_id', 'task-002', 'workspace_id', 'ws-002', 'actor_id', 'actor-002', 'spent_usd', '1.25', 'steps', '5', 'started_at', '2025-06-15T12:00:00Z');
|
|
262
|
+
});
|
|
263
|
+
it('overwrites an existing task state', () => {
|
|
264
|
+
const state1 = {
|
|
265
|
+
task_id: 'task-003',
|
|
266
|
+
workspace_id: 'ws-001',
|
|
267
|
+
actor_id: 'actor-001',
|
|
268
|
+
spent_usd: 0,
|
|
269
|
+
steps: 0,
|
|
270
|
+
started_at: '2025-01-01T00:00:00Z',
|
|
271
|
+
};
|
|
272
|
+
const state2 = { ...state1, spent_usd: 2.50, steps: 10 };
|
|
273
|
+
store.setTaskState('task-003', state1);
|
|
274
|
+
store.setTaskState('task-003', state2);
|
|
275
|
+
expect(store.getTaskState('task-003')).toEqual(state2);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
describe('getCounter / incrementCounter', () => {
|
|
279
|
+
it('returns 0 for unknown counter', () => {
|
|
280
|
+
expect(store.getCounter('user:daily:actor-001')).toBe(0);
|
|
281
|
+
});
|
|
282
|
+
it('increments and retrieves a counter', () => {
|
|
283
|
+
store.incrementCounter('user:daily:actor-001', 0.10);
|
|
284
|
+
store.incrementCounter('user:daily:actor-001', 0.25);
|
|
285
|
+
expect(store.getCounter('user:daily:actor-001')).toBeCloseTo(0.35);
|
|
286
|
+
});
|
|
287
|
+
it('fires HSET to Redis counters hash on increment', () => {
|
|
288
|
+
store.incrementCounter('ws:monthly:ws-001', 1.50);
|
|
289
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:counters', 'ws:monthly:ws-001', '1.5');
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
describe('getRetryCount / incrementRetryCount', () => {
|
|
293
|
+
it('returns 0 for unknown tool call', () => {
|
|
294
|
+
expect(store.getRetryCount('tc-unknown')).toBe(0);
|
|
295
|
+
});
|
|
296
|
+
it('increments and returns the new count', () => {
|
|
297
|
+
expect(store.incrementRetryCount('tc-001')).toBe(1);
|
|
298
|
+
expect(store.incrementRetryCount('tc-001')).toBe(2);
|
|
299
|
+
expect(store.getRetryCount('tc-001')).toBe(2);
|
|
300
|
+
});
|
|
301
|
+
it('fires HSET to Redis retries hash on increment', () => {
|
|
302
|
+
store.incrementRetryCount('tc-010');
|
|
303
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:retries', 'tc-010', '1');
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
describe('reset', () => {
|
|
307
|
+
it('clears all local state', () => {
|
|
308
|
+
store.setTaskState('task-001', {
|
|
309
|
+
task_id: 'task-001',
|
|
310
|
+
workspace_id: 'ws-001',
|
|
311
|
+
actor_id: 'actor-001',
|
|
312
|
+
spent_usd: 1,
|
|
313
|
+
steps: 1,
|
|
314
|
+
started_at: '2025-01-01T00:00:00Z',
|
|
315
|
+
});
|
|
316
|
+
store.incrementCounter('key-1', 5);
|
|
317
|
+
store.incrementRetryCount('tc-001');
|
|
318
|
+
store.reset();
|
|
319
|
+
expect(store.getTaskState('task-001')).toBeUndefined();
|
|
320
|
+
expect(store.getCounter('key-1')).toBe(0);
|
|
321
|
+
expect(store.getRetryCount('tc-001')).toBe(0);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
describe('hydrate', () => {
|
|
325
|
+
it('loads counters and retries from Redis', async () => {
|
|
326
|
+
mockRedis.hgetall
|
|
327
|
+
.mockResolvedValueOnce({ 'user:daily:a1': '2.5', 'ws:monthly:ws1': '10' }) // counters
|
|
328
|
+
.mockResolvedValueOnce({ 'tc-001': '3', 'tc-002': '1' }); // retries
|
|
329
|
+
await store.hydrate();
|
|
330
|
+
expect(store.getCounter('user:daily:a1')).toBe(2.5);
|
|
331
|
+
expect(store.getCounter('ws:monthly:ws1')).toBe(10);
|
|
332
|
+
expect(store.getRetryCount('tc-001')).toBe(3);
|
|
333
|
+
expect(store.getRetryCount('tc-002')).toBe(1);
|
|
334
|
+
});
|
|
335
|
+
it('loads task states from Redis via SCAN', async () => {
|
|
336
|
+
const taskData = {
|
|
337
|
+
task_id: 'task-100',
|
|
338
|
+
workspace_id: 'ws-100',
|
|
339
|
+
actor_id: 'actor-100',
|
|
340
|
+
spent_usd: '5.25',
|
|
341
|
+
steps: '12',
|
|
342
|
+
started_at: '2025-03-01T00:00:00Z',
|
|
343
|
+
};
|
|
344
|
+
mockRedis.hgetall
|
|
345
|
+
.mockResolvedValueOnce({}) // counters (empty)
|
|
346
|
+
.mockResolvedValueOnce({}) // retries (empty)
|
|
347
|
+
.mockResolvedValueOnce(taskData); // task hash
|
|
348
|
+
mockRedis.scan.mockResolvedValueOnce(['0', ['budget:task:task-100']]);
|
|
349
|
+
await store.hydrate();
|
|
350
|
+
const state = store.getTaskState('task-100');
|
|
351
|
+
expect(state).toBeDefined();
|
|
352
|
+
expect(state.task_id).toBe('task-100');
|
|
353
|
+
expect(state.spent_usd).toBe(5.25);
|
|
354
|
+
expect(state.steps).toBe(12);
|
|
355
|
+
});
|
|
356
|
+
it('handles hydrate errors gracefully', async () => {
|
|
357
|
+
mockRedis.hgetall.mockRejectedValueOnce(new Error('Redis down'));
|
|
358
|
+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
359
|
+
await store.hydrate();
|
|
360
|
+
expect(consoleSpy).toHaveBeenCalledWith('[RedisBudgetStore] hydrate failed:', 'Redis down');
|
|
361
|
+
consoleSpy.mockRestore();
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
describe('prefix', () => {
|
|
365
|
+
it('uses the default prefix "budget:"', () => {
|
|
366
|
+
store.incrementCounter('mykey', 1);
|
|
367
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:counters', 'mykey', '1');
|
|
368
|
+
});
|
|
369
|
+
it('uses a custom prefix', () => {
|
|
370
|
+
const customStore = new redis_1.RedisBudgetStore(mockRedis, 'app:budget:');
|
|
371
|
+
customStore.incrementCounter('mykey', 1);
|
|
372
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('app:budget:counters', 'mykey', '1');
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
// ---------------------------------------------------------------------------
|
|
377
|
+
// RedisAuditStore
|
|
378
|
+
// ---------------------------------------------------------------------------
|
|
379
|
+
describe('RedisAuditStore', () => {
|
|
380
|
+
let mockRedis;
|
|
381
|
+
let store;
|
|
382
|
+
const makeEvent = (overrides = {}) => ({
|
|
383
|
+
event_id: overrides.event_id || 'evt-001',
|
|
384
|
+
event_type: (overrides.event_type || 'TOOL_CALL_RECEIVED'),
|
|
385
|
+
timestamp: overrides.timestamp || '2025-01-01T00:00:00Z',
|
|
386
|
+
tool_call_id: overrides.tool_call_id || 'tc-001',
|
|
387
|
+
task_id: overrides.task_id || 'task-001',
|
|
388
|
+
workspace_id: overrides.workspace_id || 'ws-001',
|
|
389
|
+
actor_id: overrides.actor_id || 'actor-001',
|
|
390
|
+
tool_name: overrides.tool_name || 'http.get',
|
|
391
|
+
metadata: overrides.metadata || {},
|
|
392
|
+
});
|
|
393
|
+
beforeEach(() => {
|
|
394
|
+
mockRedis = createMockRedis();
|
|
395
|
+
store = new redis_1.RedisAuditStore(mockRedis);
|
|
396
|
+
});
|
|
397
|
+
describe('append', () => {
|
|
398
|
+
it('adds event to local cache', () => {
|
|
399
|
+
const event = makeEvent();
|
|
400
|
+
store.append(event);
|
|
401
|
+
expect(store.getAll()).toHaveLength(1);
|
|
402
|
+
expect(store.getAll()[0]).toEqual(event);
|
|
403
|
+
});
|
|
404
|
+
it('fires HSET to store event data in Redis via pipeline', () => {
|
|
405
|
+
const event = makeEvent({ event_id: 'evt-100' });
|
|
406
|
+
store.append(event);
|
|
407
|
+
expect(mockRedis.pipeline).toHaveBeenCalled();
|
|
408
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
409
|
+
expect(pipeline.hset).toHaveBeenCalledWith('audit:data', 'evt-100', JSON.stringify(event));
|
|
410
|
+
});
|
|
411
|
+
it('fires ZADD for task index via pipeline', () => {
|
|
412
|
+
const event = makeEvent({ task_id: 'task-abc', timestamp: '2025-06-15T12:00:00Z' });
|
|
413
|
+
store.append(event);
|
|
414
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
415
|
+
const ts = new Date('2025-06-15T12:00:00Z').getTime();
|
|
416
|
+
expect(pipeline.zadd).toHaveBeenCalledWith('audit:task:task-abc', ts, event.event_id);
|
|
417
|
+
});
|
|
418
|
+
it('fires ZADD for tool call index via pipeline', () => {
|
|
419
|
+
const event = makeEvent({ tool_call_id: 'tc-xyz' });
|
|
420
|
+
store.append(event);
|
|
421
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
422
|
+
expect(pipeline.zadd).toHaveBeenCalledWith('audit:toolcall:tc-xyz', expect.any(Number), event.event_id);
|
|
423
|
+
});
|
|
424
|
+
it('fires ZADD for event type index via pipeline', () => {
|
|
425
|
+
const event = makeEvent({ event_type: 'POLICY_DECIDED' });
|
|
426
|
+
store.append(event);
|
|
427
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
428
|
+
expect(pipeline.zadd).toHaveBeenCalledWith('audit:type:POLICY_DECIDED', expect.any(Number), event.event_id);
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
describe('getByTaskId', () => {
|
|
432
|
+
it('returns events filtered by task_id, sorted by timestamp', () => {
|
|
433
|
+
store.append(makeEvent({ event_id: 'e1', task_id: 'task-A', timestamp: '2025-01-02T00:00:00Z' }));
|
|
434
|
+
store.append(makeEvent({ event_id: 'e2', task_id: 'task-B', timestamp: '2025-01-01T00:00:00Z' }));
|
|
435
|
+
store.append(makeEvent({ event_id: 'e3', task_id: 'task-A', timestamp: '2025-01-01T00:00:00Z' }));
|
|
436
|
+
const results = store.getByTaskId('task-A');
|
|
437
|
+
expect(results).toHaveLength(2);
|
|
438
|
+
expect(results[0].event_id).toBe('e3'); // earlier timestamp
|
|
439
|
+
expect(results[1].event_id).toBe('e1');
|
|
440
|
+
});
|
|
441
|
+
it('returns empty array for unknown task', () => {
|
|
442
|
+
expect(store.getByTaskId('nonexistent')).toEqual([]);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
describe('getByToolCallId', () => {
|
|
446
|
+
it('returns events filtered by tool_call_id, sorted by timestamp', () => {
|
|
447
|
+
store.append(makeEvent({ event_id: 'e1', tool_call_id: 'tc-A', timestamp: '2025-01-03T00:00:00Z' }));
|
|
448
|
+
store.append(makeEvent({ event_id: 'e2', tool_call_id: 'tc-B' }));
|
|
449
|
+
store.append(makeEvent({ event_id: 'e3', tool_call_id: 'tc-A', timestamp: '2025-01-01T00:00:00Z' }));
|
|
450
|
+
const results = store.getByToolCallId('tc-A');
|
|
451
|
+
expect(results).toHaveLength(2);
|
|
452
|
+
expect(results[0].event_id).toBe('e3');
|
|
453
|
+
expect(results[1].event_id).toBe('e1');
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
describe('getByEventType', () => {
|
|
457
|
+
it('returns events filtered by event type', () => {
|
|
458
|
+
store.append(makeEvent({ event_id: 'e1', event_type: 'TOOL_CALL_RECEIVED' }));
|
|
459
|
+
store.append(makeEvent({ event_id: 'e2', event_type: 'POLICY_DECIDED' }));
|
|
460
|
+
store.append(makeEvent({ event_id: 'e3', event_type: 'TOOL_CALL_RECEIVED' }));
|
|
461
|
+
const results = store.getByEventType('TOOL_CALL_RECEIVED');
|
|
462
|
+
expect(results).toHaveLength(2);
|
|
463
|
+
expect(results.every(e => e.event_type === 'TOOL_CALL_RECEIVED')).toBe(true);
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
describe('getAll', () => {
|
|
467
|
+
it('returns a copy of all events', () => {
|
|
468
|
+
store.append(makeEvent({ event_id: 'e1' }));
|
|
469
|
+
store.append(makeEvent({ event_id: 'e2' }));
|
|
470
|
+
const all = store.getAll();
|
|
471
|
+
expect(all).toHaveLength(2);
|
|
472
|
+
// Verify it returns a copy, not the internal array
|
|
473
|
+
all.push(makeEvent({ event_id: 'e3' }));
|
|
474
|
+
expect(store.getAll()).toHaveLength(2);
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
describe('clear', () => {
|
|
478
|
+
it('removes all events from local cache', () => {
|
|
479
|
+
store.append(makeEvent({ event_id: 'e1' }));
|
|
480
|
+
store.append(makeEvent({ event_id: 'e2' }));
|
|
481
|
+
store.clear();
|
|
482
|
+
expect(store.getAll()).toHaveLength(0);
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
describe('hydrate', () => {
|
|
486
|
+
it('loads events from Redis data hash', async () => {
|
|
487
|
+
const event1 = makeEvent({ event_id: 'evt-h1', timestamp: '2025-01-02T00:00:00Z' });
|
|
488
|
+
const event2 = makeEvent({ event_id: 'evt-h2', timestamp: '2025-01-01T00:00:00Z' });
|
|
489
|
+
mockRedis.hgetall.mockResolvedValueOnce({
|
|
490
|
+
'evt-h1': JSON.stringify(event1),
|
|
491
|
+
'evt-h2': JSON.stringify(event2),
|
|
492
|
+
});
|
|
493
|
+
await store.hydrate();
|
|
494
|
+
const all = store.getAll();
|
|
495
|
+
expect(all).toHaveLength(2);
|
|
496
|
+
// Should be sorted by timestamp
|
|
497
|
+
expect(all[0].event_id).toBe('evt-h2'); // earlier
|
|
498
|
+
expect(all[1].event_id).toBe('evt-h1');
|
|
499
|
+
});
|
|
500
|
+
it('skips unparseable entries during hydrate', async () => {
|
|
501
|
+
mockRedis.hgetall.mockResolvedValueOnce({
|
|
502
|
+
'evt-good': JSON.stringify(makeEvent({ event_id: 'evt-good' })),
|
|
503
|
+
'evt-bad': 'not-valid-json{{{',
|
|
504
|
+
});
|
|
505
|
+
await store.hydrate();
|
|
506
|
+
expect(store.getAll()).toHaveLength(1);
|
|
507
|
+
expect(store.getAll()[0].event_id).toBe('evt-good');
|
|
508
|
+
});
|
|
509
|
+
it('handles hydrate errors gracefully', async () => {
|
|
510
|
+
mockRedis.hgetall.mockRejectedValueOnce(new Error('Connection refused'));
|
|
511
|
+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
512
|
+
await store.hydrate();
|
|
513
|
+
expect(consoleSpy).toHaveBeenCalledWith('[RedisAuditStore] hydrate failed:', 'Connection refused');
|
|
514
|
+
consoleSpy.mockRestore();
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
describe('prefix', () => {
|
|
518
|
+
it('uses the default prefix "audit:"', () => {
|
|
519
|
+
const event = makeEvent({ event_id: 'evt-p1', task_id: 'task-p1' });
|
|
520
|
+
store.append(event);
|
|
521
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
522
|
+
expect(pipeline.hset).toHaveBeenCalledWith('audit:data', 'evt-p1', expect.any(String));
|
|
523
|
+
expect(pipeline.zadd).toHaveBeenCalledWith('audit:task:task-p1', expect.any(Number), 'evt-p1');
|
|
524
|
+
});
|
|
525
|
+
it('uses a custom prefix', () => {
|
|
526
|
+
const customStore = new redis_1.RedisAuditStore(mockRedis, 'myapp:audit:');
|
|
527
|
+
const event = makeEvent({ event_id: 'evt-c1', task_id: 'task-c1' });
|
|
528
|
+
customStore.append(event);
|
|
529
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
530
|
+
expect(pipeline.hset).toHaveBeenCalledWith('myapp:audit:data', 'evt-c1', expect.any(String));
|
|
531
|
+
expect(pipeline.zadd).toHaveBeenCalledWith('myapp:audit:task:task-c1', expect.any(Number), 'evt-c1');
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
// ---------------------------------------------------------------------------
|
|
536
|
+
// RedisApprovalStore
|
|
537
|
+
// ---------------------------------------------------------------------------
|
|
538
|
+
describe('RedisApprovalStore', () => {
|
|
539
|
+
let mockRedis;
|
|
540
|
+
let store;
|
|
541
|
+
const makeApproval = (overrides = {}) => makeApprovalRecord(overrides);
|
|
542
|
+
beforeEach(() => {
|
|
543
|
+
mockRedis = createMockRedis();
|
|
544
|
+
store = new redis_1.RedisApprovalStore(mockRedis);
|
|
545
|
+
});
|
|
546
|
+
describe('save / getById', () => {
|
|
547
|
+
it('stores and retrieves an approval by ID', () => {
|
|
548
|
+
const approval = makeApproval({ approval_id: 'appr-100' });
|
|
549
|
+
store.save('appr-100', approval);
|
|
550
|
+
expect(store.getById('appr-100')).toEqual(approval);
|
|
551
|
+
});
|
|
552
|
+
it('returns undefined for unknown approval ID', () => {
|
|
553
|
+
expect(store.getById('nonexistent')).toBeUndefined();
|
|
554
|
+
});
|
|
555
|
+
it('fires pipeline to Redis data hash', () => {
|
|
556
|
+
const approval = makeApproval({ approval_id: 'appr-200' });
|
|
557
|
+
store.save('appr-200', approval);
|
|
558
|
+
expect(mockRedis.pipeline).toHaveBeenCalled();
|
|
559
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
560
|
+
expect(pipeline.hset).toHaveBeenCalledWith('approvals:data', 'appr-200', JSON.stringify(approval));
|
|
561
|
+
});
|
|
562
|
+
it('adds to pending set when status is pending', () => {
|
|
563
|
+
const approval = makeApproval({ status: 'pending' });
|
|
564
|
+
store.save('appr-001', approval);
|
|
565
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
566
|
+
expect(pipeline.sadd).toHaveBeenCalledWith('approvals:pending', 'appr-001');
|
|
567
|
+
});
|
|
568
|
+
it('removes from pending set when status is not pending', () => {
|
|
569
|
+
const approval = makeApproval({ status: 'approved' });
|
|
570
|
+
store.save('appr-001', approval);
|
|
571
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
572
|
+
expect(pipeline.srem).toHaveBeenCalledWith('approvals:pending', 'appr-001');
|
|
573
|
+
});
|
|
574
|
+
it('updates workspace index when workspace_id is present', () => {
|
|
575
|
+
const approval = makeApproval({ workspace_id: 'ws-500' });
|
|
576
|
+
store.save('appr-001', approval);
|
|
577
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
578
|
+
expect(pipeline.hset).toHaveBeenCalledWith('approvals:workspace:ws-500', 'appr-001', '1');
|
|
579
|
+
});
|
|
580
|
+
it('updates tool call index when tool_call_id is present', () => {
|
|
581
|
+
const approval = makeApproval({ tool_call_id: 'tc-500' });
|
|
582
|
+
store.save('appr-001', approval);
|
|
583
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
584
|
+
expect(pipeline.hset).toHaveBeenCalledWith('approvals:toolcall', 'tc-500', 'appr-001');
|
|
585
|
+
});
|
|
586
|
+
});
|
|
587
|
+
describe('getByToken', () => {
|
|
588
|
+
it('returns approval by token after indexToken', () => {
|
|
589
|
+
const approval = makeApproval({ approval_id: 'appr-t1' });
|
|
590
|
+
store.save('appr-t1', approval);
|
|
591
|
+
store.indexToken('jwt-token-abc', 'appr-t1');
|
|
592
|
+
expect(store.getByToken('jwt-token-abc')).toEqual(approval);
|
|
593
|
+
});
|
|
594
|
+
it('returns undefined for unknown token', () => {
|
|
595
|
+
expect(store.getByToken('unknown-token')).toBeUndefined();
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
describe('getByToolCallId', () => {
|
|
599
|
+
it('returns approval matching tool_call_id', () => {
|
|
600
|
+
const approval = makeApproval({ approval_id: 'appr-tc1', tool_call_id: 'tc-findme' });
|
|
601
|
+
store.save('appr-tc1', approval);
|
|
602
|
+
expect(store.getByToolCallId('tc-findme')).toEqual(approval);
|
|
603
|
+
});
|
|
604
|
+
it('returns undefined when no approval matches', () => {
|
|
605
|
+
expect(store.getByToolCallId('tc-nothing')).toBeUndefined();
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
describe('findPending', () => {
|
|
609
|
+
it('returns only pending approvals', () => {
|
|
610
|
+
store.save('appr-1', makeApproval({ approval_id: 'appr-1', status: 'pending' }));
|
|
611
|
+
store.save('appr-2', makeApproval({ approval_id: 'appr-2', status: 'approved' }));
|
|
612
|
+
store.save('appr-3', makeApproval({ approval_id: 'appr-3', status: 'pending' }));
|
|
613
|
+
const pending = store.findPending();
|
|
614
|
+
expect(pending).toHaveLength(2);
|
|
615
|
+
expect(pending.map((a) => a.approval_id).sort()).toEqual(['appr-1', 'appr-3']);
|
|
616
|
+
});
|
|
617
|
+
it('filters by workspace_id when provided', () => {
|
|
618
|
+
store.save('appr-1', makeApproval({ approval_id: 'appr-1', status: 'pending', workspace_id: 'ws-A' }));
|
|
619
|
+
store.save('appr-2', makeApproval({ approval_id: 'appr-2', status: 'pending', workspace_id: 'ws-B' }));
|
|
620
|
+
store.save('appr-3', makeApproval({ approval_id: 'appr-3', status: 'pending', workspace_id: 'ws-A' }));
|
|
621
|
+
const pending = store.findPending('ws-A');
|
|
622
|
+
expect(pending).toHaveLength(2);
|
|
623
|
+
expect(pending.every((a) => a.workspace_id === 'ws-A')).toBe(true);
|
|
624
|
+
});
|
|
625
|
+
it('returns empty array when no pending approvals exist', () => {
|
|
626
|
+
store.save('appr-1', makeApproval({ status: 'approved' }));
|
|
627
|
+
expect(store.findPending()).toEqual([]);
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
describe('indexToken', () => {
|
|
631
|
+
it('stores token-to-approvalId mapping', () => {
|
|
632
|
+
store.indexToken('my-jwt-token', 'appr-idx1');
|
|
633
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('approvals:tokens', 'my-jwt-token', 'appr-idx1');
|
|
634
|
+
});
|
|
635
|
+
it('allows lookup by token after indexing', () => {
|
|
636
|
+
const approval = makeApproval({ approval_id: 'appr-idx2' });
|
|
637
|
+
store.save('appr-idx2', approval);
|
|
638
|
+
store.indexToken('token-xyz', 'appr-idx2');
|
|
639
|
+
expect(store.getByToken('token-xyz')).toEqual(approval);
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
describe('clear', () => {
|
|
643
|
+
it('removes all local state', () => {
|
|
644
|
+
store.save('appr-1', makeApproval({ approval_id: 'appr-1' }));
|
|
645
|
+
store.save('appr-2', makeApproval({ approval_id: 'appr-2' }));
|
|
646
|
+
store.indexToken('tok-1', 'appr-1');
|
|
647
|
+
store.clear();
|
|
648
|
+
expect(store.getById('appr-1')).toBeUndefined();
|
|
649
|
+
expect(store.getById('appr-2')).toBeUndefined();
|
|
650
|
+
expect(store.getByToken('tok-1')).toBeUndefined();
|
|
651
|
+
expect(store.findPending()).toEqual([]);
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
describe('hydrate', () => {
|
|
655
|
+
it('loads approvals and token index from Redis', async () => {
|
|
656
|
+
const approval1 = makeApproval({ approval_id: 'appr-h1', status: 'pending' });
|
|
657
|
+
const approval2 = makeApproval({ approval_id: 'appr-h2', status: 'approved' });
|
|
658
|
+
mockRedis.hgetall
|
|
659
|
+
.mockResolvedValueOnce({
|
|
660
|
+
'appr-h1': JSON.stringify(approval1),
|
|
661
|
+
'appr-h2': JSON.stringify(approval2),
|
|
662
|
+
}) // data hash
|
|
663
|
+
.mockResolvedValueOnce({
|
|
664
|
+
'tok-h1': 'appr-h1',
|
|
665
|
+
'tok-h2': 'appr-h2',
|
|
666
|
+
}); // tokens hash
|
|
667
|
+
await store.hydrate();
|
|
668
|
+
expect(store.getById('appr-h1')).toEqual(approval1);
|
|
669
|
+
expect(store.getById('appr-h2')).toEqual(approval2);
|
|
670
|
+
expect(store.getByToken('tok-h1')).toEqual(approval1);
|
|
671
|
+
expect(store.getByToken('tok-h2')).toEqual(approval2);
|
|
672
|
+
expect(store.findPending()).toHaveLength(1);
|
|
673
|
+
});
|
|
674
|
+
it('skips unparseable entries during hydrate', async () => {
|
|
675
|
+
mockRedis.hgetall
|
|
676
|
+
.mockResolvedValueOnce({
|
|
677
|
+
'appr-ok': JSON.stringify(makeApproval({ approval_id: 'appr-ok' })),
|
|
678
|
+
'appr-bad': '{invalid json!!!',
|
|
679
|
+
})
|
|
680
|
+
.mockResolvedValueOnce({});
|
|
681
|
+
await store.hydrate();
|
|
682
|
+
expect(store.getById('appr-ok')).toBeDefined();
|
|
683
|
+
expect(store.getById('appr-bad')).toBeUndefined();
|
|
684
|
+
});
|
|
685
|
+
it('handles hydrate errors gracefully', async () => {
|
|
686
|
+
mockRedis.hgetall.mockRejectedValueOnce(new Error('Redis timeout'));
|
|
687
|
+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
688
|
+
await store.hydrate();
|
|
689
|
+
expect(consoleSpy).toHaveBeenCalledWith('[RedisApprovalStore] hydrate failed:', 'Redis timeout');
|
|
690
|
+
consoleSpy.mockRestore();
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
describe('prefix', () => {
|
|
694
|
+
it('uses the default prefix "approvals:"', () => {
|
|
695
|
+
const approval = makeApproval();
|
|
696
|
+
store.save('appr-001', approval);
|
|
697
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
698
|
+
expect(pipeline.hset).toHaveBeenCalledWith('approvals:data', 'appr-001', expect.any(String));
|
|
699
|
+
});
|
|
700
|
+
it('uses a custom prefix', () => {
|
|
701
|
+
const customStore = new redis_1.RedisApprovalStore(mockRedis, 'myapp:approvals:');
|
|
702
|
+
const approval = makeApproval();
|
|
703
|
+
customStore.save('appr-001', approval);
|
|
704
|
+
const pipeline = mockRedis.pipeline.mock.results[0].value;
|
|
705
|
+
expect(pipeline.hset).toHaveBeenCalledWith('myapp:approvals:data', 'appr-001', expect.any(String));
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
// ---------------------------------------------------------------------------
|
|
710
|
+
// createRedisStores
|
|
711
|
+
// ---------------------------------------------------------------------------
|
|
712
|
+
describe('createRedisStores', () => {
|
|
713
|
+
let mockRedis;
|
|
714
|
+
beforeEach(() => {
|
|
715
|
+
mockRedis = createMockRedis();
|
|
716
|
+
});
|
|
717
|
+
it('returns all 5 store instances', () => {
|
|
718
|
+
const stores = (0, redis_1.createRedisStores)(mockRedis);
|
|
719
|
+
expect(stores.budgetStore).toBeInstanceOf(redis_1.RedisBudgetStore);
|
|
720
|
+
expect(stores.auditStore).toBeInstanceOf(redis_1.RedisAuditStore);
|
|
721
|
+
expect(stores.approvalStore).toBeInstanceOf(redis_1.RedisApprovalStore);
|
|
722
|
+
expect(stores.rateLimitStore).toBeInstanceOf(redis_1.RedisRateLimitStore);
|
|
723
|
+
expect(stores.idempotencyStore).toBeInstanceOf(redis_1.RedisIdempotencyStore);
|
|
724
|
+
});
|
|
725
|
+
it('applies prefix to all stores', () => {
|
|
726
|
+
const stores = (0, redis_1.createRedisStores)(mockRedis, 'myapp:');
|
|
727
|
+
// Verify budget store uses prefixed key
|
|
728
|
+
stores.budgetStore.incrementCounter('test', 1);
|
|
729
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('myapp:budget:counters', 'test', '1');
|
|
730
|
+
// Verify audit store uses prefixed key (via pipeline)
|
|
731
|
+
mockRedis.pipeline.mockClear();
|
|
732
|
+
stores.auditStore.append({
|
|
733
|
+
event_id: 'e1',
|
|
734
|
+
event_type: 'TOOL_CALL_RECEIVED',
|
|
735
|
+
timestamp: '2025-01-01T00:00:00Z',
|
|
736
|
+
tool_call_id: 'tc-1',
|
|
737
|
+
task_id: 'task-1',
|
|
738
|
+
workspace_id: 'ws-1',
|
|
739
|
+
actor_id: 'actor-1',
|
|
740
|
+
tool_name: 'http.get',
|
|
741
|
+
metadata: {},
|
|
742
|
+
});
|
|
743
|
+
expect(mockRedis.pipeline).toHaveBeenCalled();
|
|
744
|
+
const auditPipeline = mockRedis.pipeline.mock.results[0].value;
|
|
745
|
+
expect(auditPipeline.hset).toHaveBeenCalledWith('myapp:audit:data', 'e1', expect.any(String));
|
|
746
|
+
// Clear mock for next assertion
|
|
747
|
+
mockRedis.pipeline.mockClear();
|
|
748
|
+
const mp2 = createMockPipeline();
|
|
749
|
+
mockRedis.pipeline.mockReturnValue(mp2);
|
|
750
|
+
// Verify approval store uses prefixed key (now via pipeline)
|
|
751
|
+
stores.approvalStore.save('appr-1', makeApprovalRecord({ approval_id: 'appr-1', status: 'pending', workspace_id: 'ws-1', tool_call_id: 'tc-1' }));
|
|
752
|
+
expect(mockRedis.pipeline).toHaveBeenCalled();
|
|
753
|
+
expect(mp2.hset).toHaveBeenCalledWith('myapp:approvals:data', 'appr-1', expect.any(String));
|
|
754
|
+
});
|
|
755
|
+
it('uses empty prefix when none is provided', () => {
|
|
756
|
+
const stores = (0, redis_1.createRedisStores)(mockRedis);
|
|
757
|
+
stores.budgetStore.incrementCounter('test', 1);
|
|
758
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:counters', 'test', '1');
|
|
759
|
+
});
|
|
760
|
+
it('uses empty prefix when explicitly empty string', () => {
|
|
761
|
+
const stores = (0, redis_1.createRedisStores)(mockRedis, '');
|
|
762
|
+
stores.budgetStore.incrementCounter('test', 1);
|
|
763
|
+
expect(mockRedis.hset).toHaveBeenCalledWith('budget:counters', 'test', '1');
|
|
764
|
+
});
|
|
765
|
+
});
|
|
766
|
+
//# sourceMappingURL=redis-storage.test.js.map
|