# ADR-017: Plugin Wrapper Abstraction Framework\n\n**Status**: Proposed\n**Date**: 2026-01-13\n**Author**: Architecture Team\n**Supersedes**: Manual plugin wrapper implementations in `lib_provisioning/plugins/`\n\n## Context\n\nThe provisioning system integrates with four critical plugins, each with its own wrapper layer:\n\n1. **auth.nu** (1066 lines) - Authentication plugin wrapper\n2. **orchestrator.nu** (~500 lines) - Orchestrator plugin wrapper\n3. **secretumvault.nu** (~500 lines) - Secrets vault plugin wrapper\n4. **kms.nu** (~500 lines) - Key management service plugin wrapper\n\nAnalysis reveals ~90% code duplication across these wrappers:\n\n```\n# Pattern repeated 4 times with minor variations:\nexport def plugin-available? [] {\n    # Check if plugin is installed\n}\n\nexport def try-plugin-call [method args] {\n    # Try to call the plugin\n    # On failure, fallback to HTTP\n}\n\nexport def http-fallback-call [endpoint method args] {\n    # HTTP endpoint fallback\n}\n```\n\n## Problem Statement\n\n**Current Architecture**:\n- Each plugin has manual wrapper implementation\n- ~3000 total lines across 4 files\n- Boilerplate code repeated for each plugin method\n- HTTP fallback logic duplicated\n- Error handling inconsistent\n- Testing each wrapper requires custom setup\n\n**Key Metrics**:\n- 3000 lines of plugin wrapper code\n- 90% code similarity\n- 85% reduction opportunity\n\n## Decision\n\nImplement **Plugin Wrapper Abstraction Framework**: replace manual plugin wrappers with a generic proxy framework + declarative YAML definitions.\n\n### Architecture\n\n```\nPlugin Definition (YAML)\n  ├─ plugin: auth\n  ├─ methods:\n  │  ├─ login(username, password)\n  │  ├─ logout()\n  │  └─ status()\n  └─ http_endpoint: http://localhost:8001\n\nGeneric Plugin Proxy Framework\n  ├─ availability() - Check if plugin installed\n  ├─ call() - Try plugin, fallback to HTTP\n  ├─ http_fallback() - HTTP call with retry\n  └─ error_handler() - Consistent error handling\n\nGenerated Wrappers\n  ├─ auth_wrapper.nu (150 lines, autogenerated)\n  ├─ orchestrator_wrapper.nu (150 lines)\n  ├─ vault_wrapper.nu (150 lines)\n  └─ kms_wrapper.nu (150 lines)\n```\n\n### Mechanism\n\n**Plugin Call Flow**:\n\n1. **Check Availability**: Is plugin installed and running?\n2. **Try Plugin Call**: Execute plugin method with timeout\n3. **On Failure**: Fall back to HTTP endpoint\n4. **Error Handling**: Unified error response format\n5. **Retry Logic**: Configurable retry with exponential backoff\n\n### Error Handling Pattern\n\n**Nushell 0.109 Compliant** (do-complete pattern, no try-catch):\n\n```\ndef call-plugin-with-fallback [method: string args: record] {\n    let plugin_result = (\n        do {\n            # Try plugin call\n            call-plugin $method $args\n        } | complete\n    )\n\n    if $plugin_result.exit_code != 0 {\n        # Fall back to HTTP\n        call-http-endpoint $method $args\n    } else {\n        $plugin_result.stdout | from json\n    }\n}\n```\n\n## Consequences\n\n### Positive\n\n- **85% Code Reduction**: 3000 lines → 200 (proxy) + 600 (generated)\n- **Consistency**: All plugins use identical call pattern\n- **Maintainability**: Single proxy implementation vs 4 wrapper files\n- **Testability**: Mock proxy for testing, no plugin-specific setup needed\n- **Extensibility**: New plugins require only YAML definition\n\n### Negative\n\n- **Abstraction Overhead**: Proxy layer adds indirection\n- **YAML Schema**: Must maintain schema for plugin definitions\n- **Migration Risk**: Replacing working code requires careful testing\n\n## Implementation Strategy\n\n1. **Create Generic Proxy** (`lib_provisioning/plugins/proxy.nu`)\n   - Plugin availability detection\n   - Call execution with error handling\n   - HTTP fallback mechanism\n   - Retry logic with backoff\n\n2. **Define Plugin Schema** (`lib_provisioning/plugins/definitions/plugin.schema.yaml`)\n   - Plugin metadata (name, http_endpoint)\n   - Method definitions (parameters, return types)\n   - Fallback configuration (retry count, timeout)\n\n3. **Plugin Definitions** (`lib_provisioning/plugins/definitions/`)\n   - `auth.yaml` - Authentication plugin\n   - `orchestrator.yaml` - Orchestrator plugin\n   - `secretumvault.yaml` - Secrets vault plugin\n   - `kms.yaml` - Key management service plugin\n\n4. **Code Generator** (`tools/codegen/plugin_wrapper_generator.nu`)\n   - Parse plugin YAML definitions\n   - Generate wrapper functions\n   - Ensure Nushell 0.109 compliance\n\n5. **Integration**\n   - Feature flag: `$env.PROVISIONING_USE_GENERATED_PLUGINS`\n   - Gradual migration from manual to generated wrappers\n   - Full compatibility with existing code\n\n## Testing Strategy\n\n1. **Unit Tests**\n   - Plugin availability detection\n   - Successful plugin calls\n   - HTTP fallback on plugin failure\n   - Error handling and retry logic\n\n2. **Integration Tests**\n   - Real plugin calls with actual plugins\n   - Mock HTTP server for fallback testing\n   - Timeout handling\n   - Retry with backoff\n\n3. **Contract Tests**\n   - Plugin method signatures match definitions\n   - Return values have expected structure\n   - Error responses consistent\n\n## Plugin Definitions\n\n### auth.yaml Example\n\n```\nplugin: auth\nhttp_endpoint: http://localhost:8001\nmethods:\n  login:\n    params:\n      username: string\n      password: string\n    returns: {token: string}\n  logout:\n    params: {}\n    returns: {status: string}\n  status:\n    params: {}\n    returns: {authenticated: bool}\n```\n\n## Rollback Strategy\n\n**Feature Flag Approach**:\n\n```\n# Use original manual wrappers\nexport PROVISIONING_USE_GENERATED_PLUGINS=false\n\n# Use new generated proxy framework\nexport PROVISIONING_USE_GENERATED_PLUGINS=true\n```\n\nAllows parallel operation and gradual migration.\n\n## Related ADRs\n\n- **ADR-012**: Nushell/Nickel Plugin CLI Wrapper\n- **ADR-013**: TypeDialog Integration (forms for plugin configuration)\n\n## Open Questions\n\n1. Should plugin definitions be YAML or Nickel?\n2. How do we handle plugin discovery automatically?\n3. What's the expected HTTP endpoint format for all plugins?\n4. Should retry logic be configurable per plugin?\n\n## References\n\n- Nushell 0.109 Guidelines: `.claude/guidelines/nushell.md`\n- Do-Complete Pattern: Error handling without try-catch\n- Plugin Framework: `provisioning/core/nulib/lib_provisioning/plugins/`\n
